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