A Roboteq controller c++ interface to allow a Brushless motor control on a CAN bus. Used for a FBL2360 reference.
Dependencies: CAN_FIFO_Triporteur
RoboteqController.h@3:a03c91082856, 2018-05-18 (annotated)
- Committer:
- garivetm
- Date:
- Fri May 18 09:32:45 2018 +0000
- Revision:
- 3:a03c91082856
- Parent:
- 2:d61b4a989cab
- Child:
- 4:1ee0a235c997
Enable RPDO message sending and TPDO message reception.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
garivetm | 0:faf0960419c1 | 1 | #ifndef ROBOTEQ_CONTROLLER_H |
garivetm | 0:faf0960419c1 | 2 | #define ROBOTEQ_CONTROLLER_H |
garivetm | 0:faf0960419c1 | 3 | |
garivetm | 0:faf0960419c1 | 4 | #include "mbed.h" |
garivetm | 0:faf0960419c1 | 5 | #include "PeripheralCAN.h" |
garivetm | 0:faf0960419c1 | 6 | #include "object_dict.h" |
garivetm | 0:faf0960419c1 | 7 | |
garivetm | 0:faf0960419c1 | 8 | // Client Command Specifier |
garivetm | 0:faf0960419c1 | 9 | #define CSS_CMD 2 |
garivetm | 0:faf0960419c1 | 10 | #define CSS_QUERY 4 |
garivetm | 0:faf0960419c1 | 11 | #define CSS_SUCCES 6 |
garivetm | 0:faf0960419c1 | 12 | #define CSS_ERR 8 |
garivetm | 0:faf0960419c1 | 13 | |
garivetm | 0:faf0960419c1 | 14 | // CAN id |
garivetm | 0:faf0960419c1 | 15 | #define SDO_REQUEST 0x600 |
garivetm | 0:faf0960419c1 | 16 | #define SDO_ANSWER 0x580 |
garivetm | 3:a03c91082856 | 17 | #define ID_TPDO1 0x180 |
garivetm | 3:a03c91082856 | 18 | #define ID_TPDO2 0x280 |
garivetm | 3:a03c91082856 | 19 | #define ID_TPDO3 0x380 |
garivetm | 3:a03c91082856 | 20 | #define ID_TPDO4 0x480 |
garivetm | 0:faf0960419c1 | 21 | |
garivetm | 2:d61b4a989cab | 22 | // Motor modes |
garivetm | 2:d61b4a989cab | 23 | #define MODE_OPEN_LOOP 0 // Open-loop |
garivetm | 2:d61b4a989cab | 24 | #define MODE_CLOSED_SPEED 1 // Closed-loop speed |
garivetm | 2:d61b4a989cab | 25 | #define MODE_CLOSED_POS_REL 2 // Closed-loop position relative |
garivetm | 2:d61b4a989cab | 26 | #define MODE_CLOSED_COUNT_POS 3 // Closed-loop count position |
garivetm | 2:d61b4a989cab | 27 | #define MODE_CLOSED_POS_TRACK 4 // Closed-loop position tracking |
garivetm | 2:d61b4a989cab | 28 | #define MODE_CLOSED_TORQUE 5 // Torque |
garivetm | 2:d61b4a989cab | 29 | #define MODE_CLOSED_SPEED_POS 6 // Closed-loop speed position |
garivetm | 2:d61b4a989cab | 30 | |
garivetm | 2:d61b4a989cab | 31 | // Temperatures |
garivetm | 2:d61b4a989cab | 32 | #define TEMPERATURE_MCU 1 // Mcu temperature |
garivetm | 2:d61b4a989cab | 33 | #define TEMPERATURE_CH1 2 // Channel 1 heatsink |
garivetm | 2:d61b4a989cab | 34 | #define TEMPERATURE_CH2 3 // Channel 2 heatsink |
garivetm | 2:d61b4a989cab | 35 | |
garivetm | 0:faf0960419c1 | 36 | class RoboteqController : public PeripheralCAN{ |
garivetm | 0:faf0960419c1 | 37 | public: |
garivetm | 0:faf0960419c1 | 38 | /** Constructors and destructor |
garivetm | 0:faf0960419c1 | 39 | */ |
garivetm | 0:faf0960419c1 | 40 | RoboteqController(); |
garivetm | 3:a03c91082856 | 41 | |
garivetm | 3:a03c91082856 | 42 | /** Constructor |
garivetm | 3:a03c91082856 | 43 | * |
garivetm | 3:a03c91082856 | 44 | * @param controller A pointer on the CAN controller |
garivetm | 3:a03c91082856 | 45 | * @param node_id The controller node id |
garivetm | 3:a03c91082856 | 46 | * @param mot_num Message number inside the controller |
garivetm | 3:a03c91082856 | 47 | * @param TPDO_enabled A flag that enables reception of TPDO messages |
garivetm | 3:a03c91082856 | 48 | */ |
garivetm | 3:a03c91082856 | 49 | RoboteqController(ControllerCAN* controller, uint8_t node_id, uint8_t mot_num, bool TPDO_enabled); |
garivetm | 0:faf0960419c1 | 50 | ~RoboteqController(); |
garivetm | 0:faf0960419c1 | 51 | |
garivetm | 0:faf0960419c1 | 52 | /** Update callback by the CANController |
garivetm | 0:faf0960419c1 | 53 | * |
garivetm | 0:faf0960419c1 | 54 | * @param Id Id of the concerned data structure |
garivetm | 0:faf0960419c1 | 55 | * @param msg CANMessage instance containing the new data |
garivetm | 0:faf0960419c1 | 56 | */ |
garivetm | 0:faf0960419c1 | 57 | void update(const unsigned short& Id, const CANMessage& msg); |
garivetm | 0:faf0960419c1 | 58 | |
garivetm | 3:a03c91082856 | 59 | protected: |
garivetm | 0:faf0960419c1 | 60 | |
garivetm | 3:a03c91082856 | 61 | static const uint16_t RPDO_ID[4]; // RPDO msg IDs to send data to the controller |
garivetm | 3:a03c91082856 | 62 | |
garivetm | 0:faf0960419c1 | 63 | typedef struct { |
garivetm | 0:faf0960419c1 | 64 | uint8_t css; |
garivetm | 0:faf0960419c1 | 65 | uint8_t n; |
garivetm | 0:faf0960419c1 | 66 | uint16_t index; |
garivetm | 0:faf0960419c1 | 67 | uint8_t subindex; |
garivetm | 0:faf0960419c1 | 68 | uint8_t data[4]; |
garivetm | 0:faf0960419c1 | 69 | } SDOMessage; |
garivetm | 0:faf0960419c1 | 70 | /* 1> SDO CMD OR QUERY FRAME: |
garivetm | 0:faf0960419c1 | 71 | ----------------------------------------------------------------------------- |
garivetm | 0:faf0960419c1 | 72 | Header | DLC | Payload |
garivetm | 0:faf0960419c1 | 73 | ----------------------------------------------------------------------------- |
garivetm | 0:faf0960419c1 | 74 | | | Byte0 | Byte1-2 | Byte 3 | Bytes4-7 |
garivetm | 0:faf0960419c1 | 75 | --------------------------------------------------------- |
garivetm | 0:faf0960419c1 | 76 | 0x600+nd | 8 | bits 4-7 bits2-3 bits0-1 | | | |
garivetm | 0:faf0960419c1 | 77 | --------------------------- |
garivetm | 0:faf0960419c1 | 78 | | | css n xx | index | subindex | data |
garivetm | 0:faf0960419c1 | 79 | Note: |
garivetm | 0:faf0960419c1 | 80 | • nd is the destination node id. |
garivetm | 0:faf0960419c1 | 81 | • ccs is the Client Command Specifier, if 2 it is command if 4 it is query. |
garivetm | 0:faf0960419c1 | 82 | • n is the Number of bytes in the data part which do not contain data |
garivetm | 0:faf0960419c1 | 83 | • xx not necessary for basic operation. For more details advise CANOpen standard. |
garivetm | 0:faf0960419c1 | 84 | • index is the object dictionary index of the data to be accessed |
garivetm | 0:faf0960419c1 | 85 | • subindex is the subindex of the object dictionary variable |
garivetm | 0:faf0960419c1 | 86 | • data contains the data to be uploaded. |
garivetm | 0:faf0960419c1 | 87 | |
garivetm | 0:faf0960419c1 | 88 | 2> SDO ANSWER FRAME: |
garivetm | 0:faf0960419c1 | 89 | ----------------------------------------------------------------------------- |
garivetm | 0:faf0960419c1 | 90 | Header | DLC | Payload |
garivetm | 0:faf0960419c1 | 91 | ----------------------------------------------------------------------------- |
garivetm | 0:faf0960419c1 | 92 | | | Byte0 | Byte1-2 | Byte 3 | Bytes4-7 |
garivetm | 0:faf0960419c1 | 93 | --------------------------------------------------------- |
garivetm | 0:faf0960419c1 | 94 | 0x580+nd | 8 | bits 4-7 bits2-3 bits0-1 | | | |
garivetm | 0:faf0960419c1 | 95 | --------------------------- |
garivetm | 0:faf0960419c1 | 96 | | | css n xx | index | subindex | data |
garivetm | 0:faf0960419c1 | 97 | Note: |
garivetm | 0:faf0960419c1 | 98 | • nd is the source node id. |
garivetm | 0:faf0960419c1 | 99 | • ccs is the Client Command Specifier, if 4 it is query response, 6 it is a successful |
garivetm | 0:faf0960419c1 | 100 | response to command, 8 is an error in message received. |
garivetm | 0:faf0960419c1 | 101 | • n is the Number of bytes in the data part which do not contain data |
garivetm | 0:faf0960419c1 | 102 | • xx not necessary for the simplistic way. For more details advise CANOpen standard. |
garivetm | 0:faf0960419c1 | 103 | • index is the object dictionary index of the data to be accessed. |
garivetm | 0:faf0960419c1 | 104 | • subindex is the subindex of the object dictionary variable |
garivetm | 0:faf0960419c1 | 105 | • data contains the data to be uploaded. Applicable only if css=4. |
garivetm | 0:faf0960419c1 | 106 | */ |
garivetm | 0:faf0960419c1 | 107 | void SDO_query(uint8_t n, uint16_t idx, uint8_t sub_idx); |
garivetm | 0:faf0960419c1 | 108 | void SDO_command(uint8_t n, uint16_t idx, uint8_t sub_idx, uint8_t *data); |
garivetm | 3:a03c91082856 | 109 | |
garivetm | 3:a03c91082856 | 110 | /** Helper function that convert a CAN message to a SDO message |
garivetm | 3:a03c91082856 | 111 | * |
garivetm | 3:a03c91082856 | 112 | * @param CANmsg A reference to the CAN message |
garivetm | 3:a03c91082856 | 113 | * @param CANmsg A reference to the SDO message |
garivetm | 3:a03c91082856 | 114 | */ |
garivetm | 0:faf0960419c1 | 115 | void CANMsg2SDO(const CANMessage& CANmsg, SDOMessage& SDOmsg); |
garivetm | 0:faf0960419c1 | 116 | |
garivetm | 3:a03c91082856 | 117 | /** Send an RPDO message to the controller |
garivetm | 3:a03c91082856 | 118 | * |
garivetm | 3:a03c91082856 | 119 | * @param n The RPDO message number (from 1 to 4) |
garivetm | 3:a03c91082856 | 120 | * @param user_var1 The first signed 32-bit integer to be send (the first 4 bytes) |
garivetm | 3:a03c91082856 | 121 | * @param user_var2 The second signed 32-bit integer to be send (the last 4 bytes) |
garivetm | 3:a03c91082856 | 122 | */ |
garivetm | 3:a03c91082856 | 123 | void RPDO_send(uint8_t n, int32_t user_var1, int32_t user_var2); |
garivetm | 3:a03c91082856 | 124 | |
garivetm | 3:a03c91082856 | 125 | /** Parse a TPDO message to user variables |
garivetm | 3:a03c91082856 | 126 | * |
garivetm | 3:a03c91082856 | 127 | * @param CANmsg A reference to the CAN message |
garivetm | 3:a03c91082856 | 128 | * @param user_var1 The first signed 32-bit integer to be read (the first 4 bytes) |
garivetm | 3:a03c91082856 | 129 | * @param user_var2 The second signed 32-bit integer to be read (the last 4 bytes) |
garivetm | 3:a03c91082856 | 130 | */ |
garivetm | 3:a03c91082856 | 131 | void TPDO_parse(const CANMessage& CANmsg, int32_t &user_var1, int32_t &user_var2); |
garivetm | 3:a03c91082856 | 132 | |
garivetm | 3:a03c91082856 | 133 | /** Receive TPDO frames from the controller. If TPDO are used, this virtual |
garivetm | 3:a03c91082856 | 134 | * method must be redefined in a class that inherits from this one. |
garivetm | 3:a03c91082856 | 135 | * |
garivetm | 3:a03c91082856 | 136 | * @param n The TPDO message number (from 1 to 4) |
garivetm | 3:a03c91082856 | 137 | * @param CANmsg A reference to the CAN message |
garivetm | 3:a03c91082856 | 138 | */ |
garivetm | 3:a03c91082856 | 139 | virtual void TPDO_receive(uint8_t n, const CANMessage& CANmsg); |
garivetm | 3:a03c91082856 | 140 | |
garivetm | 0:faf0960419c1 | 141 | uint8_t node_id; // Node id of the controller |
garivetm | 0:faf0960419c1 | 142 | uint8_t mot_num; // Motor number within the controller |
garivetm | 0:faf0960419c1 | 143 | |
garivetm | 2:d61b4a989cab | 144 | /* General Configuration and Safety */ |
garivetm | 2:d61b4a989cab | 145 | // TBD |
garivetm | 2:d61b4a989cab | 146 | |
garivetm | 2:d61b4a989cab | 147 | /* Analog, Digital, Pulse IO Configurations */ |
garivetm | 2:d61b4a989cab | 148 | // TBD |
garivetm | 0:faf0960419c1 | 149 | |
garivetm | 2:d61b4a989cab | 150 | /* Motor configuration (non-exhaustiv list) */ |
garivetm | 2:d61b4a989cab | 151 | uint16_t alim; // Motor ampere limit (0.1 A) |
garivetm | 2:d61b4a989cab | 152 | uint8_t kp; // PID proportionnal gain |
garivetm | 2:d61b4a989cab | 153 | uint8_t ki; // PID integral gain |
garivetm | 2:d61b4a989cab | 154 | uint8_t kd; // PID derivative gain |
garivetm | 2:d61b4a989cab | 155 | int32_t mac; // Motor acceleration rate (0.1 RPM) |
garivetm | 2:d61b4a989cab | 156 | int32_t mdec; // Motor deceleration rate (0.1 RPM) |
garivetm | 2:d61b4a989cab | 157 | uint8_t mmod; // Motor mode |
garivetm | 2:d61b4a989cab | 158 | uint16_t mxrpm; // Maximal RPM value (eq to a cmd of 1000) |
garivetm | 2:d61b4a989cab | 159 | |
garivetm | 2:d61b4a989cab | 160 | int16_t amp_bat; // Battery current (0.1 A) |
garivetm | 2:d61b4a989cab | 161 | int16_t amp_motor; // Motor current (0.1 A) |
garivetm | 2:d61b4a989cab | 162 | int16_t feedback; // Closed loop feedback |
garivetm | 3:a03c91082856 | 163 | int32_t error; // Closed loop error |
garivetm | 2:d61b4a989cab | 164 | int8_t temp_MCU; // MCU temperature |
garivetm | 3:a03c91082856 | 165 | int8_t temp_ch1; // channel 1 temperature |
garivetm | 2:d61b4a989cab | 166 | int8_t temp_ch2; // channel 2 temperature |
garivetm | 2:d61b4a989cab | 167 | int32_t vars[16]; // Integer user variables |
garivetm | 2:d61b4a989cab | 168 | bool booleans[16]; // Boolean user variables |
garivetm | 2:d61b4a989cab | 169 | int16_t speed; // Rotor speed |
garivetm | 2:d61b4a989cab | 170 | |
garivetm | 2:d61b4a989cab | 171 | /* CAN Identifiers */ |
garivetm | 0:faf0960419c1 | 172 | unsigned short Id_CSS_REQ; // CAN ID : CSS request (query or command) |
garivetm | 0:faf0960419c1 | 173 | unsigned short Id_CSS_ANS; // CAN ID : CSS answer (from query or command) |
garivetm | 3:a03c91082856 | 174 | unsigned short Id_TPDO1; // CAN ID : TPDO1 message |
garivetm | 3:a03c91082856 | 175 | unsigned short Id_TPDO2; // CAN ID : TPDO2 message |
garivetm | 3:a03c91082856 | 176 | unsigned short Id_TPDO3; // CAN ID : TPDO3 message |
garivetm | 3:a03c91082856 | 177 | unsigned short Id_TPDO4; // CAN ID : TPDO4 message |
garivetm | 0:faf0960419c1 | 178 | }; |
garivetm | 0:faf0960419c1 | 179 | |
garivetm | 0:faf0960419c1 | 180 | #endif |