27#define NORM_VAL (double)PINS_ANALOG_MAX
28#define MAX_TORQUE CONF_MAX_TORQUE
30#define PEDAL_MIN CONF_PEDAL_MIN
31#define PEDAL_MAX CONF_PEDAL_MAX
33#define BRAKE_MIN CONF_BRAKE_MIN
34#define BRAKE_MAX CONF_BRAKE_MAX
36#define STEER_MIN CONF_STEER_MIN
37#define STEER_MAX CONF_STEER_MAX
39static LOG_TAG ID =
"MotorControl";
41static int motorTorque[2] = {0, 0};
43static bool beating =
true;
44static bool init =
false;
45static bool forward =
true;
47static double pAccum = 0, bAccum = 0, sAccum = 0;
49static Canbus::Buffer MC0_RPM_Buffer(ADD_MC0_RPM);
50static Canbus::Buffer MC1_RPM_Buffer(ADD_MC1_RPM);
54 Canbus::sendData(ADD_MC0_CTRL);
55 Canbus::sendData(ADD_MC1_CTRL);
59constexpr float c = 2 * 3.1415926536 * 9;
62 int16_t MC_Rpm_Val_0 = -MC0_RPM_Buffer.getShort(2);
63 int16_t MC_Rpm_Val_1 = MC1_RPM_Buffer.getShort(2);
73 return (MC_Spd_Val_0 + MC_Spd_Val_1) / 2;
77static void normalizeInput(
double *pedal,
double *brake,
double *steer) {
78 pAccum =
EMAvg(pAccum,
cMap(*pedal, PEDAL_MIN, PEDAL_MAX, 0.0, NORM_VAL), 8);
79 bAccum =
EMAvg(bAccum,
cMap(*brake, BRAKE_MIN, BRAKE_MAX, 0.0, NORM_VAL), 8);
80 sAccum =
EMAvg(sAccum,
cMap(*steer, STEER_MIN, STEER_MAX, 0.0, NORM_VAL), 8);
84 *steer =
cMap(sAccum, 0.0, NORM_VAL, -PI / 9, PI / 9);
87static void torqueVector(
int pedal,
int brake,
int steer) {
89 double _pedal = pedal, _brake = brake, _steer = steer;
91 normalizeInput(&_pedal, &_brake, &_steer);
95 Log.
d(ID,
"Aggression Val x1000:", TVAggression * 1000,
true);
106 motorTorque[0] =
cMap(_pedal, 0.0, NORM_VAL, 0.0, MAX_TORQUE);
107 motorTorque[1] =
cMap(_pedal, 0.0, NORM_VAL, 0.0, MAX_TORQUE);
134 Canbus::sendData(ADD_MC0_CLEAR, 20, 0, 1);
135 Canbus::sendData(ADD_MC1_CLEAR, 20, 0, 1);
136 Canbus::sendData(ADD_MC0_CTRL);
137 Canbus::sendData(ADD_MC1_CTRL);
144int sendSpeed(uint32_t MC_ADD,
int speed,
bool direction,
bool enableBit) {
146 Log.
w(ID,
"Unable to set torque, heartbeat is on");
149 int setSpeed = 0, torqueLimit = 50;
154 uint8_t *bytes = (uint8_t *)&setSpeed;
155 uint8_t *limitBytes = (uint8_t *)&torqueLimit;
156 Canbus::sendData(MC_ADD, 0, 0, bytes[0], bytes[1], direction, 0b101 * enableBit, limitBytes[0], limitBytes[1]);
160void sendTorque(uint32_t MC_ADD,
int torque,
bool direction,
bool enableBit) {
162 Log.
w(ID,
"Unable to set torque, heartbeat is on");
165 uint8_t *bytes = (uint8_t *)&torque;
166 Canbus::sendData(MC_ADD, bytes[0], bytes[1], 0, 0, direction, enableBit);
175 Log.
w(ID,
"Switching direction");
176 forward = runForward;
178 Log.
e(ID,
"Unable to switch direction, car is moving too much");
183 return mc0 ? motorTorque[0] : motorTorque[1];
195 return cMap(sAccum, 0.0, NORM_VAL, -PI / 9, PI / 9);
198void setTorque(
int pedal,
int brake,
int steer) {
199 torqueVector(pedal, brake, steer);
200 sendTorque(ADD_MC1_CTRL, motorTorque[1], forward, 1);
201 sendTorque(ADD_MC0_CTRL, motorTorque[0], forward, 1);
Configure global build properties.
Special logging functionality.
const char * LOG_TAG
Type definition of logging tags This typedef is necessary to allow for easier manipulation of code by...
Logging::Log_t Log
The global logging object.
#define CONF_CAR_GEAR_RATIO
The radius of the car's wheels as a float.
#define CONF_MAXIMUM_SWITCHING_SPEED
The maximum speed value that the car can be moving before it switches between forwards and reverse.
Update, set, and get predefined pin values.
#define PINS_ANALOG_MAX
The maximum analog value, given the current PINS_ANALOG_RES.
double EMAvg(double lastVal, double newVal, int memCount)
Exponential moving average.
Various utility functions.
T cMap(T x, A inMin, B inMax, C outMin, D outMax)
Map a value from one range to another while clamping the value to boundaries.
void addCallback(beatFunc func)
Add a callback to be run at each haertbeat.
void(* beatFunc)(void)
Function called each time the heart beats.
Methods used to more easily interface with RMS Motor controllers over CAN bus.
void sendTorque(uint32_t MC_ADD, int torque, bool direction, bool enableBit)
Send a raw torque command to a MC.
void setTorque(int pedal, int brake, int steer)
Calculate and set the torque of both MCs.
int getLastSteerValue()
Get the last steer value that was internally used.
int getLastBrakeValue()
Get the last brake value that was internally used.
void setDirection(bool runForward)
Set the direction of the motors.
bool isForward(void)
Get whether the motors will spin forward.
int getLastTorqueValue(bool mc0)
Get the last torque percent value sent to a MC.
int getLastPedalValue(void)
Get the last pedal value that was internally used.
int32_t motorSpeed(int motor=-1)
Get the avg motorspeed.
void enableMotorBeating(bool enable)
Start MC heartbeat function, establishing a connection with the MCs.
void clearFaults(void)
Clear MC faults by sending a clear fault command.
void setup(void)
Initialize MC heartbeat function and clear faults, if any.
int getCanPinValue(uint8_t CAN_GPIO_Pin)
Get the pin value of a predefined canbus pin.
void d(LOG_TAG TAG, LOG_MSG message)
Log a string using a debug tag.
void w(LOG_TAG TAG, LOG_MSG message)
Log a string using a warning tag.
void e(LOG_TAG TAG, LOG_MSG message)
Log a string using an error tag.