1

Dependencies:   mbed-dev-f303 FastPWM3

Committer:
Rushu
Date:
Thu Aug 06 07:25:51 2020 +0000
Revision:
51:29e1686e8b3e
Parent:
50:1fe5c2c53af1
Child:
52:d4d5e3414865
NPP49,2as5147,drv8323rh

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 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
Rushu 51:29e1686e8b3e 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 51:29e1686e8b3e 293 //if(controller.dtheta_mech>0) {controller.theta_joint_raw += PI/180.0f;} //compensate
Rushu 51:29e1686e8b3e 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 51:29e1686e8b3e 308 /*
Rushu 49:7eac11914980 309 if(abs(controller.Ncycle_mod*57.2957795-controller.Mech_mod*57.2957795/GR)>4) //not in the error range
Rushu 49:7eac11914980 310 {
Rushu 49:7eac11914980 311 if (controller.Mech_mod>PI) //
Rushu 49:7eac11914980 312 {
Rushu 49:7eac11914980 313 controller.Ncycle -= 1;
Rushu 49:7eac11914980 314 }
Rushu 49:7eac11914980 315 else if(controller.Mech_mod<PI) //
Rushu 49:7eac11914980 316 {
Rushu 49:7eac11914980 317 controller.Ncycle += 1;
Rushu 49:7eac11914980 318 }
Rushu 49:7eac11914980 319
Rushu 49:7eac11914980 320 }
Rushu 51:29e1686e8b3e 321 */
Rushu 49:7eac11914980 322 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 323 if(J_M_flag == 0)
Rushu 49:7eac11914980 324 {
Rushu 51:29e1686e8b3e 325 if(0<abs(controller.Ncycle_mod*57.2957795-controller.Mech_mod*57.2957795/GR)<2)
Rushu 51:29e1686e8b3e 326 {
Rushu 51:29e1686e8b3e 327 if(0<abs((controller.theta_joint- controller.theta_joint_raw)*57.2957795)<2)
Rushu 51:29e1686e8b3e 328 {
Rushu 51:29e1686e8b3e 329 Joint_init = controller.theta_joint- (1.0f/GR)*spi.GetMechPosition();
Rushu 51:29e1686e8b3e 330 controller.theta_mech = controller.theta_joint;////controller.theta_joint; // easy way to use, whether available in the shock?
Rushu 51:29e1686e8b3e 331 J_M_flag = 1;
Rushu 51:29e1686e8b3e 332 }
Rushu 51:29e1686e8b3e 333 }
Rushu 49:7eac11914980 334 controller.theta_mech = controller.theta_joint_raw;
Rushu 49:7eac11914980 335 }
Rushu 49:7eac11914980 336 else if(J_M_flag == 1)
Rushu 49:7eac11914980 337 {
Rushu 49:7eac11914980 338 controller.theta_mech = Joint_init + (1.0f/GR)*spi.GetMechPosition();
Rushu 49:7eac11914980 339 }
Rushu 49:7eac11914980 340
Rushu 49:7eac11914980 341
shaorui 48:1b51771c3647 342 /////shaorui end//////////////////
shaorui 48:1b51771c3647 343 /*
shaorui 48:1b51771c3647 344 controller.c++;
shaorui 48:1b51771c3647 345 if(controller.c>=20000)
shaorui 48:1b51771c3647 346 {
shaorui 48:1b51771c3647 347 controller.cha=controller.init2-controller.init1;
shaorui 48:1b51771c3647 348 controller.init1=controller.init2;
shaorui 48:1b51771c3647 349 controller.c=0;
shaorui 48:1b51771c3647 350 printf("position: %.3f \n\r", controller.cha*360/(2.0f*PI));
shaorui 48:1b51771c3647 351 }
shaorui 48:1b51771c3647 352 */
benkatz 23:2adf23ee0305 353 /// Check state machine state, and run the appropriate function ///
benkatz 23:2adf23ee0305 354 switch(state){
benkatz 37:c0f352d6e8e3 355 case REST_MODE: // Do nothing
benkatz 23:2adf23ee0305 356 if(state_change){
benkatz 23:2adf23ee0305 357 enter_menu_state();
shaorui 48:1b51771c3647 358 wucha=0 ; //shaorui add
benkatz 23:2adf23ee0305 359 }
benkatz 23:2adf23ee0305 360 break;
benkatz 22:60276ba87ac6 361
benkatz 23:2adf23ee0305 362 case CALIBRATION_MODE: // Run encoder calibration procedure
benkatz 23:2adf23ee0305 363 if(state_change){
benkatz 23:2adf23ee0305 364 calibrate();
benkatz 23:2adf23ee0305 365 }
benkatz 23:2adf23ee0305 366 break;
benkatz 23:2adf23ee0305 367
shaorui 48:1b51771c3647 368 case J_CALIBRATION_MODE: // Run encoder calibration procedure
shaorui 48:1b51771c3647 369 if(state_change){
shaorui 48:1b51771c3647 370 jocalibrate();
shaorui 48:1b51771c3647 371
shaorui 48:1b51771c3647 372 }
shaorui 48:1b51771c3647 373 break;
shaorui 48:1b51771c3647 374
Rushu 49:7eac11914980 375 case TEST_TRAJECTORY_MODE: // Run encoder calibration procedure
shaorui 48:1b51771c3647 376 if(state_change){
Rushu 49:7eac11914980 377 test_jointround_flag=1;
Rushu 49:7eac11914980 378 J_M_flag = 0;
shaorui 48:1b51771c3647 379 stop_sign=0;
shaorui 48:1b51771c3647 380 jointcalibrate();
shaorui 48:1b51771c3647 381
shaorui 48:1b51771c3647 382 }
shaorui 48:1b51771c3647 383 break;
shaorui 48:1b51771c3647 384
shaorui 48:1b51771c3647 385
benkatz 26:2b865c00d7e9 386 case MOTOR_MODE: // Run torque control
benkatz 25:f5741040c4bb 387 if(state_change){
benkatz 25:f5741040c4bb 388 enter_torque_mode();
benkatz 28:8c7e29f719c5 389 count = 0;
benkatz 25:f5741040c4bb 390 }
benkatz 28:8c7e29f719c5 391 else{
benkatz 37:c0f352d6e8e3 392 /*
benkatz 37:c0f352d6e8e3 393 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 394 gpio.enable->write(0);
benkatz 37:c0f352d6e8e3 395 controller.ovp_flag = 1;
benkatz 37:c0f352d6e8e3 396 state = REST_MODE;
benkatz 37:c0f352d6e8e3 397 state_change = 1;
benkatz 37:c0f352d6e8e3 398 printf("OVP Triggered!\n\r");
benkatz 37:c0f352d6e8e3 399 }
benkatz 37:c0f352d6e8e3 400 */
benkatz 37:c0f352d6e8e3 401
shaorui 48:1b51771c3647 402 torque_control(&controller);
shaorui 48:1b51771c3647 403 /*
benkatz 28:8c7e29f719c5 404 if((controller.timeout > CAN_TIMEOUT) && (CAN_TIMEOUT > 0)){
benkatz 28:8c7e29f719c5 405 controller.i_d_ref = 0;
benkatz 28:8c7e29f719c5 406 controller.i_q_ref = 0;
benkatz 37:c0f352d6e8e3 407 controller.kp = 0;
benkatz 37:c0f352d6e8e3 408 controller.kd = 0;
benkatz 37:c0f352d6e8e3 409 controller.t_ff = 0;
benkatz 28:8c7e29f719c5 410 }
shaorui 48:1b51771c3647 411 */
benkatz 37:c0f352d6e8e3 412 commutate(&controller, &observer, &gpio, controller.theta_elec); // Run current loop
benkatz 28:8c7e29f719c5 413 controller.timeout += 1;
benkatz 38:67e4e1453a4b 414
Rushu 49:7eac11914980 415
Rushu 49:7eac11914980 416 count++;
benkatz 39:3580a907ef93 417 /*
benkatz 40:cd7e837b2b93 418 if(count == 4000){
benkatz 40:cd7e837b2b93 419 printf("%.4f\n\r", controller.dtheta_mech);
benkatz 32:ccac5da77844 420 count = 0;
benkatz 23:2adf23ee0305 421 }
benkatz 39:3580a907ef93 422 */
benkatz 38:67e4e1453a4b 423
benkatz 37:c0f352d6e8e3 424
benkatz 37:c0f352d6e8e3 425 }
benkatz 23:2adf23ee0305 426 break;
benkatz 23:2adf23ee0305 427 case SETUP_MODE:
benkatz 23:2adf23ee0305 428 if(state_change){
benkatz 24:58c2d7571207 429 enter_setup_state();
benkatz 23:2adf23ee0305 430 }
benkatz 23:2adf23ee0305 431 break;
benkatz 23:2adf23ee0305 432 case ENCODER_MODE:
benkatz 23:2adf23ee0305 433 print_encoder();
benkatz 23:2adf23ee0305 434 break;
benkatz 37:c0f352d6e8e3 435 }
benkatz 2:8724412ad628 436 }
benkatz 23:2adf23ee0305 437 TIM1->SR = 0x0; // reset the status register
benkatz 2:8724412ad628 438 }
benkatz 0:4e1c4df6aabd 439
benkatz 25:f5741040c4bb 440
benkatz 24:58c2d7571207 441 char cmd_val[8] = {0};
benkatz 24:58c2d7571207 442 char cmd_id = 0;
benkatz 25:f5741040c4bb 443 char char_count = 0;
benkatz 24:58c2d7571207 444
benkatz 25:f5741040c4bb 445 /// Manage state machine with commands from serial terminal or configurator gui ///
benkatz 25:f5741040c4bb 446 /// Called when data received over serial ///
benkatz 23:2adf23ee0305 447 void serial_interrupt(void){
benkatz 23:2adf23ee0305 448 while(pc.readable()){
benkatz 23:2adf23ee0305 449 char c = pc.getc();
benkatz 25:f5741040c4bb 450 if(c == 27){
benkatz 25:f5741040c4bb 451 state = REST_MODE;
benkatz 25:f5741040c4bb 452 state_change = 1;
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);
Rushu 49:7eac11914980 501 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 502 /*
shaorui 48:1b51771c3647 503 for(int i=0;i<300;i++)
shaorui 48:1b51771c3647 504 {
shaorui 48:1b51771c3647 505 printf("%.3d %.3d\n\r",i,__int_reg[i] );
shaorui 48:1b51771c3647 506 }
shaorui 48:1b51771c3647 507 for(int j=0;j<64;j++)
shaorui 48:1b51771c3647 508 {
shaorui 48:1b51771c3647 509 printf("%.3d %.3f\n\r",j,__float_reg[j] );
shaorui 48:1b51771c3647 510 }
Rushu 49:7eac11914980 511 */
benkatz 37:c0f352d6e8e3 512 break;
benkatz 37:c0f352d6e8e3 513 }
benkatz 37:c0f352d6e8e3 514
benkatz 24:58c2d7571207 515 }
benkatz 24:58c2d7571207 516 else if(state == SETUP_MODE){
benkatz 25:f5741040c4bb 517 if(c == 13){
benkatz 24:58c2d7571207 518 switch (cmd_id){
benkatz 24:58c2d7571207 519 case 'b':
benkatz 24:58c2d7571207 520 I_BW = fmaxf(fminf(atof(cmd_val), 2000.0f), 100.0f);
benkatz 24:58c2d7571207 521 break;
benkatz 24:58c2d7571207 522 case 'i':
benkatz 24:58c2d7571207 523 CAN_ID = atoi(cmd_val);
benkatz 24:58c2d7571207 524 break;
benkatz 26:2b865c00d7e9 525 case 'm':
benkatz 26:2b865c00d7e9 526 CAN_MASTER = atoi(cmd_val);
benkatz 26:2b865c00d7e9 527 break;
benkatz 24:58c2d7571207 528 case 'l':
benkatz 24:58c2d7571207 529 TORQUE_LIMIT = fmaxf(fminf(atof(cmd_val), 18.0f), 0.0f);
benkatz 24:58c2d7571207 530 break;
benkatz 28:8c7e29f719c5 531 case 't':
benkatz 28:8c7e29f719c5 532 CAN_TIMEOUT = atoi(cmd_val);
benkatz 28:8c7e29f719c5 533 break;
benkatz 24:58c2d7571207 534 default:
benkatz 24:58c2d7571207 535 printf("\n\r '%c' Not a valid command prefix\n\r\n\r", cmd_id);
benkatz 24:58c2d7571207 536 break;
benkatz 24:58c2d7571207 537 }
benkatz 24:58c2d7571207 538
benkatz 24:58c2d7571207 539 if (!prefs.ready()) prefs.open();
benkatz 24:58c2d7571207 540 prefs.flush(); // Write new prefs to flash
benkatz 24:58c2d7571207 541 prefs.close();
benkatz 24:58c2d7571207 542 prefs.load();
benkatz 24:58c2d7571207 543 state_change = 1;
benkatz 24:58c2d7571207 544 char_count = 0;
benkatz 24:58c2d7571207 545 cmd_id = 0;
benkatz 24:58c2d7571207 546 for(int i = 0; i<8; i++){cmd_val[i] = 0;}
benkatz 24:58c2d7571207 547 }
benkatz 24:58c2d7571207 548 else{
benkatz 24:58c2d7571207 549 if(char_count == 0){cmd_id = c;}
benkatz 24:58c2d7571207 550 else{
benkatz 24:58c2d7571207 551 cmd_val[char_count-1] = c;
benkatz 24:58c2d7571207 552
benkatz 24:58c2d7571207 553 }
benkatz 24:58c2d7571207 554 pc.putc(c);
benkatz 24:58c2d7571207 555 char_count++;
benkatz 23:2adf23ee0305 556 }
benkatz 23:2adf23ee0305 557 }
benkatz 24:58c2d7571207 558 else if (state == ENCODER_MODE){
benkatz 24:58c2d7571207 559 switch (c){
benkatz 24:58c2d7571207 560 case 27:
benkatz 24:58c2d7571207 561 state = REST_MODE;
benkatz 24:58c2d7571207 562 state_change = 1;
benkatz 24:58c2d7571207 563 break;
benkatz 24:58c2d7571207 564 }
benkatz 24:58c2d7571207 565 }
benkatz 24:58c2d7571207 566
benkatz 24:58c2d7571207 567 }
benkatz 22:60276ba87ac6 568 }
benkatz 0:4e1c4df6aabd 569
benkatz 0:4e1c4df6aabd 570 int main() {
benkatz 45:aadebe074af6 571
benkatz 20:bf9ea5125d52 572 controller.v_bus = V_BUS;
benkatz 22:60276ba87ac6 573 controller.mode = 0;
shaorui 48:1b51771c3647 574 controller.sidebct1=0;
benkatz 23:2adf23ee0305 575 Init_All_HW(&gpio); // Setup PWM, ADC, GPIO
benkatz 20:bf9ea5125d52 576
benkatz 9:d7eb815cb057 577 wait(.1);
benkatz 26:2b865c00d7e9 578 gpio.enable->write(1);
benkatz 45:aadebe074af6 579 TIM1->CCR3 = PWM_ARR*(1.0f); // Write duty cycles
benkatz 45:aadebe074af6 580 TIM1->CCR2 = PWM_ARR*(1.0f);
benkatz 45:aadebe074af6 581 TIM1->CCR1 = PWM_ARR*(1.0f);
benkatz 23:2adf23ee0305 582 zero_current(&controller.adc1_offset, &controller.adc2_offset); // Measure current sensor zero-offset
benkatz 26:2b865c00d7e9 583 gpio.enable->write(0);
benkatz 23:2adf23ee0305 584 reset_foc(&controller); // Reset current controller
benkatz 26:2b865c00d7e9 585 TIM1->CR1 ^= TIM_CR1_UDIS;
benkatz 26:2b865c00d7e9 586 //TIM1->CR1 |= TIM_CR1_UDIS; //enable interrupt
benkatz 20:bf9ea5125d52 587
benkatz 20:bf9ea5125d52 588 wait(.1);
shaorui 48:1b51771c3647 589 NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 0); // commutation > communication
benkatz 43:dfb72608639c 590
benkatz 37:c0f352d6e8e3 591 NVIC_SetPriority(CAN1_RX0_IRQn, 3);
benkatz 26:2b865c00d7e9 592 can.filter(CAN_ID<<21, 0xFFE00004, CANStandard, 0);
benkatz 43:dfb72608639c 593
benkatz 28:8c7e29f719c5 594 txMsg.id = CAN_MASTER;
benkatz 28:8c7e29f719c5 595 txMsg.len = 6;
benkatz 26:2b865c00d7e9 596 rxMsg.len = 8;
benkatz 43:dfb72608639c 597 can.attach(&onMsgReceived); // attach 'CAN receive-complete' interrupt handler
benkatz 25:f5741040c4bb 598 prefs.load(); // Read flash
benkatz 37:c0f352d6e8e3 599 if(isnan(E_OFFSET)){E_OFFSET = 0.0f;}
benkatz 37:c0f352d6e8e3 600 if(isnan(M_OFFSET)){M_OFFSET = 0.0f;}
benkatz 25:f5741040c4bb 601 spi.SetElecOffset(E_OFFSET); // Set position sensor offset
benkatz 37:c0f352d6e8e3 602 spi.SetMechOffset(M_OFFSET);
Rushu 49:7eac11914980 603 if(isnan(JOINT_E_OFFSET)){JOINT_E_OFFSET = 0.0f;}
Rushu 49:7eac11914980 604 if(isnan(JOINT_M_OFFSET)){JOINT_M_OFFSET = 0.0f;}
Rushu 49:7eac11914980 605 ma700.SetElecOffset(JOINT_E_OFFSET); // Set position sensor offset
Rushu 49:7eac11914980 606 ma700.SetMechOffset(JOINT_M_OFFSET);
benkatz 23:2adf23ee0305 607 int lut[128] = {0};
Rushu 49:7eac11914980 608 int joint[128]={0}; //shaorui
benkatz 23:2adf23ee0305 609 memcpy(&lut, &ENCODER_LUT, sizeof(lut));
shaorui 48:1b51771c3647 610 spi.WriteLUT(lut);
shaorui 48:1b51771c3647 611 memcpy(&joint, &ENCODER_JOINT , sizeof(joint));
shaorui 48:1b51771c3647 612 spi.WriteLUT(joint);
Rushu 49:7eac11914980 613 pc.baud(460800);//pc.baud(921600); // set serial baud rate
benkatz 20:bf9ea5125d52 614 wait(.01);
Rushu 49:7eac11914980 615 pc.printf("\n\r\n\r QXSLAB Joint\n\r\n\r");
benkatz 20:bf9ea5125d52 616 wait(.01);
benkatz 23:2adf23ee0305 617 printf("\n\r Debug Info:\n\r");
benkatz 32:ccac5da77844 618 printf(" Firmware Version: %s\n\r", VERSION_NUM);
benkatz 23:2adf23ee0305 619 printf(" ADC1 Offset: %d ADC2 Offset: %d\n\r", controller.adc1_offset, controller.adc2_offset);
benkatz 23:2adf23ee0305 620 printf(" Position Sensor Electrical Offset: %.4f\n\r", E_OFFSET);
benkatz 37:c0f352d6e8e3 621 printf(" Output Zero Position: %.4f\n\r", M_OFFSET);
benkatz 24:58c2d7571207 622 printf(" CAN ID: %d\n\r", CAN_ID);
Rushu 49:7eac11914980 623
Rushu 49:7eac11914980 624 controller.theta_joint_raw_pre=-(ma700.GetMechPosition()+spi.GetMechPosition()); //hjb added, for joint encoder filter
benkatz 23:2adf23ee0305 625 pc.attach(&serial_interrupt); // attach serial interrupt
Rushu 49:7eac11914980 626 J_M_flag = 0;
shaorui 48:1b51771c3647 627 //state_change = 1;
benkatz 20:bf9ea5125d52 628
benkatz 22:60276ba87ac6 629
shaorui 48:1b51771c3647 630 while(1) {
Rushu 49:7eac11914980 631 // wait(.1);
shaorui 48:1b51771c3647 632 if(state == MOTOR_MODE)
shaorui 48:1b51771c3647 633 {
Rushu 49:7eac11914980 634 if(test_jointround_flag==1)
shaorui 48:1b51771c3647 635 {
Rushu 49:7eac11914980 636 if(controller.theta_joint_raw*57.2957795<=1)
shaorui 48:1b51771c3647 637 {
shaorui 48:1b51771c3647 638 //if(stop_sign==0)
shaorui 48:1b51771c3647 639 //{
shaorui 48:1b51771c3647 640 controller.v_des = 0;
shaorui 48:1b51771c3647 641 wait(1);
shaorui 48:1b51771c3647 642 controller.p_des=0;
shaorui 48:1b51771c3647 643 controller.v_des = 1.5f;
shaorui 48:1b51771c3647 644 controller.kp = 0;
shaorui 48:1b51771c3647 645 controller.kd = 5.0f;
shaorui 48:1b51771c3647 646 controller.t_ff = 0;
shaorui 48:1b51771c3647 647 wait(.5);
shaorui 48:1b51771c3647 648 // }
shaorui 48:1b51771c3647 649 /*
shaorui 48:1b51771c3647 650 else
shaorui 48:1b51771c3647 651 {
Rushu 49:7eac11914980 652 test_jointround_flag=0;
shaorui 48:1b51771c3647 653 controller.v_des =0;
shaorui 48:1b51771c3647 654
shaorui 48:1b51771c3647 655 }
shaorui 48:1b51771c3647 656 */
shaorui 48:1b51771c3647 657 }
Rushu 49:7eac11914980 658 else if(controller.theta_joint_raw*57.2957795>=(359))
shaorui 48:1b51771c3647 659 {
shaorui 48:1b51771c3647 660 //stop_sign=1;
shaorui 48:1b51771c3647 661 controller.v_des = 0;
shaorui 48:1b51771c3647 662 wait(1);
shaorui 48:1b51771c3647 663 controller.p_des=0;
shaorui 48:1b51771c3647 664 controller.v_des = -1.5f;
shaorui 48:1b51771c3647 665 controller.kp = 0;
shaorui 48:1b51771c3647 666 controller.kd = 5.0f;
shaorui 48:1b51771c3647 667 controller.t_ff = 0;
shaorui 48:1b51771c3647 668 wait(.5);
shaorui 48:1b51771c3647 669 printf("test position:%.3f\n\r",(1.0f/GR)* spi.GetMechPosition());
shaorui 48:1b51771c3647 670
shaorui 48:1b51771c3647 671 }
shaorui 48:1b51771c3647 672 }
Rushu 49:7eac11914980 673 //wait(.1);
shaorui 48:1b51771c3647 674 // printf("%.3f\n\r",(1.0f/GR)* spi.GetMechPosition());
Rushu 49:7eac11914980 675 // printf("%.3d, %.3d\n\r",test_jointround_flag, stop_sign);
shaorui 48:1b51771c3647 676
shaorui 48:1b51771c3647 677 //printf("BCT: %.3x zzz: %.3x etxy: %.3x \n\r",ma700.Gettest(),ma700.Gettest1(),ma700.Gettest2());
shaorui 48:1b51771c3647 678 // float joint_mech_position=-(controller.theta_mech*360/(2.0f*PI)*GR+controller.theta_mech1*360/(2.0f*PI));
shaorui 48:1b51771c3647 679 // wucha1=(controller.theta_mech-controller.theta_mech1)*360/(2.0f*PI);
shaorui 48:1b51771c3647 680 //wucha1=controller.theta_mech*360/(2.0f*PI)-joint_mech_position;
shaorui 48:1b51771c3647 681 //wucha+=abs(wucha1);
shaorui 48:1b51771c3647 682 //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 683 // 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 684 //printf("m_position: %.3f\n\r",controller.theta_mech*360/(2.0f*PI)*GR);
shaorui 48:1b51771c3647 685 //printf("j_position: %.3f\n\r",controller.theta_mech1*360/(2.0f*PI));
Rushu 49:7eac11914980 686 // float m_position=controller.theta_mech*57.2957795;
shaorui 48:1b51771c3647 687 // float j_position=-controller.theta_mech1*360/(2.0f*PI)-controller.theta_mech*360/(2.0f*PI)*GR;
Rushu 49:7eac11914980 688 // float j_position=-(controller.theta_mech1+controller.theta_mech)*2807.49319614;
shaorui 48:1b51771c3647 689 // float j_position=-controller.theta_mech1*57.2957795;
Rushu 49:7eac11914980 690 //printf("m:%.3f\n\r,j:%.3f\n\r",m_position,j_position);
Rushu 49:7eac11914980 691 if(count >= 4000){
Rushu 50:1fe5c2c53af1 692 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 693 // 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 694 // 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 695 count = 0;
Rushu 49:7eac11914980 696 }
Rushu 49:7eac11914980 697
shaorui 48:1b51771c3647 698
Rushu 49:7eac11914980 699 i++;
Rushu 49:7eac11914980 700 //wait(.5);
shaorui 48:1b51771c3647 701
shaorui 48:1b51771c3647 702 }
shaorui 48:1b51771c3647 703
shaorui 48:1b51771c3647 704
benkatz 0:4e1c4df6aabd 705 }
benkatz 0:4e1c4df6aabd 706 }