1
Dependencies: mbed-dev-f303 FastPWM3
main.cpp@55:d614e29c60c5, 2021-04-14 (annotated)
- Committer:
- shaorui
- Date:
- Wed Apr 14 11:46:16 2021 +0000
- Revision:
- 55:d614e29c60c5
- Parent:
- 54:4c9415402628
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benkatz | 22:60276ba87ac6 | 1 | /// high-bandwidth 3-phase motor control, for robots |
benkatz | 22:60276ba87ac6 | 2 | /// Written by benkatz, with much inspiration from bayleyw, nkirkby, scolton, David Otten, and others |
benkatz | 22:60276ba87ac6 | 3 | /// Hardware documentation can be found at build-its.blogspot.com |
benkatz | 22:60276ba87ac6 | 4 | /// Written for the STM32F446, but can be implemented on other STM32 MCU's with some further register-diddling |
benkatz | 22:60276ba87ac6 | 5 | |
benkatz | 23:2adf23ee0305 | 6 | #define REST_MODE 0 |
benkatz | 23:2adf23ee0305 | 7 | #define CALIBRATION_MODE 1 |
benkatz | 26:2b865c00d7e9 | 8 | #define MOTOR_MODE 2 |
benkatz | 23:2adf23ee0305 | 9 | #define SETUP_MODE 4 |
benkatz | 23:2adf23ee0305 | 10 | #define ENCODER_MODE 5 |
Rushu | 49:7eac11914980 | 11 | #define TEST_TRAJECTORY_MODE 6 |
shaorui | 48:1b51771c3647 | 12 | #define J_CALIBRATION_MODE 7 |
benkatz | 44:efcde0af8390 | 13 | #define VERSION_NUM "1.6" |
benkatz | 26:2b865c00d7e9 | 14 | |
benkatz | 18:f1d56f4acb39 | 15 | |
benkatz | 26:2b865c00d7e9 | 16 | float __float_reg[64]; // Floats stored in flash |
shaorui | 48:1b51771c3647 | 17 | //int __int_reg[256]; // Ints stored in flash. Includes position sensor calibration lookup table |
shaorui | 48:1b51771c3647 | 18 | int __int_reg[300]; |
shaorui | 48:1b51771c3647 | 19 | int test1; |
Rushu | 49:7eac11914980 | 20 | int test_jointround_flag=0; |
shaorui | 48:1b51771c3647 | 21 | int stop_sign=0; |
benkatz | 0:4e1c4df6aabd | 22 | #include "mbed.h" |
benkatz | 0:4e1c4df6aabd | 23 | #include "PositionSensor.h" |
benkatz | 20:bf9ea5125d52 | 24 | #include "structs.h" |
benkatz | 20:bf9ea5125d52 | 25 | #include "foc.h" |
benkatz | 22:60276ba87ac6 | 26 | #include "calibration.h" |
benkatz | 20:bf9ea5125d52 | 27 | #include "hw_setup.h" |
benkatz | 23:2adf23ee0305 | 28 | #include "math_ops.h" |
benkatz | 20:bf9ea5125d52 | 29 | #include "current_controller_config.h" |
benkatz | 20:bf9ea5125d52 | 30 | #include "hw_config.h" |
benkatz | 20:bf9ea5125d52 | 31 | #include "motor_config.h" |
benkatz | 23:2adf23ee0305 | 32 | #include "stm32f4xx_flash.h" |
benkatz | 23:2adf23ee0305 | 33 | #include "FlashWriter.h" |
benkatz | 23:2adf23ee0305 | 34 | #include "user_config.h" |
benkatz | 23:2adf23ee0305 | 35 | #include "PreferenceWriter.h" |
benkatz | 42:738fa01b0346 | 36 | #include "CAN_com.h" |
shaorui | 48:1b51771c3647 | 37 | #include "math.h" |
shaorui | 48:1b51771c3647 | 38 | #include "MA700Sensor.h" |
shaorui | 48:1b51771c3647 | 39 | #include "joint_calibration.h" |
shaorui | 53:32218a36df05 | 40 | PreferenceWriter prefs(7); |
shaorui | 48:1b51771c3647 | 41 | //PreferenceWriter prefs(7); |
benkatz | 37:c0f352d6e8e3 | 42 | |
benkatz | 9:d7eb815cb057 | 43 | |
benkatz | 20:bf9ea5125d52 | 44 | GPIOStruct gpio; |
benkatz | 20:bf9ea5125d52 | 45 | ControllerStruct controller; |
benkatz | 20:bf9ea5125d52 | 46 | COMStruct com; |
benkatz | 37:c0f352d6e8e3 | 47 | ObserverStruct observer; |
benkatz | 43:dfb72608639c | 48 | Serial pc(PA_2, PA_3); |
benkatz | 9:d7eb815cb057 | 49 | |
Rushu | 47:55bdc4d5096b | 50 | CAN can(PB_8, PB_9, 1000000); // CAN Rx pin name, CAN Tx pin name, 1000kbps |
benkatz | 26:2b865c00d7e9 | 51 | CANMessage rxMsg; |
benkatz | 26:2b865c00d7e9 | 52 | CANMessage txMsg; |
benkatz | 23:2adf23ee0305 | 53 | |
shaorui | 48:1b51771c3647 | 54 | int i=1;//shaorui add |
shaorui | 48:1b51771c3647 | 55 | float wucha=0; |
shaorui | 48:1b51771c3647 | 56 | float wucha1=0; |
Rushu | 47:55bdc4d5096b | 57 | PositionSensorAM5147 spi(16384, 0.0, NPP); //14 bits encoder, 21 NPP |
shaorui | 48:1b51771c3647 | 58 | PositionSensorMA700 ma700(16384,0.0,NPP); //shaorui add(12/10) |
benkatz | 20:bf9ea5125d52 | 59 | |
benkatz | 23:2adf23ee0305 | 60 | volatile int count = 0; |
benkatz | 23:2adf23ee0305 | 61 | volatile int state = REST_MODE; |
benkatz | 23:2adf23ee0305 | 62 | volatile int state_change; |
shaorui | 55:d614e29c60c5 | 63 | volatile int brake_count=0; |
Rushu | 49:7eac11914980 | 64 | volatile float Joint_init =0; //Joint intial angle |
Rushu | 49:7eac11914980 | 65 | volatile int J_M_flag = 0; // Joint motor angle combine |
benkatz | 20:bf9ea5125d52 | 66 | |
benkatz | 26:2b865c00d7e9 | 67 | void onMsgReceived() { |
benkatz | 26:2b865c00d7e9 | 68 | //msgAvailable = true; |
benkatz | 45:aadebe074af6 | 69 | |
benkatz | 26:2b865c00d7e9 | 70 | can.read(rxMsg); |
benkatz | 28:8c7e29f719c5 | 71 | if((rxMsg.id == CAN_ID)){ |
benkatz | 28:8c7e29f719c5 | 72 | controller.timeout = 0; |
benkatz | 28:8c7e29f719c5 | 73 | if(((rxMsg.data[0]==0xFF) & (rxMsg.data[1]==0xFF) & (rxMsg.data[2]==0xFF) & (rxMsg.data[3]==0xFF) & (rxMsg.data[4]==0xFF) & (rxMsg.data[5]==0xFF) & (rxMsg.data[6]==0xFF) & (rxMsg.data[7]==0xFC))){ |
benkatz | 28:8c7e29f719c5 | 74 | state = MOTOR_MODE; |
benkatz | 28:8c7e29f719c5 | 75 | state_change = 1; |
benkatz | 28:8c7e29f719c5 | 76 | } |
benkatz | 28:8c7e29f719c5 | 77 | else if(((rxMsg.data[0]==0xFF) & (rxMsg.data[1]==0xFF) & (rxMsg.data[2]==0xFF) & (rxMsg.data[3]==0xFF) * (rxMsg.data[4]==0xFF) & (rxMsg.data[5]==0xFF) & (rxMsg.data[6]==0xFF) & (rxMsg.data[7]==0xFD))){ |
benkatz | 28:8c7e29f719c5 | 78 | state = REST_MODE; |
benkatz | 28:8c7e29f719c5 | 79 | state_change = 1; |
benkatz | 37:c0f352d6e8e3 | 80 | gpio.led->write(0);; |
benkatz | 28:8c7e29f719c5 | 81 | } |
benkatz | 28:8c7e29f719c5 | 82 | else if(((rxMsg.data[0]==0xFF) & (rxMsg.data[1]==0xFF) & (rxMsg.data[2]==0xFF) & (rxMsg.data[3]==0xFF) * (rxMsg.data[4]==0xFF) & (rxMsg.data[5]==0xFF) & (rxMsg.data[6]==0xFF) & (rxMsg.data[7]==0xFE))){ |
benkatz | 28:8c7e29f719c5 | 83 | spi.ZeroPosition(); |
benkatz | 28:8c7e29f719c5 | 84 | } |
benkatz | 28:8c7e29f719c5 | 85 | else if(state == MOTOR_MODE){ |
benkatz | 28:8c7e29f719c5 | 86 | unpack_cmd(rxMsg, &controller); |
shaorui | 48:1b51771c3647 | 87 | /* |
shaorui | 48:1b51771c3647 | 88 | if(controller.sidebct1!=controller.sidebct) |
shaorui | 48:1b51771c3647 | 89 | { |
shaorui | 48:1b51771c3647 | 90 | controller.sidebct1=controller.sidebct; |
shaorui | 48:1b51771c3647 | 91 | ma700.WriteRegister(&controller); |
shaorui | 48:1b51771c3647 | 92 | } |
shaorui | 48:1b51771c3647 | 93 | */ |
benkatz | 28:8c7e29f719c5 | 94 | } |
shaorui | 48:1b51771c3647 | 95 | |
benkatz | 37:c0f352d6e8e3 | 96 | pack_reply(&txMsg, controller.theta_mech, controller.dtheta_mech, controller.i_q_filt*KT_OUT); |
benkatz | 37:c0f352d6e8e3 | 97 | can.write(txMsg); |
benkatz | 28:8c7e29f719c5 | 98 | } |
benkatz | 26:2b865c00d7e9 | 99 | |
benkatz | 26:2b865c00d7e9 | 100 | } |
benkatz | 26:2b865c00d7e9 | 101 | |
benkatz | 23:2adf23ee0305 | 102 | void enter_menu_state(void){ |
benkatz | 23:2adf23ee0305 | 103 | printf("\n\r\n\r\n\r"); |
benkatz | 23:2adf23ee0305 | 104 | printf(" Commands:\n\r"); |
benkatz | 44:efcde0af8390 | 105 | wait_us(10); |
benkatz | 26:2b865c00d7e9 | 106 | printf(" m - Motor Mode\n\r"); |
benkatz | 44:efcde0af8390 | 107 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 108 | printf(" c - Calibrate Encoder\n\r"); |
benkatz | 44:efcde0af8390 | 109 | wait_us(10); |
shaorui | 48:1b51771c3647 | 110 | printf(" j - Joint Calibrate Encoder\n\r"); |
shaorui | 48:1b51771c3647 | 111 | wait_us(10); |
shaorui | 48:1b51771c3647 | 112 | printf(" t - Joint test Encoder\n\r"); |
shaorui | 48:1b51771c3647 | 113 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 114 | printf(" s - Setup\n\r"); |
benkatz | 44:efcde0af8390 | 115 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 116 | printf(" e - Display Encoder\n\r"); |
benkatz | 44:efcde0af8390 | 117 | wait_us(10); |
benkatz | 37:c0f352d6e8e3 | 118 | printf(" z - Set Zero Position\n\r"); |
benkatz | 44:efcde0af8390 | 119 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 120 | printf(" esc - Exit to Menu\n\r"); |
benkatz | 44:efcde0af8390 | 121 | wait_us(10); |
benkatz | 23:2adf23ee0305 | 122 | state_change = 0; |
shaorui | 55:d614e29c60c5 | 123 | gpio.brake->write(0); //shut up electrical brake ---set up electrical brake 0% duty cycle, relative to period |
shaorui | 55:d614e29c60c5 | 124 | brake_count=0; |
benkatz | 25:f5741040c4bb | 125 | gpio.enable->write(0); |
benkatz | 37:c0f352d6e8e3 | 126 | gpio.led->write(0); |
benkatz | 23:2adf23ee0305 | 127 | } |
benkatz | 24:58c2d7571207 | 128 | |
benkatz | 24:58c2d7571207 | 129 | void enter_setup_state(void){ |
benkatz | 24:58c2d7571207 | 130 | printf("\n\r\n\r Configuration Options \n\r\n\n"); |
benkatz | 44:efcde0af8390 | 131 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 132 | printf(" %-4s %-31s %-5s %-6s %-5s\n\r\n\r", "prefix", "parameter", "min", "max", "current value"); |
benkatz | 44:efcde0af8390 | 133 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 134 | printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "b", "Current Bandwidth (Hz)", "100", "2000", I_BW); |
benkatz | 44:efcde0af8390 | 135 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 136 | printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "i", "CAN ID", "0", "127", CAN_ID); |
benkatz | 44:efcde0af8390 | 137 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 138 | printf(" %-4s %-31s %-5s %-6s %-5i\n\r", "m", "CAN Master ID", "0", "127", CAN_MASTER); |
benkatz | 44:efcde0af8390 | 139 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 140 | printf(" %-4s %-31s %-5s %-6s %.1f\n\r", "l", "Torque Limit (N-m)", "0.0", "18.0", TORQUE_LIMIT); |
benkatz | 44:efcde0af8390 | 141 | wait_us(10); |
benkatz | 28:8c7e29f719c5 | 142 | printf(" %-4s %-31s %-5s %-6s %d\n\r", "t", "CAN Timeout (cycles)(0 = none)", "0", "100000", CAN_TIMEOUT); |
benkatz | 44:efcde0af8390 | 143 | wait_us(10); |
benkatz | 24:58c2d7571207 | 144 | printf("\n\r To change a value, type 'prefix''value''ENTER'\n\r i.e. 'b1000''ENTER'\n\r\n\r"); |
benkatz | 44:efcde0af8390 | 145 | wait_us(10); |
benkatz | 24:58c2d7571207 | 146 | state_change = 0; |
benkatz | 24:58c2d7571207 | 147 | } |
benkatz | 22:60276ba87ac6 | 148 | |
benkatz | 23:2adf23ee0305 | 149 | void enter_torque_mode(void){ |
shaorui | 55:d614e29c60c5 | 150 | // gpio.brake->write(0.8); |
shaorui | 55:d614e29c60c5 | 151 | gpio.brake->write(1); |
benkatz | 37:c0f352d6e8e3 | 152 | controller.ovp_flag = 0; |
benkatz | 28:8c7e29f719c5 | 153 | gpio.enable->write(1); // Enable gate drive |
benkatz | 28:8c7e29f719c5 | 154 | reset_foc(&controller); // Tesets integrators, and other control loop parameters |
benkatz | 28:8c7e29f719c5 | 155 | wait(.001); |
benkatz | 23:2adf23ee0305 | 156 | controller.i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 157 | controller.i_q_ref = 0; // Current Setpoints |
benkatz | 37:c0f352d6e8e3 | 158 | gpio.led->write(1); // Turn on status LED |
benkatz | 25:f5741040c4bb | 159 | state_change = 0; |
benkatz | 28:8c7e29f719c5 | 160 | printf("\n\r Entering Motor Mode \n\r"); |
benkatz | 23:2adf23ee0305 | 161 | } |
benkatz | 22:60276ba87ac6 | 162 | |
benkatz | 23:2adf23ee0305 | 163 | void calibrate(void){ |
shaorui | 55:d614e29c60c5 | 164 | gpio.brake->write(0.8); |
benkatz | 25:f5741040c4bb | 165 | gpio.enable->write(1); // Enable gate drive |
benkatz | 37:c0f352d6e8e3 | 166 | gpio.led->write(1); // Turn on status LED |
shaorui | 53:32218a36df05 | 167 | order_phases(&spi, &gpio, &controller, &prefs); // Check phase ordering |
benkatz | 25:f5741040c4bb | 168 | calibrate(&spi, &gpio, &controller, &prefs); // Perform calibration procedure |
shaorui | 48:1b51771c3647 | 169 | //j_calibrate(&ma700,&spi, &gpio, &controller, &prefs); |
shaorui | 48:1b51771c3647 | 170 | //j_calibrate(&ma700,&gpio, &controller, &prefs); |
benkatz | 37:c0f352d6e8e3 | 171 | gpio.led->write(0);; // Turn off status LED |
benkatz | 23:2adf23ee0305 | 172 | wait(.2); |
benkatz | 25:f5741040c4bb | 173 | gpio.enable->write(0); // Turn off gate drive |
benkatz | 23:2adf23ee0305 | 174 | printf("\n\r Calibration complete. Press 'esc' to return to menu\n\r"); |
benkatz | 23:2adf23ee0305 | 175 | state_change = 0; |
benkatz | 23:2adf23ee0305 | 176 | } |
benkatz | 23:2adf23ee0305 | 177 | |
shaorui | 48:1b51771c3647 | 178 | void jocalibrate(void){ |
shaorui | 48:1b51771c3647 | 179 | gpio.enable->write(1); // Enable gate drive |
shaorui | 48:1b51771c3647 | 180 | gpio.led->write(1); // Turn on status LED |
Rushu | 49:7eac11914980 | 181 | |
Rushu | 49:7eac11914980 | 182 | //order_phases(&spi, &gpio, &controller, &prefs); // Check phase ordering |
shaorui | 48:1b51771c3647 | 183 | //calibrate(&spi, &gpio, &controller, &prefs); // Perform calibration procedure |
shaorui | 48:1b51771c3647 | 184 | //j_calibrate(&ma700,&spi, &gpio, &controller, &prefs); |
shaorui | 48:1b51771c3647 | 185 | j_calibrate(&ma700,&gpio, &controller, &prefs); |
Rushu | 49:7eac11914980 | 186 | |
Rushu | 49:7eac11914980 | 187 | /*************同时设置转子和关节零位置同步****************///暂时不需要,标定结束,按z归零 |
Rushu | 49:7eac11914980 | 188 | /* |
Rushu | 49:7eac11914980 | 189 | spi.SetMechOffset(0); |
Rushu | 49:7eac11914980 | 190 | ma700.SetMechOffset(0); |
Rushu | 49:7eac11914980 | 191 | spi.Sample(DT); |
Rushu | 49:7eac11914980 | 192 | ma700.Sample(DT); |
Rushu | 49:7eac11914980 | 193 | wait_us(20); |
Rushu | 49:7eac11914980 | 194 | M_OFFSET = spi.GetMechPosition(); |
Rushu | 49:7eac11914980 | 195 | JOINT_M_OFFSET =ma700.GetMechPosition(); |
Rushu | 49:7eac11914980 | 196 | if (!prefs.ready()) prefs.open(); |
Rushu | 49:7eac11914980 | 197 | prefs.flush(); // Write new prefs to flash |
Rushu | 49:7eac11914980 | 198 | prefs.close(); |
Rushu | 49:7eac11914980 | 199 | prefs.load(); |
Rushu | 49:7eac11914980 | 200 | spi.SetMechOffset(M_OFFSET); |
Rushu | 49:7eac11914980 | 201 | ma700.SetMechOffset(JOINT_M_OFFSET); |
Rushu | 49:7eac11914980 | 202 | printf("\n\r Saved new zero position: %.4f\n\r\n\r", M_OFFSET); |
Rushu | 49:7eac11914980 | 203 | printf("\n\r Saved new zero position1: %.4f\n\r\n\r", JOINT_M_OFFSET); |
Rushu | 49:7eac11914980 | 204 | */ |
Rushu | 49:7eac11914980 | 205 | /*************同时设置转子和关节零位置同步****************/ |
Rushu | 49:7eac11914980 | 206 | gpio.led->write(0); // Turn off status LED |
shaorui | 48:1b51771c3647 | 207 | wait(.2); |
shaorui | 48:1b51771c3647 | 208 | gpio.enable->write(0); // Turn off gate drive |
shaorui | 48:1b51771c3647 | 209 | printf("\n\r Calibration complete. Press 'esc' to return to menu\n\r"); |
shaorui | 48:1b51771c3647 | 210 | state_change = 0; |
shaorui | 48:1b51771c3647 | 211 | } |
shaorui | 48:1b51771c3647 | 212 | |
shaorui | 48:1b51771c3647 | 213 | void jointcalibrate(void){ |
shaorui | 48:1b51771c3647 | 214 | gpio.enable->write(1); // Enable gate drive |
shaorui | 48:1b51771c3647 | 215 | gpio.led->write(1); // Turn on status LED |
shaorui | 48:1b51771c3647 | 216 | //joint_calibrate (&ma700,&spi,&gpio,&controller,&prefs); // Perform calibration procedure |
shaorui | 48:1b51771c3647 | 217 | gpio.led->write(0); // Turn off status LED |
shaorui | 48:1b51771c3647 | 218 | wait(.2); |
shaorui | 48:1b51771c3647 | 219 | gpio.enable->write(0); |
shaorui | 48:1b51771c3647 | 220 | |
shaorui | 48:1b51771c3647 | 221 | |
shaorui | 48:1b51771c3647 | 222 | |
shaorui | 48:1b51771c3647 | 223 | /************Trajectory Planning******************************/ |
shaorui | 48:1b51771c3647 | 224 | |
shaorui | 48:1b51771c3647 | 225 | |
shaorui | 48:1b51771c3647 | 226 | // enter_torque_mode(); |
shaorui | 48:1b51771c3647 | 227 | state=MOTOR_MODE; |
shaorui | 48:1b51771c3647 | 228 | state_change=1; |
shaorui | 48:1b51771c3647 | 229 | //enter_torque_mode(); |
shaorui | 48:1b51771c3647 | 230 | count = 0; |
shaorui | 48:1b51771c3647 | 231 | printf("test\n\r"); |
Rushu | 49:7eac11914980 | 232 | controller.p_des=0; |
Rushu | 49:7eac11914980 | 233 | controller.v_des = 1.5f; |
Rushu | 49:7eac11914980 | 234 | controller.kp = 0; |
Rushu | 49:7eac11914980 | 235 | controller.kd = 5.0f; |
Rushu | 49:7eac11914980 | 236 | controller.t_ff = 0; |
shaorui | 48:1b51771c3647 | 237 | /* |
shaorui | 48:1b51771c3647 | 238 | if((1.0f/GR)* spi.GetMechPosition()<=(2*PI)) |
shaorui | 48:1b51771c3647 | 239 | { |
shaorui | 48:1b51771c3647 | 240 | controller.p_des=0; |
shaorui | 48:1b51771c3647 | 241 | controller.v_des = 2.0f; |
shaorui | 48:1b51771c3647 | 242 | controller.kp = 0; |
shaorui | 48:1b51771c3647 | 243 | controller.kd = 5.0f; |
shaorui | 48:1b51771c3647 | 244 | controller.t_ff = 0; |
shaorui | 48:1b51771c3647 | 245 | wait(.5); |
shaorui | 48:1b51771c3647 | 246 | * |
shaorui | 48:1b51771c3647 | 247 | } |
shaorui | 48:1b51771c3647 | 248 | |
shaorui | 48:1b51771c3647 | 249 | |
shaorui | 48:1b51771c3647 | 250 | ************Trajectory Planning*****************************/ |
shaorui | 48:1b51771c3647 | 251 | |
shaorui | 48:1b51771c3647 | 252 | // Turn off gate drive |
shaorui | 48:1b51771c3647 | 253 | printf("\n\r Joint_Calibration complete. Press 'esc' to return to menu\n\r"); |
shaorui | 48:1b51771c3647 | 254 | //state_change = 0; |
shaorui | 48:1b51771c3647 | 255 | } |
shaorui | 48:1b51771c3647 | 256 | |
shaorui | 48:1b51771c3647 | 257 | |
shaorui | 48:1b51771c3647 | 258 | |
benkatz | 23:2adf23ee0305 | 259 | void print_encoder(void){ |
Rushu | 52:d4d5e3414865 | 260 | printf(" AS5147Mechanical Angle: %f Electrical Angle: %f Raw: %d\n\r", spi.GetMechPosition()*57.2957795, spi.GetElecPosition()*57.2957795, spi.GetRawPosition()); |
Rushu | 52:d4d5e3414865 | 261 | printf(" MA700:Mechanical Angle: %f Electrical Angle: %f Raw: %d\n\r", ma700.GetMechPosition()*57.2957795, ma700.GetElecPosition()*57.2957795, ma700.GetRawPosition()); |
Rushu | 52:d4d5e3414865 | 262 | printf(" Joint raw: %f Joint: %f Mech: %f\n\r", controller.theta_joint_raw*57.2957795, controller.theta_joint*57.2957795, controller.theta_mech*57.2957795); |
Rushu | 49:7eac11914980 | 263 | |
Rushu | 49:7eac11914980 | 264 | // float m_position=controller.theta_mech*57.2957795; |
Rushu | 49:7eac11914980 | 265 | // float j_position=-controller.theta_mech1*360/(2.0f*PI)-controller.theta_mech*360/(2.0f*PI)*GR; |
Rushu | 49:7eac11914980 | 266 | // float j_position=-(controller.theta_mech1+controller.theta_mech)*2807.49319614; |
Rushu | 49:7eac11914980 | 267 | wait(.5); |
benkatz | 22:60276ba87ac6 | 268 | } |
benkatz | 20:bf9ea5125d52 | 269 | |
benkatz | 23:2adf23ee0305 | 270 | /// Current Sampling Interrupt /// |
benkatz | 23:2adf23ee0305 | 271 | /// This runs at 40 kHz, regardless of of the mode the controller is in /// |
benkatz | 2:8724412ad628 | 272 | extern "C" void TIM1_UP_TIM10_IRQHandler(void) { |
benkatz | 2:8724412ad628 | 273 | if (TIM1->SR & TIM_SR_UIF ) { |
benkatz | 23:2adf23ee0305 | 274 | |
benkatz | 23:2adf23ee0305 | 275 | ///Sample current always /// |
benkatz | 25:f5741040c4bb | 276 | ADC1->CR2 |= 0x40000000; // Begin sample and conversion |
benkatz | 22:60276ba87ac6 | 277 | //volatile int delay; |
benkatz | 20:bf9ea5125d52 | 278 | //for (delay = 0; delay < 55; delay++); |
Rushu | 52:d4d5e3414865 | 279 | spi.Sample(DT); |
Rushu | 52:d4d5e3414865 | 280 | ma700.Sample(DT); |
Rushu | 52:d4d5e3414865 | 281 | |
benkatz | 37:c0f352d6e8e3 | 282 | controller.adc2_raw = ADC2->DR; // Read ADC Data Registers |
benkatz | 23:2adf23ee0305 | 283 | controller.adc1_raw = ADC1->DR; |
benkatz | 37:c0f352d6e8e3 | 284 | controller.adc3_raw = ADC3->DR; |
Rushu | 52:d4d5e3414865 | 285 | // sample position sensor |
benkatz | 37:c0f352d6e8e3 | 286 | controller.theta_elec = spi.GetElecPosition(); |
Rushu | 49:7eac11914980 | 287 | // controller.theta_mech = (1.0f/GR)*spi.GetMechPosition(); |
benkatz | 37:c0f352d6e8e3 | 288 | controller.dtheta_mech = (1.0f/GR)*spi.GetMechVelocity(); |
benkatz | 37:c0f352d6e8e3 | 289 | controller.dtheta_elec = spi.GetElecVelocity(); |
benkatz | 37:c0f352d6e8e3 | 290 | controller.v_bus = 0.95f*controller.v_bus + 0.05f*((float)controller.adc3_raw)*V_SCALE; |
shaorui | 48:1b51771c3647 | 291 | |
benkatz | 20:bf9ea5125d52 | 292 | |
shaorui | 48:1b51771c3647 | 293 | //////shaorui add for obtaining joint real position |
shaorui | 48:1b51771c3647 | 294 | controller.theta_elec1 = ma700.GetElecPosition(); |
Rushu | 49:7eac11914980 | 295 | controller.theta_mech1 = (1.0f/GR)*ma700.GetMechPosition(); |
shaorui | 55:d614e29c60c5 | 296 | controller.dtheta_mech1 =-(ma700.GetMechVelocity()+spi.GetMechVelocity()); |
shaorui | 48:1b51771c3647 | 297 | controller.dtheta_elec1 = ma700.GetElecVelocity(); |
Rushu | 49:7eac11914980 | 298 | |
Rushu | 49:7eac11914980 | 299 | ///hjb add joint angle |
Rushu | 49:7eac11914980 | 300 | controller.theta_joint_raw= -(ma700.GetMechPosition()+spi.GetMechPosition()); |
Rushu | 51:29e1686e8b3e | 301 | //if(controller.dtheta_mech>0) {controller.theta_joint_raw += PI/180.0f;} //compensate |
Rushu | 51:29e1686e8b3e | 302 | // else if(controller.dtheta_mech<0) {controller.theta_joint_raw -= PI/180.0f;} |
Rushu | 49:7eac11914980 | 303 | if(controller.theta_joint_raw<0){controller.theta_joint_raw += 2.0f*PI;} |
Rushu | 52:d4d5e3414865 | 304 | else if(controller.theta_joint_raw>=2.0f*PI){controller.theta_joint_raw -= 2.0f*PI;} |
Rushu | 52:d4d5e3414865 | 305 | |
Rushu | 52:d4d5e3414865 | 306 | if(controller.theta_joint_raw<0){controller.theta_joint_raw += 2.0f*PI;} //may be still below 0 |
Rushu | 52:d4d5e3414865 | 307 | else if(controller.theta_joint_raw>=2.0f*PI){controller.theta_joint_raw -= 2.0f*PI;} |
Rushu | 49:7eac11914980 | 308 | |
Rushu | 49:7eac11914980 | 309 | controller.theta_joint_raw_fil =0.99f*controller.theta_joint_raw_pre + 0.01f*controller.theta_joint_raw ;//filter |
Rushu | 49:7eac11914980 | 310 | controller.theta_joint_raw_pre =controller.theta_joint_raw_fil; |
Rushu | 49:7eac11914980 | 311 | |
Rushu | 49:7eac11914980 | 312 | //"rad 0~2pi" ("deg" =-(controller.theta_mech1+controller.theta_mech)*2807.49319614; |
Rushu | 49:7eac11914980 | 313 | //int Ncycle=0; |
Rushu | 49:7eac11914980 | 314 | //float Ncycle_mod = 0.0f; |
Rushu | 49:7eac11914980 | 315 | //float Mech_mod = 0.0f; |
Rushu | 49:7eac11914980 | 316 | controller.Ncycle = controller.theta_joint_raw/(2.0f*PI/GR); |
Rushu | 49:7eac11914980 | 317 | controller.Ncycle_mod = fmod(controller.theta_joint_raw,2.0f*PI/GR); |
Rushu | 49:7eac11914980 | 318 | controller.Mech_mod = fmod(spi.GetMechPosition(),2.0f*PI); |
Rushu | 52:d4d5e3414865 | 319 | |
Rushu | 52:d4d5e3414865 | 320 | if(controller.Mech_mod<0){controller.Mech_mod += 2.0f*PI;} |
Rushu | 52:d4d5e3414865 | 321 | else if(controller.Mech_mod>=2.0f*PI){controller.Mech_mod -= 2.0f*PI;} |
Rushu | 52:d4d5e3414865 | 322 | |
Rushu | 49:7eac11914980 | 323 | controller.theta_joint= controller.Ncycle*(2.0f*PI/GR)+controller.Mech_mod/GR; //In the real use, should turn to the theta_mech |
Rushu | 52:d4d5e3414865 | 324 | if(controller.theta_joint<0){controller.theta_joint += 2.0f*PI;} |
Rushu | 52:d4d5e3414865 | 325 | else if(controller.theta_joint>=2.0f*PI){controller.theta_joint -= 2.0f*PI;} |
Rushu | 49:7eac11914980 | 326 | if(J_M_flag == 0) |
Rushu | 49:7eac11914980 | 327 | { |
Rushu | 51:29e1686e8b3e | 328 | if(0<abs(controller.Ncycle_mod*57.2957795-controller.Mech_mod*57.2957795/GR)<2) |
Rushu | 51:29e1686e8b3e | 329 | { |
Rushu | 51:29e1686e8b3e | 330 | if(0<abs((controller.theta_joint- controller.theta_joint_raw)*57.2957795)<2) |
Rushu | 51:29e1686e8b3e | 331 | { |
Rushu | 51:29e1686e8b3e | 332 | Joint_init = controller.theta_joint- (1.0f/GR)*spi.GetMechPosition(); |
Rushu | 51:29e1686e8b3e | 333 | controller.theta_mech = controller.theta_joint;////controller.theta_joint; // easy way to use, whether available in the shock? |
Rushu | 51:29e1686e8b3e | 334 | J_M_flag = 1; |
Rushu | 51:29e1686e8b3e | 335 | } |
Rushu | 51:29e1686e8b3e | 336 | } |
Rushu | 49:7eac11914980 | 337 | controller.theta_mech = controller.theta_joint_raw; |
shaorui | 55:d614e29c60c5 | 338 | |
Rushu | 49:7eac11914980 | 339 | } |
Rushu | 49:7eac11914980 | 340 | else if(J_M_flag == 1) |
Rushu | 49:7eac11914980 | 341 | { |
Rushu | 49:7eac11914980 | 342 | controller.theta_mech = Joint_init + (1.0f/GR)*spi.GetMechPosition(); |
shaorui | 55:d614e29c60c5 | 343 | |
shaorui | 53:32218a36df05 | 344 | |
Rushu | 49:7eac11914980 | 345 | } |
Rushu | 49:7eac11914980 | 346 | |
Rushu | 49:7eac11914980 | 347 | |
shaorui | 48:1b51771c3647 | 348 | /////shaorui end////////////////// |
shaorui | 48:1b51771c3647 | 349 | /* |
shaorui | 48:1b51771c3647 | 350 | controller.c++; |
shaorui | 48:1b51771c3647 | 351 | if(controller.c>=20000) |
shaorui | 48:1b51771c3647 | 352 | { |
shaorui | 48:1b51771c3647 | 353 | controller.cha=controller.init2-controller.init1; |
shaorui | 48:1b51771c3647 | 354 | controller.init1=controller.init2; |
shaorui | 48:1b51771c3647 | 355 | controller.c=0; |
shaorui | 48:1b51771c3647 | 356 | printf("position: %.3f \n\r", controller.cha*360/(2.0f*PI)); |
shaorui | 48:1b51771c3647 | 357 | } |
shaorui | 48:1b51771c3647 | 358 | */ |
benkatz | 23:2adf23ee0305 | 359 | /// Check state machine state, and run the appropriate function /// |
benkatz | 23:2adf23ee0305 | 360 | switch(state){ |
benkatz | 37:c0f352d6e8e3 | 361 | case REST_MODE: // Do nothing |
benkatz | 23:2adf23ee0305 | 362 | if(state_change){ |
benkatz | 23:2adf23ee0305 | 363 | enter_menu_state(); |
shaorui | 48:1b51771c3647 | 364 | wucha=0 ; //shaorui add |
shaorui | 53:32218a36df05 | 365 | test_jointround_flag=0; |
benkatz | 23:2adf23ee0305 | 366 | } |
benkatz | 23:2adf23ee0305 | 367 | break; |
benkatz | 22:60276ba87ac6 | 368 | |
benkatz | 23:2adf23ee0305 | 369 | case CALIBRATION_MODE: // Run encoder calibration procedure |
benkatz | 23:2adf23ee0305 | 370 | if(state_change){ |
benkatz | 23:2adf23ee0305 | 371 | calibrate(); |
shaorui | 55:d614e29c60c5 | 372 | gpio.brake->write(0); //shut up electrical brake ---set up electrical brake 0% duty cycle, relative to period |
benkatz | 23:2adf23ee0305 | 373 | } |
benkatz | 23:2adf23ee0305 | 374 | break; |
benkatz | 23:2adf23ee0305 | 375 | |
shaorui | 48:1b51771c3647 | 376 | case J_CALIBRATION_MODE: // Run encoder calibration procedure |
shaorui | 48:1b51771c3647 | 377 | if(state_change){ |
shaorui | 48:1b51771c3647 | 378 | jocalibrate(); |
shaorui | 48:1b51771c3647 | 379 | |
shaorui | 48:1b51771c3647 | 380 | } |
shaorui | 48:1b51771c3647 | 381 | break; |
shaorui | 48:1b51771c3647 | 382 | |
Rushu | 49:7eac11914980 | 383 | case TEST_TRAJECTORY_MODE: // Run encoder calibration procedure |
shaorui | 48:1b51771c3647 | 384 | if(state_change){ |
Rushu | 49:7eac11914980 | 385 | test_jointround_flag=1; |
shaorui | 55:d614e29c60c5 | 386 | //J_M_flag = 0; |
shaorui | 48:1b51771c3647 | 387 | stop_sign=0; |
shaorui | 48:1b51771c3647 | 388 | jointcalibrate(); |
shaorui | 48:1b51771c3647 | 389 | |
shaorui | 48:1b51771c3647 | 390 | } |
shaorui | 48:1b51771c3647 | 391 | break; |
shaorui | 48:1b51771c3647 | 392 | |
shaorui | 48:1b51771c3647 | 393 | |
benkatz | 26:2b865c00d7e9 | 394 | case MOTOR_MODE: // Run torque control |
benkatz | 25:f5741040c4bb | 395 | if(state_change){ |
benkatz | 25:f5741040c4bb | 396 | enter_torque_mode(); |
benkatz | 28:8c7e29f719c5 | 397 | count = 0; |
benkatz | 25:f5741040c4bb | 398 | } |
benkatz | 28:8c7e29f719c5 | 399 | else{ |
benkatz | 37:c0f352d6e8e3 | 400 | /* |
benkatz | 37:c0f352d6e8e3 | 401 | if(controller.v_bus>28.0f){ //Turn of gate drive if bus voltage is too high, to prevent FETsplosion if the bus is cut during regen |
benkatz | 37:c0f352d6e8e3 | 402 | gpio.enable->write(0); |
benkatz | 37:c0f352d6e8e3 | 403 | controller.ovp_flag = 1; |
benkatz | 37:c0f352d6e8e3 | 404 | state = REST_MODE; |
benkatz | 37:c0f352d6e8e3 | 405 | state_change = 1; |
benkatz | 37:c0f352d6e8e3 | 406 | printf("OVP Triggered!\n\r"); |
benkatz | 37:c0f352d6e8e3 | 407 | } |
benkatz | 37:c0f352d6e8e3 | 408 | */ |
shaorui | 55:d614e29c60c5 | 409 | brake_count++; |
shaorui | 55:d614e29c60c5 | 410 | if(brake_count>=3*40000) |
shaorui | 55:d614e29c60c5 | 411 | { |
shaorui | 55:d614e29c60c5 | 412 | //gpio.brake->write(0.125); //set up electrical brake 12.5% duty cycle, relative to period |
shaorui | 55:d614e29c60c5 | 413 | gpio.brake->write(0.3); |
shaorui | 55:d614e29c60c5 | 414 | } |
shaorui | 48:1b51771c3647 | 415 | torque_control(&controller); |
shaorui | 55:d614e29c60c5 | 416 | |
benkatz | 28:8c7e29f719c5 | 417 | if((controller.timeout > CAN_TIMEOUT) && (CAN_TIMEOUT > 0)){ |
benkatz | 28:8c7e29f719c5 | 418 | controller.i_d_ref = 0; |
benkatz | 28:8c7e29f719c5 | 419 | controller.i_q_ref = 0; |
benkatz | 37:c0f352d6e8e3 | 420 | controller.kp = 0; |
benkatz | 37:c0f352d6e8e3 | 421 | controller.kd = 0; |
benkatz | 37:c0f352d6e8e3 | 422 | controller.t_ff = 0; |
benkatz | 28:8c7e29f719c5 | 423 | } |
shaorui | 55:d614e29c60c5 | 424 | |
benkatz | 37:c0f352d6e8e3 | 425 | commutate(&controller, &observer, &gpio, controller.theta_elec); // Run current loop |
benkatz | 28:8c7e29f719c5 | 426 | controller.timeout += 1; |
benkatz | 38:67e4e1453a4b | 427 | |
Rushu | 49:7eac11914980 | 428 | |
Rushu | 49:7eac11914980 | 429 | count++; |
benkatz | 39:3580a907ef93 | 430 | /* |
benkatz | 40:cd7e837b2b93 | 431 | if(count == 4000){ |
benkatz | 40:cd7e837b2b93 | 432 | printf("%.4f\n\r", controller.dtheta_mech); |
benkatz | 32:ccac5da77844 | 433 | count = 0; |
benkatz | 23:2adf23ee0305 | 434 | } |
benkatz | 39:3580a907ef93 | 435 | */ |
benkatz | 38:67e4e1453a4b | 436 | |
benkatz | 37:c0f352d6e8e3 | 437 | |
benkatz | 37:c0f352d6e8e3 | 438 | } |
benkatz | 23:2adf23ee0305 | 439 | break; |
benkatz | 23:2adf23ee0305 | 440 | case SETUP_MODE: |
benkatz | 23:2adf23ee0305 | 441 | if(state_change){ |
benkatz | 24:58c2d7571207 | 442 | enter_setup_state(); |
benkatz | 23:2adf23ee0305 | 443 | } |
benkatz | 23:2adf23ee0305 | 444 | break; |
benkatz | 23:2adf23ee0305 | 445 | case ENCODER_MODE: |
benkatz | 23:2adf23ee0305 | 446 | print_encoder(); |
benkatz | 23:2adf23ee0305 | 447 | break; |
benkatz | 37:c0f352d6e8e3 | 448 | } |
benkatz | 2:8724412ad628 | 449 | } |
benkatz | 23:2adf23ee0305 | 450 | TIM1->SR = 0x0; // reset the status register |
benkatz | 2:8724412ad628 | 451 | } |
benkatz | 0:4e1c4df6aabd | 452 | |
benkatz | 25:f5741040c4bb | 453 | |
benkatz | 24:58c2d7571207 | 454 | char cmd_val[8] = {0}; |
benkatz | 24:58c2d7571207 | 455 | char cmd_id = 0; |
benkatz | 25:f5741040c4bb | 456 | char char_count = 0; |
benkatz | 24:58c2d7571207 | 457 | |
benkatz | 25:f5741040c4bb | 458 | /// Manage state machine with commands from serial terminal or configurator gui /// |
benkatz | 25:f5741040c4bb | 459 | /// Called when data received over serial /// |
benkatz | 23:2adf23ee0305 | 460 | void serial_interrupt(void){ |
benkatz | 23:2adf23ee0305 | 461 | while(pc.readable()){ |
benkatz | 23:2adf23ee0305 | 462 | char c = pc.getc(); |
benkatz | 25:f5741040c4bb | 463 | if(c == 27){ |
benkatz | 25:f5741040c4bb | 464 | state = REST_MODE; |
benkatz | 25:f5741040c4bb | 465 | state_change = 1; |
shaorui | 53:32218a36df05 | 466 | test_jointround_flag=0; |
benkatz | 25:f5741040c4bb | 467 | char_count = 0; |
benkatz | 25:f5741040c4bb | 468 | cmd_id = 0; |
benkatz | 37:c0f352d6e8e3 | 469 | gpio.led->write(0);; |
benkatz | 25:f5741040c4bb | 470 | for(int i = 0; i<8; i++){cmd_val[i] = 0;} |
benkatz | 25:f5741040c4bb | 471 | } |
benkatz | 24:58c2d7571207 | 472 | if(state == REST_MODE){ |
benkatz | 23:2adf23ee0305 | 473 | switch (c){ |
benkatz | 23:2adf23ee0305 | 474 | case 'c': |
benkatz | 23:2adf23ee0305 | 475 | state = CALIBRATION_MODE; |
benkatz | 23:2adf23ee0305 | 476 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 477 | break; |
shaorui | 48:1b51771c3647 | 478 | |
shaorui | 48:1b51771c3647 | 479 | case 't': |
Rushu | 49:7eac11914980 | 480 | state = TEST_TRAJECTORY_MODE; |
shaorui | 48:1b51771c3647 | 481 | state_change = 1; |
shaorui | 48:1b51771c3647 | 482 | break; |
shaorui | 48:1b51771c3647 | 483 | case 'j': |
shaorui | 48:1b51771c3647 | 484 | state = J_CALIBRATION_MODE; |
shaorui | 48:1b51771c3647 | 485 | state_change = 1; |
shaorui | 48:1b51771c3647 | 486 | break; |
shaorui | 48:1b51771c3647 | 487 | |
benkatz | 26:2b865c00d7e9 | 488 | case 'm': |
benkatz | 26:2b865c00d7e9 | 489 | state = MOTOR_MODE; |
benkatz | 23:2adf23ee0305 | 490 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 491 | break; |
benkatz | 23:2adf23ee0305 | 492 | case 'e': |
benkatz | 23:2adf23ee0305 | 493 | state = ENCODER_MODE; |
benkatz | 23:2adf23ee0305 | 494 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 495 | break; |
benkatz | 23:2adf23ee0305 | 496 | case 's': |
benkatz | 23:2adf23ee0305 | 497 | state = SETUP_MODE; |
benkatz | 23:2adf23ee0305 | 498 | state_change = 1; |
benkatz | 23:2adf23ee0305 | 499 | break; |
shaorui | 48:1b51771c3647 | 500 | |
benkatz | 37:c0f352d6e8e3 | 501 | case 'z': |
benkatz | 37:c0f352d6e8e3 | 502 | spi.SetMechOffset(0); |
shaorui | 48:1b51771c3647 | 503 | ma700.SetMechOffset(0); |
benkatz | 45:aadebe074af6 | 504 | spi.Sample(DT); |
shaorui | 48:1b51771c3647 | 505 | ma700.Sample(DT); |
benkatz | 37:c0f352d6e8e3 | 506 | wait_us(20); |
benkatz | 37:c0f352d6e8e3 | 507 | M_OFFSET = spi.GetMechPosition(); |
shaorui | 48:1b51771c3647 | 508 | JOINT_M_OFFSET = ma700.GetMechPosition(); |
benkatz | 37:c0f352d6e8e3 | 509 | if (!prefs.ready()) prefs.open(); |
benkatz | 37:c0f352d6e8e3 | 510 | prefs.flush(); // Write new prefs to flash |
benkatz | 37:c0f352d6e8e3 | 511 | prefs.close(); |
benkatz | 37:c0f352d6e8e3 | 512 | prefs.load(); |
benkatz | 37:c0f352d6e8e3 | 513 | spi.SetMechOffset(M_OFFSET); |
shaorui | 48:1b51771c3647 | 514 | ma700.SetMechOffset(JOINT_M_OFFSET); |
shaorui | 53:32218a36df05 | 515 | J_M_flag=0; |
Rushu | 49:7eac11914980 | 516 | printf("\n\r Saved new M-J J-J zero position: %.4f %.4f\n\r\n\r", M_OFFSET, JOINT_M_OFFSET); |
Rushu | 49:7eac11914980 | 517 | /* |
shaorui | 48:1b51771c3647 | 518 | for(int i=0;i<300;i++) |
shaorui | 48:1b51771c3647 | 519 | { |
shaorui | 48:1b51771c3647 | 520 | printf("%.3d %.3d\n\r",i,__int_reg[i] ); |
shaorui | 48:1b51771c3647 | 521 | } |
shaorui | 48:1b51771c3647 | 522 | for(int j=0;j<64;j++) |
shaorui | 48:1b51771c3647 | 523 | { |
shaorui | 48:1b51771c3647 | 524 | printf("%.3d %.3f\n\r",j,__float_reg[j] ); |
shaorui | 48:1b51771c3647 | 525 | } |
Rushu | 49:7eac11914980 | 526 | */ |
benkatz | 37:c0f352d6e8e3 | 527 | break; |
benkatz | 37:c0f352d6e8e3 | 528 | } |
benkatz | 37:c0f352d6e8e3 | 529 | |
benkatz | 24:58c2d7571207 | 530 | } |
benkatz | 24:58c2d7571207 | 531 | else if(state == SETUP_MODE){ |
benkatz | 25:f5741040c4bb | 532 | if(c == 13){ |
benkatz | 24:58c2d7571207 | 533 | switch (cmd_id){ |
benkatz | 24:58c2d7571207 | 534 | case 'b': |
benkatz | 24:58c2d7571207 | 535 | I_BW = fmaxf(fminf(atof(cmd_val), 2000.0f), 100.0f); |
benkatz | 24:58c2d7571207 | 536 | break; |
benkatz | 24:58c2d7571207 | 537 | case 'i': |
benkatz | 24:58c2d7571207 | 538 | CAN_ID = atoi(cmd_val); |
benkatz | 24:58c2d7571207 | 539 | break; |
benkatz | 26:2b865c00d7e9 | 540 | case 'm': |
benkatz | 26:2b865c00d7e9 | 541 | CAN_MASTER = atoi(cmd_val); |
benkatz | 26:2b865c00d7e9 | 542 | break; |
benkatz | 24:58c2d7571207 | 543 | case 'l': |
benkatz | 24:58c2d7571207 | 544 | TORQUE_LIMIT = fmaxf(fminf(atof(cmd_val), 18.0f), 0.0f); |
benkatz | 24:58c2d7571207 | 545 | break; |
benkatz | 28:8c7e29f719c5 | 546 | case 't': |
benkatz | 28:8c7e29f719c5 | 547 | CAN_TIMEOUT = atoi(cmd_val); |
benkatz | 28:8c7e29f719c5 | 548 | break; |
benkatz | 24:58c2d7571207 | 549 | default: |
benkatz | 24:58c2d7571207 | 550 | printf("\n\r '%c' Not a valid command prefix\n\r\n\r", cmd_id); |
benkatz | 24:58c2d7571207 | 551 | break; |
benkatz | 24:58c2d7571207 | 552 | } |
benkatz | 24:58c2d7571207 | 553 | |
benkatz | 24:58c2d7571207 | 554 | if (!prefs.ready()) prefs.open(); |
benkatz | 24:58c2d7571207 | 555 | prefs.flush(); // Write new prefs to flash |
benkatz | 24:58c2d7571207 | 556 | prefs.close(); |
benkatz | 24:58c2d7571207 | 557 | prefs.load(); |
benkatz | 24:58c2d7571207 | 558 | state_change = 1; |
benkatz | 24:58c2d7571207 | 559 | char_count = 0; |
benkatz | 24:58c2d7571207 | 560 | cmd_id = 0; |
benkatz | 24:58c2d7571207 | 561 | for(int i = 0; i<8; i++){cmd_val[i] = 0;} |
benkatz | 24:58c2d7571207 | 562 | } |
benkatz | 24:58c2d7571207 | 563 | else{ |
benkatz | 24:58c2d7571207 | 564 | if(char_count == 0){cmd_id = c;} |
benkatz | 24:58c2d7571207 | 565 | else{ |
benkatz | 24:58c2d7571207 | 566 | cmd_val[char_count-1] = c; |
benkatz | 24:58c2d7571207 | 567 | |
benkatz | 24:58c2d7571207 | 568 | } |
benkatz | 24:58c2d7571207 | 569 | pc.putc(c); |
benkatz | 24:58c2d7571207 | 570 | char_count++; |
benkatz | 23:2adf23ee0305 | 571 | } |
benkatz | 23:2adf23ee0305 | 572 | } |
benkatz | 24:58c2d7571207 | 573 | else if (state == ENCODER_MODE){ |
benkatz | 24:58c2d7571207 | 574 | switch (c){ |
benkatz | 24:58c2d7571207 | 575 | case 27: |
benkatz | 24:58c2d7571207 | 576 | state = REST_MODE; |
benkatz | 24:58c2d7571207 | 577 | state_change = 1; |
benkatz | 24:58c2d7571207 | 578 | break; |
benkatz | 24:58c2d7571207 | 579 | } |
benkatz | 24:58c2d7571207 | 580 | } |
benkatz | 24:58c2d7571207 | 581 | |
benkatz | 24:58c2d7571207 | 582 | } |
benkatz | 22:60276ba87ac6 | 583 | } |
benkatz | 0:4e1c4df6aabd | 584 | |
benkatz | 0:4e1c4df6aabd | 585 | int main() { |
benkatz | 45:aadebe074af6 | 586 | |
benkatz | 20:bf9ea5125d52 | 587 | controller.v_bus = V_BUS; |
benkatz | 22:60276ba87ac6 | 588 | controller.mode = 0; |
shaorui | 48:1b51771c3647 | 589 | controller.sidebct1=0; |
benkatz | 23:2adf23ee0305 | 590 | Init_All_HW(&gpio); // Setup PWM, ADC, GPIO |
shaorui | 55:d614e29c60c5 | 591 | gpio.brake->period_ms(25); //set up electrical brake 250 ms period |
benkatz | 9:d7eb815cb057 | 592 | wait(.1); |
benkatz | 26:2b865c00d7e9 | 593 | gpio.enable->write(1); |
benkatz | 45:aadebe074af6 | 594 | TIM1->CCR3 = PWM_ARR*(1.0f); // Write duty cycles |
benkatz | 45:aadebe074af6 | 595 | TIM1->CCR2 = PWM_ARR*(1.0f); |
benkatz | 45:aadebe074af6 | 596 | TIM1->CCR1 = PWM_ARR*(1.0f); |
benkatz | 23:2adf23ee0305 | 597 | zero_current(&controller.adc1_offset, &controller.adc2_offset); // Measure current sensor zero-offset |
benkatz | 26:2b865c00d7e9 | 598 | gpio.enable->write(0); |
benkatz | 23:2adf23ee0305 | 599 | reset_foc(&controller); // Reset current controller |
benkatz | 26:2b865c00d7e9 | 600 | TIM1->CR1 ^= TIM_CR1_UDIS; |
benkatz | 26:2b865c00d7e9 | 601 | //TIM1->CR1 |= TIM_CR1_UDIS; //enable interrupt |
Rushu | 54:4c9415402628 | 602 | reset_foc(&controller); // Reset current controller |
Rushu | 54:4c9415402628 | 603 | reset_observer(&observer); // Reset observer |
benkatz | 20:bf9ea5125d52 | 604 | wait(.1); |
shaorui | 48:1b51771c3647 | 605 | NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 0); // commutation > communication |
benkatz | 43:dfb72608639c | 606 | |
benkatz | 37:c0f352d6e8e3 | 607 | NVIC_SetPriority(CAN1_RX0_IRQn, 3); |
Rushu | 52:d4d5e3414865 | 608 | // If preferences haven't been user configured yet, set defaults |
Rushu | 52:d4d5e3414865 | 609 | prefs.load(); // Read flash |
Rushu | 52:d4d5e3414865 | 610 | can.filter(CAN_ID , 0xFFF, CANStandard, 0); |
benkatz | 28:8c7e29f719c5 | 611 | txMsg.id = CAN_MASTER; |
benkatz | 28:8c7e29f719c5 | 612 | txMsg.len = 6; |
benkatz | 26:2b865c00d7e9 | 613 | rxMsg.len = 8; |
Rushu | 52:d4d5e3414865 | 614 | can.attach(&onMsgReceived); |
Rushu | 52:d4d5e3414865 | 615 | |
benkatz | 37:c0f352d6e8e3 | 616 | if(isnan(E_OFFSET)){E_OFFSET = 0.0f;} |
benkatz | 37:c0f352d6e8e3 | 617 | if(isnan(M_OFFSET)){M_OFFSET = 0.0f;} |
benkatz | 25:f5741040c4bb | 618 | spi.SetElecOffset(E_OFFSET); // Set position sensor offset |
benkatz | 37:c0f352d6e8e3 | 619 | spi.SetMechOffset(M_OFFSET); |
Rushu | 52:d4d5e3414865 | 620 | |
Rushu | 54:4c9415402628 | 621 | // Set potision sensor nonlinearity lookup table |
Rushu | 54:4c9415402628 | 622 | init_controller_params(&controller); |
shaorui | 53:32218a36df05 | 623 | //pc.baud(460800);//pc.baud(921600); |
shaorui | 53:32218a36df05 | 624 | pc.baud(115000); |
Rushu | 52:d4d5e3414865 | 625 | //=====benk 2020 last version===// |
Rushu | 52:d4d5e3414865 | 626 | |
Rushu | 52:d4d5e3414865 | 627 | spi.Sample(1.0f); |
Rushu | 52:d4d5e3414865 | 628 | printf("MP: %.3f\n\r",spi.GetMechPosition()); |
Rushu | 52:d4d5e3414865 | 629 | if(spi.GetMechPosition() > 2.0f*PI){spi.SetMechOffset(M_OFFSET+2.0f*PI);} // now zeroes to +- 30 degrees about nominal, independent of rollover point |
Rushu | 52:d4d5e3414865 | 630 | else if (spi.GetMechPosition() < 0){spi.SetMechOffset(M_OFFSET-2.0f*PI);} |
Rushu | 52:d4d5e3414865 | 631 | //=====benk 2020 last version===// |
Rushu | 52:d4d5e3414865 | 632 | |
Rushu | 49:7eac11914980 | 633 | if(isnan(JOINT_E_OFFSET)){JOINT_E_OFFSET = 0.0f;} |
Rushu | 49:7eac11914980 | 634 | if(isnan(JOINT_M_OFFSET)){JOINT_M_OFFSET = 0.0f;} |
Rushu | 49:7eac11914980 | 635 | ma700.SetElecOffset(JOINT_E_OFFSET); // Set position sensor offset |
Rushu | 49:7eac11914980 | 636 | ma700.SetMechOffset(JOINT_M_OFFSET); |
Rushu | 52:d4d5e3414865 | 637 | //=====benk 2020 last version===// |
Rushu | 52:d4d5e3414865 | 638 | ma700.Sample(1.0f); |
Rushu | 52:d4d5e3414865 | 639 | printf("JP: %.3f\n\r",ma700.GetMechPosition()); |
Rushu | 52:d4d5e3414865 | 640 | if(ma700.GetMechPosition() > 2.0f*PI){ma700.SetMechOffset(JOINT_M_OFFSET+2.0f*PI);} // now zeroes to +- 30 degrees about nominal, independent of rollover point |
Rushu | 52:d4d5e3414865 | 641 | else if (ma700.GetMechPosition() < 0){ma700.SetMechOffset(JOINT_M_OFFSET-2.0f*PI);} |
Rushu | 52:d4d5e3414865 | 642 | |
Rushu | 52:d4d5e3414865 | 643 | spi.Sample(1.0f); |
Rushu | 52:d4d5e3414865 | 644 | ma700.Sample(1.0f); |
Rushu | 52:d4d5e3414865 | 645 | printf("MP: %.3f JP: %.3f\n\r",spi.GetMechPosition(),ma700.GetMechPosition()); |
Rushu | 52:d4d5e3414865 | 646 | |
Rushu | 52:d4d5e3414865 | 647 | //=====benk 2020 last version===// |
benkatz | 23:2adf23ee0305 | 648 | int lut[128] = {0}; |
Rushu | 49:7eac11914980 | 649 | int joint[128]={0}; //shaorui |
benkatz | 23:2adf23ee0305 | 650 | memcpy(&lut, &ENCODER_LUT, sizeof(lut)); |
shaorui | 48:1b51771c3647 | 651 | spi.WriteLUT(lut); |
shaorui | 48:1b51771c3647 | 652 | memcpy(&joint, &ENCODER_JOINT , sizeof(joint)); |
shaorui | 53:32218a36df05 | 653 | ma700.WriteLUT(joint); |
Rushu | 52:d4d5e3414865 | 654 | // set serial baud rate |
benkatz | 20:bf9ea5125d52 | 655 | wait(.01); |
Rushu | 52:d4d5e3414865 | 656 | printf("MP: %.3f JP: %.3f\n\r",spi.GetMechPosition(),ma700.GetMechPosition()); |
Rushu | 52:d4d5e3414865 | 657 | |
Rushu | 49:7eac11914980 | 658 | pc.printf("\n\r\n\r QXSLAB Joint\n\r\n\r"); |
benkatz | 20:bf9ea5125d52 | 659 | wait(.01); |
benkatz | 23:2adf23ee0305 | 660 | printf("\n\r Debug Info:\n\r"); |
benkatz | 32:ccac5da77844 | 661 | printf(" Firmware Version: %s\n\r", VERSION_NUM); |
benkatz | 23:2adf23ee0305 | 662 | printf(" ADC1 Offset: %d ADC2 Offset: %d\n\r", controller.adc1_offset, controller.adc2_offset); |
Rushu | 52:d4d5e3414865 | 663 | printf(" Position Sensor Electrical Offset: %.4f\n\r", E_OFFSET*57.2957795); |
Rushu | 52:d4d5e3414865 | 664 | printf(" Output Zero Position: %.4f\n\r", M_OFFSET*57.2957795); |
benkatz | 24:58c2d7571207 | 665 | printf(" CAN ID: %d\n\r", CAN_ID); |
Rushu | 49:7eac11914980 | 666 | |
Rushu | 49:7eac11914980 | 667 | controller.theta_joint_raw_pre=-(ma700.GetMechPosition()+spi.GetMechPosition()); //hjb added, for joint encoder filter |
benkatz | 23:2adf23ee0305 | 668 | pc.attach(&serial_interrupt); // attach serial interrupt |
Rushu | 49:7eac11914980 | 669 | J_M_flag = 0; |
shaorui | 48:1b51771c3647 | 670 | //state_change = 1; |
benkatz | 20:bf9ea5125d52 | 671 | |
benkatz | 22:60276ba87ac6 | 672 | |
shaorui | 48:1b51771c3647 | 673 | while(1) { |
Rushu | 49:7eac11914980 | 674 | // wait(.1); |
shaorui | 48:1b51771c3647 | 675 | if(state == MOTOR_MODE) |
shaorui | 48:1b51771c3647 | 676 | { |
shaorui | 55:d614e29c60c5 | 677 | |
shaorui | 55:d614e29c60c5 | 678 | |
shaorui | 55:d614e29c60c5 | 679 | |
Rushu | 49:7eac11914980 | 680 | if(controller.theta_joint_raw*57.2957795<=1) |
shaorui | 48:1b51771c3647 | 681 | { |
shaorui | 55:d614e29c60c5 | 682 | |
shaorui | 48:1b51771c3647 | 683 | controller.v_des = 0; |
shaorui | 48:1b51771c3647 | 684 | wait(1); |
shaorui | 48:1b51771c3647 | 685 | controller.p_des=0; |
shaorui | 55:d614e29c60c5 | 686 | controller.v_des = 1.5f; |
shaorui | 48:1b51771c3647 | 687 | controller.kp = 0; |
shaorui | 55:d614e29c60c5 | 688 | controller.kd = 5.0f; |
shaorui | 48:1b51771c3647 | 689 | controller.t_ff = 0; |
shaorui | 48:1b51771c3647 | 690 | wait(.5); |
shaorui | 48:1b51771c3647 | 691 | } |
shaorui | 55:d614e29c60c5 | 692 | |
Rushu | 49:7eac11914980 | 693 | else if(controller.theta_joint_raw*57.2957795>=(359)) |
shaorui | 48:1b51771c3647 | 694 | { |
shaorui | 48:1b51771c3647 | 695 | //stop_sign=1; |
shaorui | 48:1b51771c3647 | 696 | controller.v_des = 0; |
shaorui | 48:1b51771c3647 | 697 | wait(1); |
shaorui | 48:1b51771c3647 | 698 | controller.p_des=0; |
shaorui | 55:d614e29c60c5 | 699 | controller.v_des = -1.5f; |
shaorui | 48:1b51771c3647 | 700 | controller.kp = 0; |
shaorui | 55:d614e29c60c5 | 701 | controller.kd = 5.0f; |
shaorui | 48:1b51771c3647 | 702 | controller.t_ff = 0; |
shaorui | 48:1b51771c3647 | 703 | wait(.5); |
shaorui | 48:1b51771c3647 | 704 | printf("test position:%.3f\n\r",(1.0f/GR)* spi.GetMechPosition()); |
shaorui | 48:1b51771c3647 | 705 | |
shaorui | 55:d614e29c60c5 | 706 | } |
shaorui | 55:d614e29c60c5 | 707 | |
shaorui | 48:1b51771c3647 | 708 | |
shaorui | 55:d614e29c60c5 | 709 | if(count >= 40000){ |
shaorui | 55:d614e29c60c5 | 710 | printf("J_init: %.3f Pref: %.3f preal: %.3f JM: %.3d one: %.3f two: %.3f\n\r",Joint_init*57.2957795,(1.0f/GR)*spi.GetMechPosition()*57.2957795,controller.theta_mech*57.2957795,J_M_flag ,controller.Ncycle_mod*57.2957795-controller.Mech_mod*57.2957795/GR,(controller.theta_joint- controller.theta_joint_raw)*57.2957795); |
shaorui | 55:d614e29c60c5 | 711 | // printf("Pref: %.3f qian: %.3f hou: %.3f M: %.3d \n\r", (1.0f/GR)*spi.GetMechPosition()*57.2957795,controller.theta_joint_raw*57.2957795,controller.theta_mech*57.2957795,J_M_flag); |
shaorui | 55:d614e29c60c5 | 712 | // printf("v: %.3f v1: %.3f \n\r",controller.dtheta_mech*57.2957795,controller.dtheta_mech1*57.2957795); |
Rushu | 49:7eac11914980 | 713 | count = 0; |
Rushu | 49:7eac11914980 | 714 | } |
shaorui | 55:d614e29c60c5 | 715 | |
Rushu | 49:7eac11914980 | 716 | i++; |
shaorui | 48:1b51771c3647 | 717 | } |
shaorui | 55:d614e29c60c5 | 718 | } |
benkatz | 0:4e1c4df6aabd | 719 | } |