Used to communicate with the Maxon EPOS 24/2 motor controller using the ST Nucleo micro controller. Can be easily modified to work with other micro controllers as well. Only limited functionality (velocity mode and current reading).
maxonEPOS2.h
00001 /* 00002 * @file maxonEPOS2.h 00003 * @brief Library to interact with the EPOS 24/2 Maxon motor controller via ST Nucleo micro-controller 00004 * @author Nadun Wijesinghe 00005 * @date 2016 October 6 00006 * @version 1.0 00007 * 00008 * Built on top of the code written by Alexander Zahabizadeh 00009 * (file Arduino_to_Maxon_EPOS.ino). 00010 * 00011 * The purpose is to control a Maxon motor using EPOS 24/2 controller module. 00012 * The parameters of the Maxon motor must be set using EPOS Studio (maximum 00013 * velocity, maximum current, thermal time constant winding etc) before using 00014 * this file. 00015 * 00016 */ 00017 00018 #ifndef MAXONEPOS2_H_ 00019 #define MAXONEPOS2_H_ 00020 00021 #include "mbed.h" 00022 #include "BufferedSerial.h" 00023 00024 /** Class for communicating with Maxon EPOS 24/2 over the serial interface (and communicating with the PC via USB) 00025 * 00026 * Can be used for transmitting and receiving for the following functions: 00027 * Running on velocity mode 00028 * Reading current 00029 * 00030 * Example: 00031 * @code 00032 * #include "mbed.h" 00033 * #include "maxonEPOS2.h" 00034 * #include "BufferedSerial.h" 00035 * 00036 * int main() 00037 * { 00038 * // Declare variables and serial interface 00039 * BufferedSerial pc (USBTX, USBRX, 256, 4); 00040 * int serialID = 2; 00041 * unsigned char nodeID = 0x01; 00042 * long motorSpeed = 8000; 00043 * long receivedErrorCode = 0; 00044 * long current = 0; 00045 * 00046 * maxonEPOS2 motor (serialID, 1, 0); 00047 * motor.init(); 00048 * motor.debug("Initialization done.\n"); 00049 * 00050 * // Start up the motor and set the velocity 00051 * receivedErrorCode = motor.faultReset(nodeID); 00052 * if (!receivedErrorCode) receivedErrorCode = motor.motorShutdown(nodeID); 00053 * if (!receivedErrorCode) receivedErrorCode = motor.motorVelocityModeOn(nodeID); wait(1); 00054 * if (!receivedErrorCode) receivedErrorCode = motor.motorSwitchOn(nodeID); wait(1); 00055 * if (!receivedErrorCode) receivedErrorCode = motor.motorSetVelocity(nodeID, motorSpeed); wait(1); 00056 * 00057 * if (!receivedErrorCode) 00058 * { 00059 * // Measure the current 10 times 00060 * for (int i = 0; i < 10; i++) 00061 * { 00062 * receivedErrorCode = motor.motorGetCurrent(nodeID); 00063 * current = motor.parseData(); 00064 * pc.printf("Current: %l\n", current); 00065 * wait_ms(1000); 00066 * } 00067 * } 00068 * 00069 * // Shut down the motor and EPOS 00070 * if (!receivedErrorCode) receivedErrorCode = motor.motorSetVelocity(nodeID, 0); 00071 * if (!receivedErrorCode) receivedErrorCode = motor.motorShutdown(nodeID); 00072 * 00073 * if (receivedErrorCode) pc.printf ("Error code %d received.\n", receivedErrorCode); 00074 * 00075 * exit(0); 00076 * } 00077 * @endcode 00078 */ 00079 00080 /** 00081 * @class maxonEPOS2 00082 * @brief Controllers for the Maxon EPOS 24/2 for the ST Nucleo 00083 */ 00084 class maxonEPOS2 00085 { 00086 public: 00087 00088 00089 //maxonEPOS2(); 00090 00091 // Transmission error values 00092 #define noError 0 00093 #define firstConfirmFail -1 00094 #define firstConfirmError -2 00095 #define secondConfirmFail -3 00096 #define secondConfirmError -4 00097 #define responseError -5 00098 00099 /** 00100 * Create a maxonEPOS2 object, connected to the PC and the specified transmit and received pair 00101 * @param serialID The TX/RX pin pair .Only works for the pairs Serial2, Serial3, Serial5 and Serial 7 (default). 00102 * @param debugHigh 1 to enable high level debugging on PC via the USB port (0 to disable) 00103 * @param debugLow 1 to enable low level debugging on PC via the USB port (0 to disable) 00104 * @note A serial interface to connect to the PC via the USB interface is automatically created. 00105 */ 00106 maxonEPOS2 (int serialID, int debugHigh, int debugLow) : pc(getSerialPC()), epos(getSerialEPOS(serialID)), 00107 DEBUG_HL(debugHigh), DEBUG_LL(debugLow) {} 00108 00109 /** 00110 * Initialize the debugging serial port (PC) and motor controller serial port (EPOS) 00111 * @note The PC serial port is set for 9600 baud rate, EPOS2 serial port is set to 115200 baud rate. 00112 * @note Both serial ports are set to use 8 bits, no parity bit and 1 stop bit 00113 */ 00114 void init(); 00115 00116 /** 00117 * Display a debug statement on the serial interface on PC 00118 * @param s The string to be sent through the PC serial port 00119 */ 00120 void debug(const char* s); 00121 00122 /* 00123 * Get the BuffereSerial object to communicate with EPOS via the defined serial ID 00124 * @param serialID the serial port number of the ST Nucleo (only 2, 3, 5 and 7 are supported) 00125 * @return The serial object for EPOS communication 00126 */ 00127 BufferedSerial maxonEPOS2::getSerialEPOS(int serialID); 00128 00129 /* 00130 * Get the BuffereSerial object to communicate with PC via the USB port 00131 * @return The serial object for PC communication 00132 */ 00133 BufferedSerial maxonEPOS2::getSerialPC(); 00134 00135 // Motor control 00136 00137 /* Clear faults on the EPOS 00138 * @param nodeID The node ID 00139 * @return error code after communication (0 if no error) 00140 * @see Transmission error values 00141 */ 00142 long faultReset (unsigned char nodeID); 00143 00144 /* Shut down the EPOS 00145 * @param nodeID The node ID 00146 * @return error code after communication (0 if no error) 00147 * @see Transmission error values 00148 */ 00149 long motorShutdown (unsigned char nodeID); 00150 00151 /* Switch on the EPOS 00152 * @param nodeID The node ID 00153 * @return error code after communication (0 if no error) 00154 * @see Transmission error values 00155 */ 00156 long motorSwitchOn (unsigned char nodeID); 00157 00158 /* Turn on velocity mode 00159 * @param nodeID The node ID 00160 * @return error code after communication (0 if no error) 00161 * @see Transmission error values 00162 */ 00163 long motorVelocityModeOn (unsigned char nodeID); 00164 00165 /* Set motor velocity 00166 * @param nodeID The node ID 00167 * @param motorSpeed The motor speed (should be less than the maximum defined motor speed done in the setup stage) 00168 * @return error code after communication (0 if no error) 00169 * @see Transmission error values 00170 */ 00171 long motorSetVelocity (unsigned char nodeID, long motorSpeed); 00172 00173 /* Get the motor current 00174 * @param nodeID The node ID 00175 * @return error code after communication (0 if no error) 00176 * @see Transmission error values 00177 * @see A separate function (parseData) must be called to parse the data fields and compute the current 00178 */ 00179 long motorGetCurrent (unsigned char nodeID); 00180 00181 /** 00182 * Parse the received array to get the data 00183 * @return The number included in the 4 data bytes of the received message 00184 * @see Can be used with the motorGetCurrent() function 00185 */ 00186 long parseData (); 00187 00188 private: 00189 00190 // Serial interfaces and debugging 00191 BufferedSerial pc; 00192 BufferedSerial epos; 00193 char DEBUG_HL; 00194 char DEBUG_LL; 00195 00196 // Transmitting and receiving 00197 #define TX_RX_ARRAY_SIZE 16 00198 unsigned char command[TX_RX_ARRAY_SIZE]; 00199 unsigned char transmitArray[TX_RX_ARRAY_SIZE]; 00200 unsigned int transmitArraySize; 00201 unsigned char receivedArray[TX_RX_ARRAY_SIZE]; 00202 unsigned int receivedArraySize; 00203 00204 // Op-codes 00205 #define opCode_Write 0x11 00206 #define opCode_Read 0x10 00207 00208 // Transmission constants and masks 00209 #define MAX_TRANSMIT_COUNT 5 00210 #define COMMAND_DELAY 100 00211 #define HIGH_BYTE_MASK 0xFF00 00212 #define LOW_BYTE_MASK 0x00FF 00213 00214 // Motor parameters 00215 #define MAX_PROFILE_VELOCITY {0x27, 0x10} 00216 #define MAX_ACCELERATION {0x4E, 0x20} 00217 00218 // Indexes and sub indexes 00219 // Common 00220 #define i_ControlWord 0x6040 00221 #define subI_ControlWord 0x00 00222 #define i_ModesOfOperation 0x6060 00223 #define subI_ModesOfOperation 0x00 00224 00225 // Position mode 00226 #define i_MinPositionLimit 0x607D 00227 #define subI_MinPositionLimit 0x01 00228 #define i_MaxPositionLimit 0x607D 00229 #define subI_MaxPositionLimit 0x02 00230 #define i_PositionModeSettingValue 0x2062 00231 #define subI_PositionModeSettingValue 0x00 00232 #define i_MaxFollowingError 0x6065 00233 #define subI_MaxFollowingError 0x00 00234 00235 // Velocity mode 00236 #define i_VelocityModeSettingValue 0x206B 00237 #define subI_VelocityModeSettingValue 0x00 00238 #define i_MaxProfileVelocity 0x607F 00239 #define subI_MaxProfileVelocity 0x00 00240 #define i_MaxAcceleration 0x60C5 00241 #define subI_MaxAcceleration 0x00 00242 00243 // Current measuring 00244 #define i_CurrentActualValueAveraged 0x2027 00245 #define subI_CurrentActualValueAveraged 0x00 00246 00247 // Data values 00248 #define d_FaultReset {0x00, 0x80} 00249 #define d_Shutdown {0x00, 0x06} 00250 #define d_SwitchOn {0x00, 0x0F} 00251 #define d_CurrentMode {0xFF, 0xFD} 00252 #define d_PositionMode {0xFF, 0xFF} 00253 #define d_VelocityMode {0xFF, 0xFE} 00254 00255 00256 // Function prototypes 00257 // Data transmission and receiving 00258 void buildSerialCommand (unsigned char opCode, unsigned int index, 00259 unsigned char nodeID, unsigned char subIndex, unsigned char* data, 00260 unsigned int dataSize); 00261 void buildTransmitArray (); 00262 long transmitCommand (); 00263 int getObject (unsigned char comms[], int commsSize); 00264 void clearBuffer (); 00265 00266 00267 // Data processing and debugging 00268 long parseErrorCode (int transmitResult); 00269 void printArray (unsigned char* array, unsigned int arraySize, 00270 const char* arrayName, const char* sizeName, char printHeader); 00271 void numberToByteArray (long number, unsigned char* data); 00272 uint16_t crc_xmodem_update (uint16_t crc, uint8_t data); 00273 uint16_t calc_crc (unsigned char *msg,int n); 00274 00275 }; 00276 00277 #endif /* MAXONEPOS2_H_ */
Generated on Tue Jul 26 2022 01:55:06 by
1.7.2