A port of the smart motor library for ChibiOS/RT, header. More...
Go to the source code of this file.
Data Structures | |
struct | smartMotor |
struct | _smartController |
Typedefs | |
typedef struct _smartController | smartController |
Functions | |
void | SmartMotorsInit (void) |
Initialize the smartMotor library. More... | |
void | SmartMotorLinkMotors (tVexMotor master, tVexMotor slave) |
Link two motors. More... | |
void | SmartMotorsSetEncoderGearing (tVexMotor index, float ratio) |
Set the encoder gearing. More... | |
void | _SmartMotorsAddPowerExtender (int p0, int p1, int p2, int p3,...) |
Add a power extender to the system. More... | |
float | SmartMotorGetSpeed (tVexMotor index) |
Get Motor speed. More... | |
float | _SmartMotorGetCurrent (tVexMotor index, int s,...) |
Get Motor current. More... | |
float | SmartMotorGetTemperature (tVexMotor index) |
Get Motor temperature. More... | |
int | SmartMotorGetLimitCmd (tVexMotor index) |
Get Motor command limit. More... | |
float | SmartMotorGetControllerCurrent (short index) |
Get Controller current. More... | |
float | SmartMotorGetControllerTemperature (short index) |
Get Controller temperature. More... | |
void | SmartMotorPtcMonitorEnable (void) |
Enable current limit by monitoring the PTC temperatures. More... | |
void | SmartMotorPtcMonitorDisable (void) |
Disable current limit by monitoring the PTC temperatures. More... | |
void | SmartMotorCurrentMonitorEnable (void) |
Enable current limit by using a preset threshold. More... | |
void | SmartMotorCurrentMonitorDisable (void) |
Disable current limit by using a preset threshold. More... | |
void | _SmartMotorSetLimitCurent (tVexMotor index, float current,...) |
Set Motor current limit. More... | |
void | SmartMotorSetFreeRpm (tVexMotor index, short max_rpm) |
Set Motor maximum rpm. More... | |
void | SmartMotorSetSlewRate (tVexMotor index, int slew_rate) |
Set Motor slew rate. More... | |
void | SmartMotorRun (void) |
Start the smart motor monitoring. More... | |
void | SmartMotorStop (void) |
Stop smart motor tasks. More... | |
void | SmartMotorSetControllerStatusLed (int index, tVexDigitalPin port) |
Set Controller status LED. More... | |
void | SmartMotorSetPowerExpanderStatusPort (tVexAnalogPin port) |
Set power expander status port. More... | |
void | SmartMotorDebugStatus (void) |
Dump controller and motor status to the debug stream. More... | |
void | _SmartMotorSetRpmSensor (tVexMotor index, tVexAnalogPin port, float ticks_per_rev, bool_t revesed,...) |
Assign (non encoder) sensor for rpm measurement. More... | |
void | _SetMotor (int index, int value, bool_t immediate,...) |
Set motor to value. More... | |
smartMotor * | SmartMotorGetPtr (tVexMotor index) |
Get pointer to smartMotor structure - not used locally. More... | |
smartController * | SmartMotorControllerGetPtr (short index) |
Get pointer to smartController structure - not used locally. More... | |
void | SmartMotorSpeed (smartMotor *m, int deltaTime) |
calculate speed in rpm for the motor More... | |
void | SmartMotorSimulateSpeed (smartMotor *m) |
calculate a simulated speed in rpm for a motor More... | |
float | SmartMotorCurrent (smartMotor *m, float v_battery) |
Estimate smart motor current. More... | |
float | SmartMotorControllerCurrent (smartController *s) |
Calculate the current for a controller bank. More... | |
int | SmartMotorSafeCommand (smartMotor *m, float v_battery) |
Calculate safe current command. More... | |
float | SmartMotorTemperature (smartMotor *m, int deltaTime) |
Calculate the PTC temperature for a motor. More... | |
float | SmartMotorControllerTemperature (smartController *s, int deltaTime) |
Calculate the PTC temperature for a controller bank. More... | |
void | SmartMotorMonitorPtc (smartMotor *m, float v_battery) |
Monitor Motor PTC temperature. More... | |
void | SmartMotorControllerMonitorPtc (smartController *s, float v_battery) |
Monitor controller bank PTC temperature. More... | |
void | SmartMotorMonitorCurrent (smartMotor *m, float v_battery) |
Monitor Motor Current. More... | |
void | SmartMotorControllerSetLed (smartController *s) |
Set the current monitor LED on or off. More... | |
msg_t | SmartMotorTask (void *arg) |
The smart motor task. More... | |
msg_t | SmartMotorSlewRateTask (void *arg) |
The motor slew rate task. More... | |
A port of the smart motor library for ChibiOS/RT, header.
#define kSmartMotorLibVersion 111 |
#define SMLIB_R_SYS 0.3 |
#define SMLIB_PWM_FREQ 1150 |
#define SMLIB_V_DIODE 0.75 |
#define SMLIB_I_FREE_393 0.2 |
#define SMLIB_I_STALL_393 4.8 |
#define SMLIB_RPM_FREE_393 110 |
#define SMLIB_R_393 (7.2/SMLIB_I_STALL_393) |
#define SMLIB_L_393 0.000650 |
#define SMLIB_Ke_393 (7.2*(1-SMLIB_I_FREE_393/SMLIB_I_STALL_393)/SMLIB_RPM_FREE_393) |
#define SMLIB_I_SAFE393 0.90 |
#define SMLIB_I_FREE_269 0.18 |
#define SMLIB_I_STALL_269 2.88 |
#define SMLIB_RPM_FREE_269 120 |
#define SMLIB_R_269 (7.2/SMLIB_I_STALL_269) |
#define SMLIB_L_269 0.000650 |
#define SMLIB_Ke_269 (7.2*(1-SMLIB_I_FREE_269/SMLIB_I_STALL_269)/SMLIB_RPM_FREE_269) |
#define SMLIB_I_SAFE269 0.75 |
#define SMLIB_I_SAFECORTEX 3.0 |
#define SMLIB_I_SAFEPE 3.0 |
#define SMLIB_TPR_269 240.448 |
#define SMLIB_TPR_393S 392 |
#define SMLIB_TPR_393T 627.2 |
#define SMLIB_TPR_QUAD 360.0 |
#define SMLIB_TPR_POT 6000.0 |
#define SMLIB_TEMP_AMBIENT (( 72.0-32.0) * 5 / 9) |
#define SMLIB_TEMP_TRIP 100.0 |
#define SMLIB_TEMP_HYST 10.0 |
#define SMLIB_TEMP_REF 25.0 |
#define SMLIB_I_HOLD_CORTEX 3.0 |
#define SMLIB_T_TRIP_CORTEX 1.7 |
#define SMLIB_K_TAU_CORTEX 0.5 |
#define SMLIB_TAU_CORTEX (SMLIB_K_TAU_CORTEX * SMLIB_T_TRIP_CORTEX * 5.0 * 5.0) |
#define SMLIB_C1_CORTEX ( (SMLIB_TEMP_TRIP - SMLIB_TEMP_REF) / (SMLIB_I_HOLD_CORTEX * SMLIB_I_HOLD_CORTEX) ) |
#define SMLIB_C2_CORTEX (1.0 / (SMLIB_TAU_CORTEX * 1000.0)) |
#define SMLIB_I_HOLD_393 1.0 |
#define SMLIB_T_TRIP_393 7.1 |
#define SMLIB_K_TAU_393 0.5 |
#define SMLIB_TAU_393 (SMLIB_K_TAU_393 * SMLIB_T_TRIP_393 * 5.0 * 5.0) |
#define SMLIB_C1_393 ( (SMLIB_TEMP_TRIP - SMLIB_TEMP_REF) / (SMLIB_I_HOLD_393 * SMLIB_I_HOLD_393) ) |
#define SMLIB_C2_393 (1.0 / (SMLIB_TAU_393 * 1000.0)) |
#define SMLIB_I_HOLD_269 0.75 |
#define SMLIB_T_TRIP_269 2.0 |
#define SMLIB_K_TAU_269 0.5 |
#define SMLIB_TAU_269 (SMLIB_K_TAU_269 * SMLIB_T_TRIP_269 * 5.0 * 5.0) |
#define SMLIB_C1_269 ( (SMLIB_TEMP_TRIP - SMLIB_TEMP_REF) / (SMLIB_I_HOLD_269 * SMLIB_I_HOLD_269) ) |
#define SMLIB_C2_269 (1.0 / (SMLIB_TAU_269 * 1000.0)) |
#define SMLIB_C1_3WIRE SMLIB_C1_269 |
#define SMLIB_C2_3WIRE SMLIB_C2_269 |
#define SMLIB_LEDON 0 |
#define SMLIB_LEDOFF 1 |
#define SMLIB_MOTOR_MAX_CMD 127 |
#define SMLIB_MOTOR_MIN_CMD (-127) |
#define SMLIB_MOTOR_DEFAULT_SLEW_RATE 10 |
#define SMLIB_MOTOR_FAST_SLEW_RATE 256 |
#define SMLIB_MOTOR_DEADBAND 10 |
#define SMLIB_MOTOR_MAX_CMD_UNDEFINED 255 |
#define SMLIB_TOTAL_NUM_CONTROL_BANKS 3 |
#define SMLIB_TOTAL_NUM_BANK_MOTORS 5 |
#define SMLIB_CORTEX_PORT_0 0 |
#define SMLIB_CORTEX_PORT_1 1 |
#define SMLIB_PWREXP_PORT_0 2 |
#define _SmartMotorGetPtr | ( | index) | ((smartMotor *)&sMotors[ index ]) |
#define _SmartMotorControllerGetPtr | ( | index) | ((smartController *)&sPorts[ index ]) |
#define SmartMotorsAddPowerExtender | ( | p0, | |
... | |||
) | _SmartMotorsAddPowerExtender( p0, ##__VA_ARGS__, -1, -1, -1 ) |
#define SmartMotorGetCurrent | ( | index, | |
... | |||
) | _SmartMotorGetCurrent( index, ##__VA_ARGS__, 0 ) |
#define SmartMotorSetLimitCurent | ( | index, | |
... | |||
) | _SmartMotorSetLimitCurent( index, ##__VA_ARGS__, 1.0 ) |
#define SmartMotorSetRpmSensor | ( | index, | |
port, | |||
ticks_per_rev, | |||
... | |||
) | _SmartMotorSetRpmSensor( index, port, ticks_per_rev, ##__VA_ARGS__, FALSE ) |
#define SetMotor | ( | index, | |
value, | |||
... | |||
) | _SetMotor( index, value, ##__VA_ARGS__, FALSE ) |
typedef struct _smartController smartController |
void SmartMotorsInit | ( | void | ) |
Initialize the smartMotor library.
Initialize the smart motor library - This function must be called once when the program starts. Motors are automatically detected and assigned to the cortex banls, if a power expander is used then it should be added by using the SmartMotorsAddPowerExtender function after calling SmartMotorsInit
Link two motors.
[in] | master | The master motor (usually with an encoder) |
[in] | slave | The slave motor (usually un-encoded) |
This lets us use encoder counts from one motor for another motor.
void SmartMotorsSetEncoderGearing | ( | tVexMotor | index, |
float | ratio | ||
) |
Set the encoder gearing.
[in] | index | The motor index |
[in] | ratio | The gear ratio |
If using a quad encoder then it may not be directly connected to the motor. This function allows a change of gearing by changing the ticks per rpm. Only call this function once per motor after calling init
ratio is encoder revs/motor revs
void _SmartMotorsAddPowerExtender | ( | int | p0, |
int | p1, | ||
int | p2, | ||
int | p3, | ||
... | |||
) |
Add a power extender to the system.
[in] | p0 | The first power expander motor port |
[in] | p1 | The second power expander motor port |
[in] | p2 | The third power expander motor port |
[in] | p3 | The fourth power expander motor port |
This function reassigns up to four motors to a power expander rather than ports on the cortex.
There is a macro SmartMotorsAddPowerExtender for ROBOTC compatibility
float SmartMotorGetSpeed | ( | tVexMotor | index) |
Get Motor speed.
[in] | index | The motor index |
float _SmartMotorGetCurrent | ( | tVexMotor | index, |
int | s, | ||
... | |||
) |
Get Motor current.
[in] | index | The motor index |
[in] | s | Set as 1 for signed current otherwise absolute is returned |
float SmartMotorGetTemperature | ( | tVexMotor | index) |
Get Motor temperature.
[in] | index | The motor index |
int SmartMotorGetLimitCmd | ( | tVexMotor | index) |
Get Motor command limit.
[in] | index | The motor index |
float SmartMotorGetControllerCurrent | ( | short | index) |
Get Controller current.
[in] | index | The motor controller index (0, 1 or 2) |
float SmartMotorGetControllerTemperature | ( | short | index) |
Get Controller temperature.
[in] | index | The motor controller index (0, 1 or 2) |
void SmartMotorPtcMonitorEnable | ( | void | ) |
Enable current limit by monitoring the PTC temperatures.
void SmartMotorPtcMonitorDisable | ( | void | ) |
Disable current limit by monitoring the PTC temperatures.
void SmartMotorCurrentMonitorEnable | ( | void | ) |
Enable current limit by using a preset threshold.
void SmartMotorCurrentMonitorDisable | ( | void | ) |
Disable current limit by using a preset threshold.
void _SmartMotorSetLimitCurent | ( | tVexMotor | index, |
float | current, | ||
... | |||
) |
Set Motor current limit.
[in] | index | The motor index |
[in] | current | The maximum motor current |
void SmartMotorSetFreeRpm | ( | tVexMotor | index, |
short | max_rpm | ||
) |
Set Motor maximum rpm.
[in] | index | The motor index |
[in] | max_rpm | The maximum motor rpm |
void SmartMotorSetSlewRate | ( | tVexMotor | index, |
int | slew_rate | ||
) |
Set Motor slew rate.
[in] | index | The motor index |
[in] | slew_rate | The maximum motor slew_rate |
void SmartMotorRun | ( | void | ) |
Start the smart motor monitoring.
After initialization the smart motor tasks need to be started
void SmartMotorStop | ( | void | ) |
Stop smart motor tasks.
void SmartMotorSetControllerStatusLed | ( | int | index, |
tVexDigitalPin | port | ||
) |
Set Controller status LED.
[in] | index | The motor controller index (0, 1 or 2) |
[in] | port | The digital port that the led is connected to |
This function allows an led to be assigned to a controller bank that is illuminated when any motor on that bank trips the ptc or exceeds the preset current limit
void SmartMotorSetPowerExpanderStatusPort | ( | tVexAnalogPin | port) |
Set power expander status port.
[in] | port | The analog port for status from the power expander |
This allows the battery voltage of the power expander to be monitored. Monitoring occurs after the ptc in the power expander so can be used to cut power to those motors if necessary.
void SmartMotorDebugStatus | ( | void | ) |
Dump controller and motor status to the debug stream.
void _SmartMotorSetRpmSensor | ( | tVexMotor | index, |
tVexAnalogPin | port, | ||
float | ticks_per_rev, | ||
bool_t | reversed, | ||
... | |||
) |
Assign (non encoder) sensor for rpm measurement.
[in] | index | The motor index |
[in] | port | The sensor port |
[in] | ticks_per_rev | ticks per rovolution for this sensor |
[in] | reversed | A bool indicating motor direction is reversed |
This function can be used to assign any sensor to a motor for calculating rpm, the most likely situation would be a potentiometer. The parameter ticks_per_rev is the sensor change for one revolution of the motor, for example, a potentiometer with 1:1 gearing has about 6000 ticks_per_rev, if the gearing was 7:1 then this would be reduced to about 857. Set reversed to true if the potentiometer value decreases with positive motor command values.
void _SetMotor | ( | int | index, |
int | value, | ||
bool_t | immediate, | ||
... | |||
) |
Set motor to value.
[in] | index | The motor index |
[in] | value | The motor control value (speed) |
[in] | immediate | If TRUE then bypass the slew rate control |
Set a new control value for the motor. This is the only call that should be used to change motor speed. Using vexMotorSet will completely bugger up the code - do not use it
The naming of this function is inconsistent with the rest of the library to allow backwards compatibility with team 8888's existing library
smartMotor* SmartMotorGetPtr | ( | tVexMotor | index) |
Get pointer to smartMotor structure - not used locally.
[in] | index | The motor index |
smartController* SmartMotorControllerGetPtr | ( | short | index) |
Get pointer to smartController structure - not used locally.
[in] | index | The motor index |
void SmartMotorSpeed | ( | smartMotor * | m, |
int | deltaTime | ||
) |
calculate speed in rpm for the motor
[in] | m | Pointer to smartMotor structure |
[in] | deltaTime | The time in mS from the last call to this function |
void SmartMotorSimulateSpeed | ( | smartMotor * | m) |
calculate a simulated speed in rpm for a motor
[in] | m | Pointer to smartMotor structure |
float SmartMotorCurrent | ( | smartMotor * | m, |
float | v_battery | ||
) |
Estimate smart motor current.
[in] | m | Pointer to smartMotor structure |
[in] | v_battery | The battery voltage in volts |
Estimate current in Vex motor using vamfun's algorithm.
subroutine written by Vamfun...Mentor Vex 1508, 599.
7.13.2012 vamfu... blog info n@ya hoo.c omhttp://vamfun.wordpress.com
Modified by James Pearman 7.28.2012.
Modified by James Pearman 10.1.2012 - more generalized code.
If cmd is positive then rpm must also be positive for this to work.
float SmartMotorControllerCurrent | ( | smartController * | s) |
Calculate the current for a controller bank.
[in] | s | A pointer to a smartController structure |
int SmartMotorSafeCommand | ( | smartMotor * | m, |
float | v_battery | ||
) |
Calculate safe current command.
[in] | m | Pointer to smartMotor structure |
[in] | v_battery | The battery voltage in volts |
Calculate a command for the motor which will result in a target current based on motor speed.
target_current should be set prior to calling the function and must be POSITIVE.
If command direction and rpm are not of the same polarity then this function tends to fallover due to back emf being of opposite direction to the drive direction. In this situation we have no choice but to let current go higher as the motor is changing directions and will in effect stall as it does through zero.
float SmartMotorTemperature | ( | smartMotor * | m, |
int | deltaTime | ||
) |
Calculate the PTC temperature for a motor.
[in] | m | Pointer to smartMotor structure |
[in] | deltaTime | The time in mS from the last call to this function |
float SmartMotorControllerTemperature | ( | smartController * | s, |
int | deltaTime | ||
) |
Calculate the PTC temperature for a controller bank.
[in] | s | Pointer to smartController structure |
[in] | deltaTime | The time in mS from the last call to this function |
void SmartMotorMonitorPtc | ( | smartMotor * | m, |
float | v_battery | ||
) |
Monitor Motor PTC temperature.
[in] | m | Pointer to smartMotor structure |
[in] | v_battery | The battery voltage in volts |
If the temperature is above the set point then calculate a command that will result in a safe current
void SmartMotorControllerMonitorPtc | ( | smartController * | s, |
float | v_battery | ||
) |
Monitor controller bank PTC temperature.
[in] | s | Pointer to smartController structure |
[in] | v_battery | The battery voltage in volts |
If the temperature is above the set point then calculate a commands for each motor that will result in a safe current.
void SmartMotorMonitorCurrent | ( | smartMotor * | m, |
float | v_battery | ||
) |
Monitor Motor Current.
[in] | m | Pointer to smartMotor structure |
[in] | v_battery | The battery voltage in volts |
void SmartMotorControllerSetLed | ( | smartController * | s) |
Set the current monitor LED on or off.
[in] | s | Pointer to smartController structure |
Turn on status LED if either controller ptc is tripped or any connected motors are tripped. LED is only turned on in this function as it may be shared between more then one controller. The LED is briefly turned off each loop around the calling task but only remains off for a very short period which is inperceptable.
msg_t SmartMotorTask | ( | void * | arg) |
The smart motor task.
[in] | arg | pointer to user data (not used) |
msg_t SmartMotorSlewRateTask | ( | void * | arg) |
The motor slew rate task.
[in] | arg | pointer to user data (not used) |
Task - compares the requested speed of each motor to the actual speed and increments or decrements to reduce the difference as necessary