Motor calibration

Dependencies:   BioroboticsMotorControl mbed BioroboticsEMGFilter MODSERIAL

Committer:
MAHCSnijders
Date:
Wed Oct 31 09:30:19 2018 +0000
Revision:
1:9c75e4cca419
Parent:
0:61f4586742be
Child:
2:9d23d93d097f
Motor calibration with break and LEDs to check

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MAHCSnijders 0:61f4586742be 1 #include "mbed.h"
MAHCSnijders 0:61f4586742be 2
MAHCSnijders 0:61f4586742be 3 Motor motor1(D6, D7, D13, D12); // Defining motor pins (PWM, direction, encoder)
MAHCSnijders 0:61f4586742be 4 Motor motor2(D5, D4, D10, D11); // Defining motor pins (PWM, direction, encoder)
MAHCSnijders 1:9c75e4cca419 5 DigitalOut calibLED1(LED_BLUE); // LED to check if calibration motor 1 is done
MAHCSnijders 1:9c75e4cca419 6 DigitalOut calibLED2(LED_RED); // LED to check if calibration motor 2 is done
MAHCSnijders 0:61f4586742be 7
MAHCSnijders 1:9c75e4cca419 8 const float pid_period = 0.001; // PID sample period in seconds.
MAHCSnijders 1:9c75e4cca419 9 const double Kp = 10.0; // PID proportional
MAHCSnijders 1:9c75e4cca419 10 const double Ki = 0.1; // PID integral
MAHCSnijders 1:9c75e4cca419 11 const double Kd = 0.5; // PID differential
MAHCSnijders 0:61f4586742be 12
MAHCSnijders 1:9c75e4cca419 13 Ticker motor_calib; // Ticker for motor calibration
MAHCSnijders 0:61f4586742be 14
MAHCSnijders 0:61f4586742be 15 void Motor_Calibration()
MAHCSnijders 1:9c75e4cca419 16 {
MAHCSnijders 1:9c75e4cca419 17 if (motor_angle1 = motor1.get_current_angle)
MAHCSnijders 1:9c75e4cca419 18 {
MAHCSnijders 1:9c75e4cca419 19 motor1.set_current_angle_as_zero();
MAHCSnijders 1:9c75e4cca419 20 calibLED1 = 0;
MAHCSnijders 1:9c75e4cca419 21 break
MAHCSnijders 1:9c75e4cca419 22 }
MAHCSnijders 1:9c75e4cca419 23
MAHCSnijders 1:9c75e4cca419 24 if (motor_angle2 = motor2.get_current_angle)
MAHCSnijders 1:9c75e4cca419 25 {
MAHCSnijders 1:9c75e4cca419 26 motor2.set_current_angle_as_zero();
MAHCSnijders 1:9c75e4cca419 27 calibLED2 = 0;
MAHCSnijders 1:9c75e4cca419 28 break
MAHCSnijders 1:9c75e4cca419 29 }
MAHCSnijders 1:9c75e4cca419 30
MAHCSnijders 0:61f4586742be 31 float motor_angle1 = motor1.get_current_angle;
MAHCSnijders 0:61f4586742be 32 motor_angle1--;
MAHCSnijders 0:61f4586742be 33 motor1.set_target_angle(motor_angle1);
MAHCSnijders 0:61f4586742be 34
MAHCSnijders 0:61f4586742be 35 float motor_angle2 = motor2.get_current_angle;
MAHCSnijders 0:61f4586742be 36 motor_angle2--;
MAHCSnijders 0:61f4586742be 37 motor2.set_target_angle(motor_angle2);
MAHCSnijders 1:9c75e4cca419 38
MAHCSnijders 1:9c75e4cca419 39
MAHCSnijders 0:61f4586742be 40 }
MAHCSnijders 0:61f4586742be 41
MAHCSnijders 0:61f4586742be 42 int main()
MAHCSnijders 0:61f4586742be 43 {
MAHCSnijders 1:9c75e4cca419 44 calibLED1 = 1;
MAHCSnijders 1:9c75e4cca419 45 calibLED2 = 1;
MAHCSnijders 1:9c75e4cca419 46 motor1.set_pid_k_values(Kp, Ki, Kd); // Attach PID-controller values
MAHCSnijders 0:61f4586742be 47 motor2.set_pid_k_values(Kp, Ki, Kd);
MAHCSnijders 1:9c75e4cca419 48 motor1.start(pid_period) // Attach PID sample time
MAHCSnijders 0:61f4586742be 49 motor2.start(pid_period);
MAHCSnijders 1:9c75e4cca419 50 motor_calib.attach(Motor_Calibration,0.5); // Ticker for motor calibration fucntion
MAHCSnijders 0:61f4586742be 51 while (true) {} // Empty while loop to keep function from stopping
MAHCSnijders 0:61f4586742be 52 }