1

Dependencies:   mbed-dev-f303 FastPWM3

Committer:
shaorui
Date:
Fri Oct 23 09:37:45 2020 +0000
Revision:
54:e201ae25e467
Parent:
53:32218a36df05
Joint_control_2AS5147_DRV8323RH_GR49_HJB_2020_8_10

Who changed what in which revision?

UserRevisionLine numberNew 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;
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
shaorui 53:32218a36df05 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 52:d4d5e3414865 254 printf(" AS5147Mechanical Angle: %f Electrical Angle: %f Raw: %d\n\r", spi.GetMechPosition()*57.2957795, spi.GetElecPosition()*57.2957795, spi.GetRawPosition());
Rushu 52:d4d5e3414865 255 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 256 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 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++);
Rushu 52:d4d5e3414865 273 spi.Sample(DT);
Rushu 52:d4d5e3414865 274 ma700.Sample(DT);
Rushu 52:d4d5e3414865 275
benkatz 37:c0f352d6e8e3 276 controller.adc2_raw = ADC2->DR; // Read ADC Data Registers
benkatz 23:2adf23ee0305 277 controller.adc1_raw = ADC1->DR;
benkatz 37:c0f352d6e8e3 278 controller.adc3_raw = ADC3->DR;
Rushu 52:d4d5e3414865 279 // sample position sensor
benkatz 37:c0f352d6e8e3 280 controller.theta_elec = spi.GetElecPosition();
Rushu 49:7eac11914980 281 // controller.theta_mech = (1.0f/GR)*spi.GetMechPosition();
benkatz 37:c0f352d6e8e3 282 controller.dtheta_mech = (1.0f/GR)*spi.GetMechVelocity();
benkatz 37:c0f352d6e8e3 283 controller.dtheta_elec = spi.GetElecVelocity();
benkatz 37:c0f352d6e8e3 284 controller.v_bus = 0.95f*controller.v_bus + 0.05f*((float)controller.adc3_raw)*V_SCALE;
shaorui 48:1b51771c3647 285
benkatz 20:bf9ea5125d52 286
shaorui 48:1b51771c3647 287 //////shaorui add for obtaining joint real position
shaorui 48:1b51771c3647 288 controller.theta_elec1 = ma700.GetElecPosition();
Rushu 49:7eac11914980 289 controller.theta_mech1 = (1.0f/GR)*ma700.GetMechPosition();
Rushu 49:7eac11914980 290 controller.dtheta_mech1 =(1.0f/GR)*ma700.GetMechVelocity();
shaorui 48:1b51771c3647 291 controller.dtheta_elec1 = ma700.GetElecVelocity();
Rushu 49:7eac11914980 292
Rushu 49:7eac11914980 293 ///hjb add joint angle
Rushu 49:7eac11914980 294 controller.theta_joint_raw= -(ma700.GetMechPosition()+spi.GetMechPosition());
Rushu 51:29e1686e8b3e 295 //if(controller.dtheta_mech>0) {controller.theta_joint_raw += PI/180.0f;} //compensate
Rushu 51:29e1686e8b3e 296 // else if(controller.dtheta_mech<0) {controller.theta_joint_raw -= PI/180.0f;}
Rushu 49:7eac11914980 297 if(controller.theta_joint_raw<0){controller.theta_joint_raw += 2.0f*PI;}
Rushu 52:d4d5e3414865 298 else if(controller.theta_joint_raw>=2.0f*PI){controller.theta_joint_raw -= 2.0f*PI;}
Rushu 52:d4d5e3414865 299
Rushu 52:d4d5e3414865 300 if(controller.theta_joint_raw<0){controller.theta_joint_raw += 2.0f*PI;} //may be still below 0
Rushu 52:d4d5e3414865 301 else if(controller.theta_joint_raw>=2.0f*PI){controller.theta_joint_raw -= 2.0f*PI;}
Rushu 49:7eac11914980 302
Rushu 49:7eac11914980 303 controller.theta_joint_raw_fil =0.99f*controller.theta_joint_raw_pre + 0.01f*controller.theta_joint_raw ;//filter
Rushu 49:7eac11914980 304 controller.theta_joint_raw_pre =controller.theta_joint_raw_fil;
Rushu 49:7eac11914980 305
Rushu 49:7eac11914980 306 //"rad 0~2pi" ("deg" =-(controller.theta_mech1+controller.theta_mech)*2807.49319614;
Rushu 49:7eac11914980 307 //int Ncycle=0;
Rushu 49:7eac11914980 308 //float Ncycle_mod = 0.0f;
Rushu 49:7eac11914980 309 //float Mech_mod = 0.0f;
Rushu 49:7eac11914980 310 controller.Ncycle = controller.theta_joint_raw/(2.0f*PI/GR);
Rushu 49:7eac11914980 311 controller.Ncycle_mod = fmod(controller.theta_joint_raw,2.0f*PI/GR);
Rushu 49:7eac11914980 312 controller.Mech_mod = fmod(spi.GetMechPosition(),2.0f*PI);
Rushu 52:d4d5e3414865 313
Rushu 52:d4d5e3414865 314 if(controller.Mech_mod<0){controller.Mech_mod += 2.0f*PI;}
Rushu 52:d4d5e3414865 315 else if(controller.Mech_mod>=2.0f*PI){controller.Mech_mod -= 2.0f*PI;}
Rushu 52:d4d5e3414865 316
Rushu 49:7eac11914980 317 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 318 if(controller.theta_joint<0){controller.theta_joint += 2.0f*PI;}
Rushu 52:d4d5e3414865 319 else if(controller.theta_joint>=2.0f*PI){controller.theta_joint -= 2.0f*PI;}
Rushu 49:7eac11914980 320 if(J_M_flag == 0)
Rushu 49:7eac11914980 321 {
Rushu 51:29e1686e8b3e 322 if(0<abs(controller.Ncycle_mod*57.2957795-controller.Mech_mod*57.2957795/GR)<2)
Rushu 51:29e1686e8b3e 323 {
Rushu 51:29e1686e8b3e 324 if(0<abs((controller.theta_joint- controller.theta_joint_raw)*57.2957795)<2)
Rushu 51:29e1686e8b3e 325 {
Rushu 51:29e1686e8b3e 326 Joint_init = controller.theta_joint- (1.0f/GR)*spi.GetMechPosition();
Rushu 51:29e1686e8b3e 327 controller.theta_mech = controller.theta_joint;////controller.theta_joint; // easy way to use, whether available in the shock?
Rushu 51:29e1686e8b3e 328 J_M_flag = 1;
Rushu 51:29e1686e8b3e 329 }
Rushu 51:29e1686e8b3e 330 }
Rushu 49:7eac11914980 331 controller.theta_mech = controller.theta_joint_raw;
Rushu 49:7eac11914980 332 }
Rushu 49:7eac11914980 333 else if(J_M_flag == 1)
Rushu 49:7eac11914980 334 {
Rushu 49:7eac11914980 335 controller.theta_mech = Joint_init + (1.0f/GR)*spi.GetMechPosition();
shaorui 53:32218a36df05 336
Rushu 49:7eac11914980 337 }
Rushu 49:7eac11914980 338
Rushu 49:7eac11914980 339
shaorui 48:1b51771c3647 340 /////shaorui end//////////////////
shaorui 48:1b51771c3647 341 /*
shaorui 48:1b51771c3647 342 controller.c++;
shaorui 48:1b51771c3647 343 if(controller.c>=20000)
shaorui 48:1b51771c3647 344 {
shaorui 48:1b51771c3647 345 controller.cha=controller.init2-controller.init1;
shaorui 48:1b51771c3647 346 controller.init1=controller.init2;
shaorui 48:1b51771c3647 347 controller.c=0;
shaorui 48:1b51771c3647 348 printf("position: %.3f \n\r", controller.cha*360/(2.0f*PI));
shaorui 48:1b51771c3647 349 }
shaorui 48:1b51771c3647 350 */
benkatz 23:2adf23ee0305 351 /// Check state machine state, and run the appropriate function ///
benkatz 23:2adf23ee0305 352 switch(state){
benkatz 37:c0f352d6e8e3 353 case REST_MODE: // Do nothing
benkatz 23:2adf23ee0305 354 if(state_change){
benkatz 23:2adf23ee0305 355 enter_menu_state();
shaorui 48:1b51771c3647 356 wucha=0 ; //shaorui add
shaorui 53:32218a36df05 357 test_jointround_flag=0;
benkatz 23:2adf23ee0305 358 }
benkatz 23:2adf23ee0305 359 break;
benkatz 22:60276ba87ac6 360
benkatz 23:2adf23ee0305 361 case CALIBRATION_MODE: // Run encoder calibration procedure
benkatz 23:2adf23ee0305 362 if(state_change){
benkatz 23:2adf23ee0305 363 calibrate();
benkatz 23:2adf23ee0305 364 }
benkatz 23:2adf23ee0305 365 break;
benkatz 23:2adf23ee0305 366
shaorui 48:1b51771c3647 367 case J_CALIBRATION_MODE: // Run encoder calibration procedure
shaorui 48:1b51771c3647 368 if(state_change){
shaorui 48:1b51771c3647 369 jocalibrate();
shaorui 48:1b51771c3647 370
shaorui 48:1b51771c3647 371 }
shaorui 48:1b51771c3647 372 break;
shaorui 48:1b51771c3647 373
Rushu 49:7eac11914980 374 case TEST_TRAJECTORY_MODE: // Run encoder calibration procedure
shaorui 48:1b51771c3647 375 if(state_change){
Rushu 49:7eac11914980 376 test_jointround_flag=1;
Rushu 49:7eac11914980 377 J_M_flag = 0;
shaorui 48:1b51771c3647 378 stop_sign=0;
shaorui 48:1b51771c3647 379 jointcalibrate();
shaorui 48:1b51771c3647 380
shaorui 48:1b51771c3647 381 }
shaorui 48:1b51771c3647 382 break;
shaorui 48:1b51771c3647 383
shaorui 48:1b51771c3647 384
benkatz 26:2b865c00d7e9 385 case MOTOR_MODE: // Run torque control
benkatz 25:f5741040c4bb 386 if(state_change){
benkatz 25:f5741040c4bb 387 enter_torque_mode();
benkatz 28:8c7e29f719c5 388 count = 0;
benkatz 25:f5741040c4bb 389 }
benkatz 28:8c7e29f719c5 390 else{
benkatz 37:c0f352d6e8e3 391 /*
benkatz 37:c0f352d6e8e3 392 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 393 gpio.enable->write(0);
benkatz 37:c0f352d6e8e3 394 controller.ovp_flag = 1;
benkatz 37:c0f352d6e8e3 395 state = REST_MODE;
benkatz 37:c0f352d6e8e3 396 state_change = 1;
benkatz 37:c0f352d6e8e3 397 printf("OVP Triggered!\n\r");
benkatz 37:c0f352d6e8e3 398 }
benkatz 37:c0f352d6e8e3 399 */
benkatz 37:c0f352d6e8e3 400
shaorui 48:1b51771c3647 401 torque_control(&controller);
shaorui 48:1b51771c3647 402 /*
benkatz 28:8c7e29f719c5 403 if((controller.timeout > CAN_TIMEOUT) && (CAN_TIMEOUT > 0)){
benkatz 28:8c7e29f719c5 404 controller.i_d_ref = 0;
benkatz 28:8c7e29f719c5 405 controller.i_q_ref = 0;
benkatz 37:c0f352d6e8e3 406 controller.kp = 0;
benkatz 37:c0f352d6e8e3 407 controller.kd = 0;
benkatz 37:c0f352d6e8e3 408 controller.t_ff = 0;
benkatz 28:8c7e29f719c5 409 }
shaorui 48:1b51771c3647 410 */
benkatz 37:c0f352d6e8e3 411 commutate(&controller, &observer, &gpio, controller.theta_elec); // Run current loop
benkatz 28:8c7e29f719c5 412 controller.timeout += 1;
benkatz 38:67e4e1453a4b 413
Rushu 49:7eac11914980 414
Rushu 49:7eac11914980 415 count++;
benkatz 39:3580a907ef93 416 /*
benkatz 40:cd7e837b2b93 417 if(count == 4000){
benkatz 40:cd7e837b2b93 418 printf("%.4f\n\r", controller.dtheta_mech);
benkatz 32:ccac5da77844 419 count = 0;
benkatz 23:2adf23ee0305 420 }
benkatz 39:3580a907ef93 421 */
benkatz 38:67e4e1453a4b 422
benkatz 37:c0f352d6e8e3 423
benkatz 37:c0f352d6e8e3 424 }
benkatz 23:2adf23ee0305 425 break;
benkatz 23:2adf23ee0305 426 case SETUP_MODE:
benkatz 23:2adf23ee0305 427 if(state_change){
benkatz 24:58c2d7571207 428 enter_setup_state();
benkatz 23:2adf23ee0305 429 }
benkatz 23:2adf23ee0305 430 break;
benkatz 23:2adf23ee0305 431 case ENCODER_MODE:
benkatz 23:2adf23ee0305 432 print_encoder();
benkatz 23:2adf23ee0305 433 break;
benkatz 37:c0f352d6e8e3 434 }
benkatz 2:8724412ad628 435 }
benkatz 23:2adf23ee0305 436 TIM1->SR = 0x0; // reset the status register
benkatz 2:8724412ad628 437 }
benkatz 0:4e1c4df6aabd 438
benkatz 25:f5741040c4bb 439
benkatz 24:58c2d7571207 440 char cmd_val[8] = {0};
benkatz 24:58c2d7571207 441 char cmd_id = 0;
benkatz 25:f5741040c4bb 442 char char_count = 0;
benkatz 24:58c2d7571207 443
benkatz 25:f5741040c4bb 444 /// Manage state machine with commands from serial terminal or configurator gui ///
benkatz 25:f5741040c4bb 445 /// Called when data received over serial ///
benkatz 23:2adf23ee0305 446 void serial_interrupt(void){
benkatz 23:2adf23ee0305 447 while(pc.readable()){
benkatz 23:2adf23ee0305 448 char c = pc.getc();
benkatz 25:f5741040c4bb 449 if(c == 27){
benkatz 25:f5741040c4bb 450 state = REST_MODE;
benkatz 25:f5741040c4bb 451 state_change = 1;
shaorui 53:32218a36df05 452 test_jointround_flag=0;
benkatz 25:f5741040c4bb 453 char_count = 0;
benkatz 25:f5741040c4bb 454 cmd_id = 0;
benkatz 37:c0f352d6e8e3 455 gpio.led->write(0);;
benkatz 25:f5741040c4bb 456 for(int i = 0; i<8; i++){cmd_val[i] = 0;}
benkatz 25:f5741040c4bb 457 }
benkatz 24:58c2d7571207 458 if(state == REST_MODE){
benkatz 23:2adf23ee0305 459 switch (c){
benkatz 23:2adf23ee0305 460 case 'c':
benkatz 23:2adf23ee0305 461 state = CALIBRATION_MODE;
benkatz 23:2adf23ee0305 462 state_change = 1;
benkatz 23:2adf23ee0305 463 break;
shaorui 48:1b51771c3647 464
shaorui 48:1b51771c3647 465 case 't':
Rushu 49:7eac11914980 466 state = TEST_TRAJECTORY_MODE;
shaorui 48:1b51771c3647 467 state_change = 1;
shaorui 48:1b51771c3647 468 break;
shaorui 48:1b51771c3647 469 case 'j':
shaorui 48:1b51771c3647 470 state = J_CALIBRATION_MODE;
shaorui 48:1b51771c3647 471 state_change = 1;
shaorui 48:1b51771c3647 472 break;
shaorui 48:1b51771c3647 473
benkatz 26:2b865c00d7e9 474 case 'm':
benkatz 26:2b865c00d7e9 475 state = MOTOR_MODE;
benkatz 23:2adf23ee0305 476 state_change = 1;
benkatz 23:2adf23ee0305 477 break;
benkatz 23:2adf23ee0305 478 case 'e':
benkatz 23:2adf23ee0305 479 state = ENCODER_MODE;
benkatz 23:2adf23ee0305 480 state_change = 1;
benkatz 23:2adf23ee0305 481 break;
benkatz 23:2adf23ee0305 482 case 's':
benkatz 23:2adf23ee0305 483 state = SETUP_MODE;
benkatz 23:2adf23ee0305 484 state_change = 1;
benkatz 23:2adf23ee0305 485 break;
shaorui 48:1b51771c3647 486
benkatz 37:c0f352d6e8e3 487 case 'z':
benkatz 37:c0f352d6e8e3 488 spi.SetMechOffset(0);
shaorui 48:1b51771c3647 489 ma700.SetMechOffset(0);
benkatz 45:aadebe074af6 490 spi.Sample(DT);
shaorui 48:1b51771c3647 491 ma700.Sample(DT);
benkatz 37:c0f352d6e8e3 492 wait_us(20);
benkatz 37:c0f352d6e8e3 493 M_OFFSET = spi.GetMechPosition();
shaorui 48:1b51771c3647 494 JOINT_M_OFFSET = ma700.GetMechPosition();
benkatz 37:c0f352d6e8e3 495 if (!prefs.ready()) prefs.open();
benkatz 37:c0f352d6e8e3 496 prefs.flush(); // Write new prefs to flash
benkatz 37:c0f352d6e8e3 497 prefs.close();
benkatz 37:c0f352d6e8e3 498 prefs.load();
benkatz 37:c0f352d6e8e3 499 spi.SetMechOffset(M_OFFSET);
shaorui 48:1b51771c3647 500 ma700.SetMechOffset(JOINT_M_OFFSET);
shaorui 53:32218a36df05 501 J_M_flag=0;
Rushu 49:7eac11914980 502 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 503 /*
shaorui 48:1b51771c3647 504 for(int i=0;i<300;i++)
shaorui 48:1b51771c3647 505 {
shaorui 48:1b51771c3647 506 printf("%.3d %.3d\n\r",i,__int_reg[i] );
shaorui 48:1b51771c3647 507 }
shaorui 48:1b51771c3647 508 for(int j=0;j<64;j++)
shaorui 48:1b51771c3647 509 {
shaorui 48:1b51771c3647 510 printf("%.3d %.3f\n\r",j,__float_reg[j] );
shaorui 48:1b51771c3647 511 }
Rushu 49:7eac11914980 512 */
benkatz 37:c0f352d6e8e3 513 break;
benkatz 37:c0f352d6e8e3 514 }
benkatz 37:c0f352d6e8e3 515
benkatz 24:58c2d7571207 516 }
benkatz 24:58c2d7571207 517 else if(state == SETUP_MODE){
benkatz 25:f5741040c4bb 518 if(c == 13){
benkatz 24:58c2d7571207 519 switch (cmd_id){
benkatz 24:58c2d7571207 520 case 'b':
benkatz 24:58c2d7571207 521 I_BW = fmaxf(fminf(atof(cmd_val), 2000.0f), 100.0f);
benkatz 24:58c2d7571207 522 break;
benkatz 24:58c2d7571207 523 case 'i':
benkatz 24:58c2d7571207 524 CAN_ID = atoi(cmd_val);
benkatz 24:58c2d7571207 525 break;
benkatz 26:2b865c00d7e9 526 case 'm':
benkatz 26:2b865c00d7e9 527 CAN_MASTER = atoi(cmd_val);
benkatz 26:2b865c00d7e9 528 break;
benkatz 24:58c2d7571207 529 case 'l':
benkatz 24:58c2d7571207 530 TORQUE_LIMIT = fmaxf(fminf(atof(cmd_val), 18.0f), 0.0f);
benkatz 24:58c2d7571207 531 break;
benkatz 28:8c7e29f719c5 532 case 't':
benkatz 28:8c7e29f719c5 533 CAN_TIMEOUT = atoi(cmd_val);
benkatz 28:8c7e29f719c5 534 break;
benkatz 24:58c2d7571207 535 default:
benkatz 24:58c2d7571207 536 printf("\n\r '%c' Not a valid command prefix\n\r\n\r", cmd_id);
benkatz 24:58c2d7571207 537 break;
benkatz 24:58c2d7571207 538 }
benkatz 24:58c2d7571207 539
benkatz 24:58c2d7571207 540 if (!prefs.ready()) prefs.open();
benkatz 24:58c2d7571207 541 prefs.flush(); // Write new prefs to flash
benkatz 24:58c2d7571207 542 prefs.close();
benkatz 24:58c2d7571207 543 prefs.load();
benkatz 24:58c2d7571207 544 state_change = 1;
benkatz 24:58c2d7571207 545 char_count = 0;
benkatz 24:58c2d7571207 546 cmd_id = 0;
benkatz 24:58c2d7571207 547 for(int i = 0; i<8; i++){cmd_val[i] = 0;}
benkatz 24:58c2d7571207 548 }
benkatz 24:58c2d7571207 549 else{
benkatz 24:58c2d7571207 550 if(char_count == 0){cmd_id = c;}
benkatz 24:58c2d7571207 551 else{
benkatz 24:58c2d7571207 552 cmd_val[char_count-1] = c;
benkatz 24:58c2d7571207 553
benkatz 24:58c2d7571207 554 }
benkatz 24:58c2d7571207 555 pc.putc(c);
benkatz 24:58c2d7571207 556 char_count++;
benkatz 23:2adf23ee0305 557 }
benkatz 23:2adf23ee0305 558 }
benkatz 24:58c2d7571207 559 else if (state == ENCODER_MODE){
benkatz 24:58c2d7571207 560 switch (c){
benkatz 24:58c2d7571207 561 case 27:
benkatz 24:58c2d7571207 562 state = REST_MODE;
benkatz 24:58c2d7571207 563 state_change = 1;
benkatz 24:58c2d7571207 564 break;
benkatz 24:58c2d7571207 565 }
benkatz 24:58c2d7571207 566 }
benkatz 24:58c2d7571207 567
benkatz 24:58c2d7571207 568 }
benkatz 22:60276ba87ac6 569 }
benkatz 0:4e1c4df6aabd 570
benkatz 0:4e1c4df6aabd 571 int main() {
benkatz 45:aadebe074af6 572
benkatz 20:bf9ea5125d52 573 controller.v_bus = V_BUS;
benkatz 22:60276ba87ac6 574 controller.mode = 0;
shaorui 48:1b51771c3647 575 controller.sidebct1=0;
benkatz 23:2adf23ee0305 576 Init_All_HW(&gpio); // Setup PWM, ADC, GPIO
benkatz 20:bf9ea5125d52 577
benkatz 9:d7eb815cb057 578 wait(.1);
benkatz 26:2b865c00d7e9 579 gpio.enable->write(1);
benkatz 45:aadebe074af6 580 TIM1->CCR3 = PWM_ARR*(1.0f); // Write duty cycles
benkatz 45:aadebe074af6 581 TIM1->CCR2 = PWM_ARR*(1.0f);
benkatz 45:aadebe074af6 582 TIM1->CCR1 = PWM_ARR*(1.0f);
benkatz 23:2adf23ee0305 583 zero_current(&controller.adc1_offset, &controller.adc2_offset); // Measure current sensor zero-offset
benkatz 26:2b865c00d7e9 584 gpio.enable->write(0);
benkatz 23:2adf23ee0305 585 reset_foc(&controller); // Reset current controller
benkatz 26:2b865c00d7e9 586 TIM1->CR1 ^= TIM_CR1_UDIS;
benkatz 26:2b865c00d7e9 587 //TIM1->CR1 |= TIM_CR1_UDIS; //enable interrupt
benkatz 20:bf9ea5125d52 588
benkatz 20:bf9ea5125d52 589 wait(.1);
shaorui 48:1b51771c3647 590 NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 0); // commutation > communication
benkatz 43:dfb72608639c 591
benkatz 37:c0f352d6e8e3 592 NVIC_SetPriority(CAN1_RX0_IRQn, 3);
Rushu 52:d4d5e3414865 593 // If preferences haven't been user configured yet, set defaults
Rushu 52:d4d5e3414865 594 prefs.load(); // Read flash
Rushu 52:d4d5e3414865 595 can.filter(CAN_ID , 0xFFF, CANStandard, 0);
benkatz 28:8c7e29f719c5 596 txMsg.id = CAN_MASTER;
benkatz 28:8c7e29f719c5 597 txMsg.len = 6;
benkatz 26:2b865c00d7e9 598 rxMsg.len = 8;
Rushu 52:d4d5e3414865 599 can.attach(&onMsgReceived);
Rushu 52:d4d5e3414865 600
benkatz 37:c0f352d6e8e3 601 if(isnan(E_OFFSET)){E_OFFSET = 0.0f;}
benkatz 37:c0f352d6e8e3 602 if(isnan(M_OFFSET)){M_OFFSET = 0.0f;}
benkatz 25:f5741040c4bb 603 spi.SetElecOffset(E_OFFSET); // Set position sensor offset
benkatz 37:c0f352d6e8e3 604 spi.SetMechOffset(M_OFFSET);
Rushu 52:d4d5e3414865 605
shaorui 53:32218a36df05 606 //pc.baud(460800);//pc.baud(921600);
shaorui 53:32218a36df05 607 pc.baud(115000);
Rushu 52:d4d5e3414865 608 //=====benk 2020 last version===//
Rushu 52:d4d5e3414865 609
Rushu 52:d4d5e3414865 610 spi.Sample(1.0f);
Rushu 52:d4d5e3414865 611 printf("MP: %.3f\n\r",spi.GetMechPosition());
Rushu 52:d4d5e3414865 612 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 613 else if (spi.GetMechPosition() < 0){spi.SetMechOffset(M_OFFSET-2.0f*PI);}
Rushu 52:d4d5e3414865 614 //=====benk 2020 last version===//
Rushu 52:d4d5e3414865 615
Rushu 49:7eac11914980 616 if(isnan(JOINT_E_OFFSET)){JOINT_E_OFFSET = 0.0f;}
Rushu 49:7eac11914980 617 if(isnan(JOINT_M_OFFSET)){JOINT_M_OFFSET = 0.0f;}
Rushu 49:7eac11914980 618 ma700.SetElecOffset(JOINT_E_OFFSET); // Set position sensor offset
Rushu 49:7eac11914980 619 ma700.SetMechOffset(JOINT_M_OFFSET);
Rushu 52:d4d5e3414865 620 //=====benk 2020 last version===//
Rushu 52:d4d5e3414865 621 ma700.Sample(1.0f);
Rushu 52:d4d5e3414865 622 printf("JP: %.3f\n\r",ma700.GetMechPosition());
Rushu 52:d4d5e3414865 623 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 624 else if (ma700.GetMechPosition() < 0){ma700.SetMechOffset(JOINT_M_OFFSET-2.0f*PI);}
Rushu 52:d4d5e3414865 625
Rushu 52:d4d5e3414865 626 spi.Sample(1.0f);
Rushu 52:d4d5e3414865 627 ma700.Sample(1.0f);
Rushu 52:d4d5e3414865 628 printf("MP: %.3f JP: %.3f\n\r",spi.GetMechPosition(),ma700.GetMechPosition());
Rushu 52:d4d5e3414865 629
Rushu 52:d4d5e3414865 630 //=====benk 2020 last version===//
benkatz 23:2adf23ee0305 631 int lut[128] = {0};
Rushu 49:7eac11914980 632 int joint[128]={0}; //shaorui
benkatz 23:2adf23ee0305 633 memcpy(&lut, &ENCODER_LUT, sizeof(lut));
shaorui 48:1b51771c3647 634 spi.WriteLUT(lut);
shaorui 48:1b51771c3647 635 memcpy(&joint, &ENCODER_JOINT , sizeof(joint));
shaorui 53:32218a36df05 636 ma700.WriteLUT(joint);
Rushu 52:d4d5e3414865 637 // set serial baud rate
benkatz 20:bf9ea5125d52 638 wait(.01);
Rushu 52:d4d5e3414865 639 printf("MP: %.3f JP: %.3f\n\r",spi.GetMechPosition(),ma700.GetMechPosition());
Rushu 52:d4d5e3414865 640
Rushu 49:7eac11914980 641 pc.printf("\n\r\n\r QXSLAB Joint\n\r\n\r");
benkatz 20:bf9ea5125d52 642 wait(.01);
benkatz 23:2adf23ee0305 643 printf("\n\r Debug Info:\n\r");
benkatz 32:ccac5da77844 644 printf(" Firmware Version: %s\n\r", VERSION_NUM);
benkatz 23:2adf23ee0305 645 printf(" ADC1 Offset: %d ADC2 Offset: %d\n\r", controller.adc1_offset, controller.adc2_offset);
Rushu 52:d4d5e3414865 646 printf(" Position Sensor Electrical Offset: %.4f\n\r", E_OFFSET*57.2957795);
Rushu 52:d4d5e3414865 647 printf(" Output Zero Position: %.4f\n\r", M_OFFSET*57.2957795);
benkatz 24:58c2d7571207 648 printf(" CAN ID: %d\n\r", CAN_ID);
Rushu 49:7eac11914980 649
Rushu 49:7eac11914980 650 controller.theta_joint_raw_pre=-(ma700.GetMechPosition()+spi.GetMechPosition()); //hjb added, for joint encoder filter
benkatz 23:2adf23ee0305 651 pc.attach(&serial_interrupt); // attach serial interrupt
Rushu 49:7eac11914980 652 J_M_flag = 0;
shaorui 48:1b51771c3647 653 //state_change = 1;
benkatz 20:bf9ea5125d52 654
benkatz 22:60276ba87ac6 655
shaorui 48:1b51771c3647 656 while(1) {
Rushu 49:7eac11914980 657 // wait(.1);
shaorui 48:1b51771c3647 658 if(state == MOTOR_MODE)
shaorui 48:1b51771c3647 659 {
Rushu 49:7eac11914980 660 if(test_jointround_flag==1)
shaorui 48:1b51771c3647 661 {
Rushu 49:7eac11914980 662 if(controller.theta_joint_raw*57.2957795<=1)
shaorui 48:1b51771c3647 663 {
shaorui 48:1b51771c3647 664 //if(stop_sign==0)
shaorui 48:1b51771c3647 665 //{
shaorui 48:1b51771c3647 666 controller.v_des = 0;
shaorui 48:1b51771c3647 667 wait(1);
shaorui 48:1b51771c3647 668 controller.p_des=0;
shaorui 53:32218a36df05 669 controller.v_des = 1.0f;
shaorui 48:1b51771c3647 670 controller.kp = 0;
shaorui 54:e201ae25e467 671 controller.kd = 4.0f;
shaorui 48:1b51771c3647 672 controller.t_ff = 0;
shaorui 48:1b51771c3647 673 wait(.5);
shaorui 48:1b51771c3647 674 // }
shaorui 48:1b51771c3647 675 /*
shaorui 48:1b51771c3647 676 else
shaorui 48:1b51771c3647 677 {
Rushu 49:7eac11914980 678 test_jointround_flag=0;
shaorui 48:1b51771c3647 679 controller.v_des =0;
shaorui 48:1b51771c3647 680
shaorui 48:1b51771c3647 681 }
shaorui 48:1b51771c3647 682 */
shaorui 48:1b51771c3647 683 }
Rushu 49:7eac11914980 684 else if(controller.theta_joint_raw*57.2957795>=(359))
shaorui 48:1b51771c3647 685 {
shaorui 48:1b51771c3647 686 //stop_sign=1;
shaorui 48:1b51771c3647 687 controller.v_des = 0;
shaorui 48:1b51771c3647 688 wait(1);
shaorui 48:1b51771c3647 689 controller.p_des=0;
shaorui 53:32218a36df05 690 controller.v_des = -1.0f;
shaorui 48:1b51771c3647 691 controller.kp = 0;
shaorui 54:e201ae25e467 692 controller.kd = 4.0f;
shaorui 48:1b51771c3647 693 controller.t_ff = 0;
shaorui 48:1b51771c3647 694 wait(.5);
shaorui 48:1b51771c3647 695 printf("test position:%.3f\n\r",(1.0f/GR)* spi.GetMechPosition());
shaorui 48:1b51771c3647 696
shaorui 48:1b51771c3647 697 }
shaorui 48:1b51771c3647 698 }
Rushu 49:7eac11914980 699 //wait(.1);
shaorui 48:1b51771c3647 700 // printf("%.3f\n\r",(1.0f/GR)* spi.GetMechPosition());
Rushu 49:7eac11914980 701 // printf("%.3d, %.3d\n\r",test_jointround_flag, stop_sign);
shaorui 48:1b51771c3647 702
shaorui 48:1b51771c3647 703 //printf("BCT: %.3x zzz: %.3x etxy: %.3x \n\r",ma700.Gettest(),ma700.Gettest1(),ma700.Gettest2());
shaorui 48:1b51771c3647 704 // float joint_mech_position=-(controller.theta_mech*360/(2.0f*PI)*GR+controller.theta_mech1*360/(2.0f*PI));
shaorui 48:1b51771c3647 705 // wucha1=(controller.theta_mech-controller.theta_mech1)*360/(2.0f*PI);
shaorui 48:1b51771c3647 706 //wucha1=controller.theta_mech*360/(2.0f*PI)-joint_mech_position;
shaorui 48:1b51771c3647 707 //wucha+=abs(wucha1);
shaorui 48:1b51771c3647 708 //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 709 // 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 710 //printf("m_position: %.3f\n\r",controller.theta_mech*360/(2.0f*PI)*GR);
shaorui 48:1b51771c3647 711 //printf("j_position: %.3f\n\r",controller.theta_mech1*360/(2.0f*PI));
Rushu 49:7eac11914980 712 // float m_position=controller.theta_mech*57.2957795;
shaorui 48:1b51771c3647 713 // float j_position=-controller.theta_mech1*360/(2.0f*PI)-controller.theta_mech*360/(2.0f*PI)*GR;
Rushu 49:7eac11914980 714 // float j_position=-(controller.theta_mech1+controller.theta_mech)*2807.49319614;
shaorui 48:1b51771c3647 715 // float j_position=-controller.theta_mech1*57.2957795;
Rushu 49:7eac11914980 716 //printf("m:%.3f\n\r,j:%.3f\n\r",m_position,j_position);
shaorui 54:e201ae25e467 717 if(count >= 400){
Rushu 52:d4d5e3414865 718 // 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 52:d4d5e3414865 719 printf("Jraw:%.3f J: %.3f Mec: %.3f N: %.3d Nmod: %.3f Mmod: %.3f \n\r",controller.theta_joint_raw*57.2957795,controller.theta_joint*57.2957795, controller.theta_mech*57.2957795, controller.Ncycle,controller.Ncycle_mod*57.2957795,controller.Mech_mod*57.2957795);
Rushu 52:d4d5e3414865 720
shaorui 53:32218a36df05 721 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);
shaorui 53:32218a36df05 722 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 723 count = 0;
Rushu 49:7eac11914980 724 }
Rushu 49:7eac11914980 725
shaorui 48:1b51771c3647 726
Rushu 49:7eac11914980 727 i++;
Rushu 49:7eac11914980 728 //wait(.5);
shaorui 48:1b51771c3647 729
shaorui 48:1b51771c3647 730 }
shaorui 48:1b51771c3647 731
shaorui 48:1b51771c3647 732
benkatz 0:4e1c4df6aabd 733 }
benkatz 0:4e1c4df6aabd 734 }