added melody to old motorcontol

Committer:
kubitz
Date:
Sun Mar 08 10:52:52 2020 +0000
Revision:
14:0146695df1a5
Parent:
13:58ada64bb664
Child:
15:2c9b3251c383
working threads

Who changed what in which revision?

UserRevisionLine numberNew contents of line
estott 0:de4320f74764 1 #include "mbed.h"
kubitz 13:58ada64bb664 2 #include "rtos.h"
estott 0:de4320f74764 3 //Photointerrupter input pins
estott 10:a4b5723b6c9d 4 #define I1pin D3
estott 10:a4b5723b6c9d 5 #define I2pin D6
estott 10:a4b5723b6c9d 6 #define I3pin D5
estott 2:4e88faab6988 7
estott 2:4e88faab6988 8 //Incremental encoder input pins
estott 10:a4b5723b6c9d 9 #define CHApin D12
estott 10:a4b5723b6c9d 10 #define CHBpin D11
estott 0:de4320f74764 11
estott 0:de4320f74764 12 //Motor Drive output pins //Mask in output byte
estott 10:a4b5723b6c9d 13 #define L1Lpin D1 //0x01
estott 10:a4b5723b6c9d 14 #define L1Hpin A3 //0x02
estott 10:a4b5723b6c9d 15 #define L2Lpin D0 //0x04
estott 10:a4b5723b6c9d 16 #define L2Hpin A6 //0x08
estott 10:a4b5723b6c9d 17 #define L3Lpin D10 //0x10
estott 10:a4b5723b6c9d 18 #define L3Hpin D2 //0x20
estott 10:a4b5723b6c9d 19
estott 10:a4b5723b6c9d 20 #define PWMpin D9
estott 5:08f338b5e4d9 21
estott 5:08f338b5e4d9 22 //Motor current sense
estott 5:08f338b5e4d9 23 #define MCSPpin A1
estott 5:08f338b5e4d9 24 #define MCSNpin A0
estott 0:de4320f74764 25
estott 11:5ff18183764a 26 //Test outputs
estott 11:5ff18183764a 27 #define TP0pin D4
estott 11:5ff18183764a 28 #define TP1pin D13
estott 11:5ff18183764a 29 #define TP2pin A2
estott 11:5ff18183764a 30
kubitz 14:0146695df1a5 31 Serial pc(SERIAL_TX, SERIAL_RX);
kubitz 14:0146695df1a5 32
kubitz 14:0146695df1a5 33
kubitz 14:0146695df1a5 34 // motor controller variables
kubitz 14:0146695df1a5 35 float position_local = 0;
kubitz 14:0146695df1a5 36 int position_old = 0;
kubitz 14:0146695df1a5 37 float velocity = 0;
kubitz 14:0146695df1a5 38 Timer timer_velocity;
kubitz 14:0146695df1a5 39 uint32_t last_time_MtrCtlr;
kubitz 14:0146695df1a5 40 float pos_error_old;
kubitz 14:0146695df1a5 41 int i = 0;
estott 0:de4320f74764 42 //Mapping from sequential drive states to motor phase outputs
estott 0:de4320f74764 43 /*
estott 0:de4320f74764 44 State L1 L2 L3
estott 0:de4320f74764 45 0 H - L
estott 0:de4320f74764 46 1 - H L
estott 0:de4320f74764 47 2 L H -
estott 0:de4320f74764 48 3 L - H
estott 0:de4320f74764 49 4 - L H
estott 0:de4320f74764 50 5 H L -
estott 0:de4320f74764 51 6 - - -
estott 0:de4320f74764 52 7 - - -
estott 0:de4320f74764 53 */
estott 0:de4320f74764 54 //Drive state to output table
estott 0:de4320f74764 55 const int8_t driveTable[] = {0x12,0x18,0x09,0x21,0x24,0x06,0x00,0x00};
estott 2:4e88faab6988 56
estott 0:de4320f74764 57 //Mapping from interrupter inputs to sequential rotor states. 0x00 and 0x07 are not valid
kubitz 13:58ada64bb664 58 const int8_t stateMap[] = {0x07,0x05,0x03,0x04,0x01,0x00,0x02,0x07};
estott 2:4e88faab6988 59 //const int8_t stateMap[] = {0x07,0x01,0x03,0x02,0x05,0x00,0x04,0x07}; //Alternative if phase order of input or drive is reversed
estott 2:4e88faab6988 60
kubitz 13:58ada64bb664 61 // PARAMETERS //
kubitz 13:58ada64bb664 62 const float kp_rot = 0.08;
kubitz 13:58ada64bb664 63
kubitz 13:58ada64bb664 64 const float kp_pos = 0.05;
kubitz 13:58ada64bb664 65 const float kd_pos = 0;
kubitz 13:58ada64bb664 66 const float pwm_period =0.25f;
kubitz 13:58ada64bb664 67
kubitz 13:58ada64bb664 68 int v_ideal = 30;
kubitz 13:58ada64bb664 69 float pos_goal = 500;
kubitz 13:58ada64bb664 70
kubitz 13:58ada64bb664 71 // SHARED GLOBAL VARIABLES //
kubitz 13:58ada64bb664 72 Semaphore pos_semaphore(0);
kubitz 13:58ada64bb664 73 int position = 0;
kubitz 13:58ada64bb664 74 bool direction = 1;
kubitz 13:58ada64bb664 75
kubitz 13:58ada64bb664 76 int lead = 2;
kubitz 13:58ada64bb664 77
kubitz 13:58ada64bb664 78 // NON-SHARED GLOBAL VARIABLES //
kubitz 13:58ada64bb664 79 int lead_old;
estott 0:de4320f74764 80
estott 0:de4320f74764 81 //Status LED
estott 0:de4320f74764 82 DigitalOut led1(LED1);
estott 0:de4320f74764 83
estott 0:de4320f74764 84 //Photointerrupter inputs
kubitz 13:58ada64bb664 85 InterruptIn I1(I1pin);
kubitz 13:58ada64bb664 86 InterruptIn I2(I2pin);
kubitz 13:58ada64bb664 87 InterruptIn I3(I3pin);
estott 0:de4320f74764 88
estott 0:de4320f74764 89 //Motor Drive outputs
estott 0:de4320f74764 90 DigitalOut L1L(L1Lpin);
estott 0:de4320f74764 91 DigitalOut L1H(L1Hpin);
estott 0:de4320f74764 92 DigitalOut L2L(L2Lpin);
estott 0:de4320f74764 93 DigitalOut L2H(L2Hpin);
estott 0:de4320f74764 94 DigitalOut L3L(L3Lpin);
estott 0:de4320f74764 95 DigitalOut L3H(L3Hpin);
estott 0:de4320f74764 96
estott 11:5ff18183764a 97 DigitalOut TP1(TP1pin);
estott 11:5ff18183764a 98 PwmOut MotorPWM(PWMpin);
estott 11:5ff18183764a 99
kubitz 13:58ada64bb664 100 Ticker motorCtrlTicker;
kubitz 14:0146695df1a5 101 Thread thread_motorCtrl (osPriorityNormal,1024);
kubitz 13:58ada64bb664 102
kubitz 13:58ada64bb664 103 volatile int8_t orState = 0; //Rotot offset at motor state 0
kubitz 13:58ada64bb664 104 volatile int8_t intState = 0;
kubitz 13:58ada64bb664 105 volatile int8_t intStateOld = 0;
kubitz 13:58ada64bb664 106
kubitz 13:58ada64bb664 107
estott 0:de4320f74764 108 //Set a given drive state
kubitz 13:58ada64bb664 109 void motorOut(int8_t driveState)
kubitz 13:58ada64bb664 110 {
kubitz 13:58ada64bb664 111
estott 2:4e88faab6988 112 //Lookup the output byte from the drive state.
estott 2:4e88faab6988 113 int8_t driveOut = driveTable[driveState & 0x07];
kubitz 13:58ada64bb664 114
estott 2:4e88faab6988 115 //Turn off first
estott 2:4e88faab6988 116 if (~driveOut & 0x01) L1L = 0;
estott 2:4e88faab6988 117 if (~driveOut & 0x02) L1H = 1;
estott 2:4e88faab6988 118 if (~driveOut & 0x04) L2L = 0;
estott 2:4e88faab6988 119 if (~driveOut & 0x08) L2H = 1;
estott 2:4e88faab6988 120 if (~driveOut & 0x10) L3L = 0;
estott 2:4e88faab6988 121 if (~driveOut & 0x20) L3H = 1;
kubitz 13:58ada64bb664 122
estott 2:4e88faab6988 123 //Then turn on
estott 2:4e88faab6988 124 if (driveOut & 0x01) L1L = 1;
estott 2:4e88faab6988 125 if (driveOut & 0x02) L1H = 0;
estott 2:4e88faab6988 126 if (driveOut & 0x04) L2L = 1;
estott 2:4e88faab6988 127 if (driveOut & 0x08) L2H = 0;
estott 2:4e88faab6988 128 if (driveOut & 0x10) L3L = 1;
estott 2:4e88faab6988 129 if (driveOut & 0x20) L3H = 0;
kubitz 13:58ada64bb664 130 }
kubitz 13:58ada64bb664 131
kubitz 13:58ada64bb664 132 //Convert photointerrupter inputs to a rotor state
kubitz 13:58ada64bb664 133 inline int8_t readRotorState()
kubitz 13:58ada64bb664 134 {
kubitz 13:58ada64bb664 135 return stateMap[I1 + 2*I2 + 4*I3];
kubitz 13:58ada64bb664 136 }
kubitz 13:58ada64bb664 137
kubitz 13:58ada64bb664 138
kubitz 13:58ada64bb664 139 void move()
kubitz 13:58ada64bb664 140 {
kubitz 13:58ada64bb664 141 intState = readRotorState();
kubitz 13:58ada64bb664 142
kubitz 13:58ada64bb664 143 // Updates direction only if statechanges by 1
kubitz 13:58ada64bb664 144 // If state chance is missed then interrupt is unchanged
kubitz 13:58ada64bb664 145 if( intState == 0 && intStateOld == 5) {
kubitz 13:58ada64bb664 146 direction = 1;
kubitz 13:58ada64bb664 147 position++;
kubitz 13:58ada64bb664 148 } else if( intState == 5 && intStateOld == 0) {
kubitz 13:58ada64bb664 149 direction = 0;
kubitz 13:58ada64bb664 150 position--;
kubitz 13:58ada64bb664 151 } else if ( intState == intStateOld + 1 ) {
kubitz 13:58ada64bb664 152 direction = 1;
kubitz 13:58ada64bb664 153 position++;
kubitz 13:58ada64bb664 154 } else if (intState == intStateOld - 1) {
kubitz 13:58ada64bb664 155 direction = 0;
kubitz 13:58ada64bb664 156 position--;
estott 0:de4320f74764 157 }
kubitz 13:58ada64bb664 158
kubitz 13:58ada64bb664 159 pos_semaphore.release();
kubitz 13:58ada64bb664 160
kubitz 13:58ada64bb664 161 intStateOld = intState;
kubitz 13:58ada64bb664 162
kubitz 13:58ada64bb664 163 motorOut((intState-orState+lead+6)%6); //+6 to make sure the remainder is positive
estott 0:de4320f74764 164 }
estott 0:de4320f74764 165
kubitz 13:58ada64bb664 166
kubitz 13:58ada64bb664 167 void motorCtrlTick()
kubitz 13:58ada64bb664 168 {
kubitz 14:0146695df1a5 169 thread_motorCtrl.signal_set(0x1);
kubitz 14:0146695df1a5 170 //osSignalSet(thread_motorCtrl, 0x1);
kubitz 13:58ada64bb664 171 }
kubitz 13:58ada64bb664 172
kubitz 13:58ada64bb664 173 float get_y_rot(float rot_error)
kubitz 13:58ada64bb664 174 {
kubitz 13:58ada64bb664 175 return kp_rot*(rot_error);
kubitz 13:58ada64bb664 176 }
kubitz 13:58ada64bb664 177
kubitz 13:58ada64bb664 178 float get_y_pos(float e, float change_e)
kubitz 13:58ada64bb664 179 {
kubitz 13:58ada64bb664 180 return kp_pos*e + kd_pos*change_e;
kubitz 13:58ada64bb664 181 }
kubitz 13:58ada64bb664 182
kubitz 13:58ada64bb664 183 float get_motor_out(float y)
kubitz 13:58ada64bb664 184 {
kubitz 13:58ada64bb664 185 float motor_out;
kubitz 13:58ada64bb664 186 if( y < 0) {
kubitz 13:58ada64bb664 187 motor_out = y*-1;
kubitz 13:58ada64bb664 188 } else {
kubitz 13:58ada64bb664 189 motor_out = y;
kubitz 13:58ada64bb664 190 }
kubitz 13:58ada64bb664 191 if (y > 1 || y < -1) {
kubitz 13:58ada64bb664 192 motor_out = 1;
kubitz 13:58ada64bb664 193 }
kubitz 13:58ada64bb664 194 return motor_out;
estott 0:de4320f74764 195 }
kubitz 13:58ada64bb664 196
kubitz 13:58ada64bb664 197 int get_position_local(){
kubitz 13:58ada64bb664 198 pos_semaphore.wait();
kubitz 13:58ada64bb664 199 return position;
kubitz 13:58ada64bb664 200 }
kubitz 13:58ada64bb664 201
kubitz 13:58ada64bb664 202 void attach_ISR(){
kubitz 13:58ada64bb664 203 I1.rise(&move);
kubitz 13:58ada64bb664 204 I1.fall(&move);
kubitz 13:58ada64bb664 205 I2.rise(&move);
kubitz 13:58ada64bb664 206 I2.fall(&move);
kubitz 13:58ada64bb664 207 I3.rise(&move);
kubitz 13:58ada64bb664 208 I3.fall(&move);
kubitz 13:58ada64bb664 209 }
kubitz 13:58ada64bb664 210
kubitz 13:58ada64bb664 211
kubitz 13:58ada64bb664 212 float combine_speed(float y_pos, float y_rot, float error_rot)
kubitz 13:58ada64bb664 213 {
kubitz 13:58ada64bb664 214 if(error_rot >= 0) {
kubitz 13:58ada64bb664 215 if(y_pos < y_rot) {
kubitz 13:58ada64bb664 216 return y_pos;
kubitz 13:58ada64bb664 217 } else {
kubitz 13:58ada64bb664 218 return y_rot;
kubitz 13:58ada64bb664 219 }
kubitz 13:58ada64bb664 220 }
kubitz 13:58ada64bb664 221 else {
kubitz 13:58ada64bb664 222 if(y_pos > y_rot){
kubitz 13:58ada64bb664 223 return y_pos;
kubitz 13:58ada64bb664 224 }
kubitz 13:58ada64bb664 225 else{
kubitz 13:58ada64bb664 226 return y_rot;
estott 0:de4320f74764 227 }
estott 2:4e88faab6988 228 }
kubitz 13:58ada64bb664 229 return y_pos;
kubitz 13:58ada64bb664 230 }
kubitz 13:58ada64bb664 231
kubitz 13:58ada64bb664 232
kubitz 13:58ada64bb664 233 void update_lead(float y){
kubitz 13:58ada64bb664 234 // No functionality for breaking
kubitz 13:58ada64bb664 235 if(y >= 0){
kubitz 13:58ada64bb664 236 lead = 2;
kubitz 13:58ada64bb664 237 }
kubitz 13:58ada64bb664 238 else {
kubitz 13:58ada64bb664 239 lead = -2;
kubitz 13:58ada64bb664 240 }
kubitz 13:58ada64bb664 241
estott 0:de4320f74764 242 }
kubitz 14:0146695df1a5 243 void motorInitSequence()
kubitz 13:58ada64bb664 244 {
kubitz 13:58ada64bb664 245 motorCtrlTicker.attach_us(&motorCtrlTick,100000);
kubitz 14:0146695df1a5 246 pos_error_old = pos_goal;
kubitz 13:58ada64bb664 247
kubitz 13:58ada64bb664 248 MotorPWM.write(1);
kubitz 13:58ada64bb664 249 MotorPWM.period(pwm_period);
kubitz 13:58ada64bb664 250
kubitz 13:58ada64bb664 251 motorOut(0);
kubitz 13:58ada64bb664 252 wait(3.0);
kubitz 13:58ada64bb664 253 orState = readRotorState();
kubitz 13:58ada64bb664 254
kubitz 13:58ada64bb664 255 if(v_ideal > 0){
kubitz 13:58ada64bb664 256 lead = 2;
kubitz 13:58ada64bb664 257 motorOut(1);
kubitz 13:58ada64bb664 258 }
kubitz 13:58ada64bb664 259 else{
kubitz 13:58ada64bb664 260 lead = -2;
kubitz 13:58ada64bb664 261 motorOut(5);
kubitz 13:58ada64bb664 262 }
kubitz 13:58ada64bb664 263 attach_ISR();
kubitz 13:58ada64bb664 264
kubitz 13:58ada64bb664 265 position_local = get_position_local();
kubitz 13:58ada64bb664 266 position_old = position_local;
kubitz 13:58ada64bb664 267
kubitz 13:58ada64bb664 268
kubitz 14:0146695df1a5 269 }
kubitz 14:0146695df1a5 270
kubitz 14:0146695df1a5 271 void motorCtrlFn()
kubitz 14:0146695df1a5 272 {
kubitz 13:58ada64bb664 273 while(1) {
kubitz 13:58ada64bb664 274 //motorCtrlT.signal_wait(0x1);
kubitz 13:58ada64bb664 275 osSignalWait(0x1, osWaitForever);
kubitz 13:58ada64bb664 276
kubitz 13:58ada64bb664 277 position_local = get_position_local();
kubitz 13:58ada64bb664 278
kubitz 13:58ada64bb664 279
kubitz 14:0146695df1a5 280 velocity = ((position_local - position_old)/6)*10;
kubitz 13:58ada64bb664 281 position_old = position_local;
kubitz 13:58ada64bb664 282
kubitz 13:58ada64bb664 283 float error_rot = v_ideal - velocity;
kubitz 13:58ada64bb664 284 float y_rot = get_y_rot(error_rot);
kubitz 13:58ada64bb664 285 float motor_out = get_motor_out(y_rot);
kubitz 13:58ada64bb664 286
kubitz 13:58ada64bb664 287 update_lead(y_rot);
kubitz 13:58ada64bb664 288
kubitz 13:58ada64bb664 289 MotorPWM.period(pwm_period);
kubitz 13:58ada64bb664 290 MotorPWM.write(motor_out);
kubitz 13:58ada64bb664 291
kubitz 13:58ada64bb664 292 if(i > 10) {
kubitz 14:0146695df1a5 293 pc.printf("Velocity = %f, Position = %f, MotorOut = %f, y = %f, lead = %d\r\n", velocity, position_local, motor_out, y_rot, lead);
kubitz 13:58ada64bb664 294 i = 0;
kubitz 13:58ada64bb664 295 }
kubitz 13:58ada64bb664 296 i++;
kubitz 13:58ada64bb664 297 }
kubitz 13:58ada64bb664 298 }
kubitz 13:58ada64bb664 299
kubitz 13:58ada64bb664 300
kubitz 13:58ada64bb664 301 //Main
kubitz 13:58ada64bb664 302 int main()
kubitz 13:58ada64bb664 303 {
kubitz 14:0146695df1a5 304 motorInitSequence();
kubitz 14:0146695df1a5 305 thread_motorCtrl.start(motorCtrlFn);
kubitz 14:0146695df1a5 306
kubitz 13:58ada64bb664 307 }
kubitz 13:58ada64bb664 308