Tobias Vollenweider
/
PM2_Example_PES_board
Works. 2
main.cpp@10:c5d85e35758c, 2021-04-07 (annotated)
- Committer:
- pmic
- Date:
- Wed Apr 07 12:13:50 2021 +0000
- Revision:
- 10:c5d85e35758c
- Parent:
- 9:f10b974d01e0
- Child:
- 11:3b145ff0ddbd
Last commit before first workshop 2.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pmic | 1:93d997d6b232 | 1 | #include "mbed.h" |
pmic | 1:93d997d6b232 | 2 | #include "platform/mbed_thread.h" |
pmic | 6:e1fa1a2d7483 | 3 | |
pmic | 6:e1fa1a2d7483 | 4 | /* PM2_Libary */ |
pmic | 1:93d997d6b232 | 5 | #include "EncoderCounter.h" |
pmic | 1:93d997d6b232 | 6 | #include "Servo.h" |
pmic | 3:d22942631cd7 | 7 | #include "SpeedController.h" |
pmic | 9:f10b974d01e0 | 8 | #include "FastPWM.h" |
pmic | 6:e1fa1a2d7483 | 9 | |
pmic | 6:e1fa1a2d7483 | 10 | using namespace std::chrono; |
pmic | 6:e1fa1a2d7483 | 11 | |
pmic | 8:9bb806a7f585 | 12 | InterruptIn user_button(USER_BUTTON); |
pmic | 8:9bb806a7f585 | 13 | DigitalOut led(LED1); |
pmic | 6:e1fa1a2d7483 | 14 | |
pmic | 6:e1fa1a2d7483 | 15 | bool executeMainTask = false; |
pmic | 6:e1fa1a2d7483 | 16 | Timer user_button_timer, loop_timer; |
pmic | 7:c0f5bb355f41 | 17 | int Ts_ms = 50; |
pmic | 6:e1fa1a2d7483 | 18 | |
pmic | 6:e1fa1a2d7483 | 19 | /* declaration of custom button functions */ |
pmic | 6:e1fa1a2d7483 | 20 | void button_fall(); |
pmic | 6:e1fa1a2d7483 | 21 | void button_rise(); |
pmic | 6:e1fa1a2d7483 | 22 | |
pmic | 6:e1fa1a2d7483 | 23 | /* create analog input object */ |
pmic | 6:e1fa1a2d7483 | 24 | AnalogIn analogIn(PC_2); |
pmic | 6:e1fa1a2d7483 | 25 | float dist = 0.0f; |
pmic | 6:e1fa1a2d7483 | 26 | |
pmic | 6:e1fa1a2d7483 | 27 | /* create enable dc motor digital out object */ |
pmic | 6:e1fa1a2d7483 | 28 | DigitalOut enable_motors(PB_15); |
pmic | 10:c5d85e35758c | 29 | /* create pwm objects */ |
pmic | 10:c5d85e35758c | 30 | FastPWM pwmOut_M1(PB_13); |
pmic | 10:c5d85e35758c | 31 | FastPWM pwmOut_M2(PA_9); |
pmic | 10:c5d85e35758c | 32 | FastPWM pwmOut_M3(PA_10); |
pmic | 10:c5d85e35758c | 33 | double Ts_pwm_s = 0.00005; // this needs to be a double value (no f at the end) |
pmic | 6:e1fa1a2d7483 | 34 | /* create encoder read objects */ |
pmic | 10:c5d85e35758c | 35 | EncoderCounter encoderCounter_M1(PA_6, PC_7); |
pmic | 10:c5d85e35758c | 36 | EncoderCounter encoderCounter_M2(PB_6, PB_7); |
pmic | 10:c5d85e35758c | 37 | EncoderCounter encoderCounter_M3(PA_0, PA_1); |
pmic | 10:c5d85e35758c | 38 | /* create speed controller objects, only M1 and M2, M3 is used open-loop */ |
pmic | 10:c5d85e35758c | 39 | float counts_per_turn = 20.0f*78.125f; // counts/turn * gearratio |
pmic | 10:c5d85e35758c | 40 | float kn = 180.0f/12.0f; // (RPM/V) |
pmic | 10:c5d85e35758c | 41 | float max_voltage = 12.0f; // adjust this to 6.0f if only one batterypack is used |
pmic | 10:c5d85e35758c | 42 | SpeedController speedController_M1(counts_per_turn, kn, max_voltage, pwmOut_M1, encoderCounter_M1); |
pmic | 10:c5d85e35758c | 43 | SpeedController speedController_M2(counts_per_turn, kn, max_voltage, pwmOut_M2, encoderCounter_M2); |
pmic | 6:e1fa1a2d7483 | 44 | |
pmic | 6:e1fa1a2d7483 | 45 | /* create servo objects */ |
pmic | 10:c5d85e35758c | 46 | Servo servo_S1(PB_2); |
pmic | 10:c5d85e35758c | 47 | Servo servo_S2(PC_8); |
pmic | 10:c5d85e35758c | 48 | // Servo servo_S3(PC_6); // not needed in this example |
pmic | 10:c5d85e35758c | 49 | int servoPeriod_mus = 20000; |
pmic | 10:c5d85e35758c | 50 | int servoOutput_mus_S1 = 0; |
pmic | 10:c5d85e35758c | 51 | int servoOutput_mus_S2 = 0; |
pmic | 10:c5d85e35758c | 52 | int servo_counter = 0; |
pmic | 10:c5d85e35758c | 53 | int loops_per_second = static_cast<int>(ceilf(1.0f/(0.001f*(float)Ts_ms))); |
pmic | 1:93d997d6b232 | 54 | |
pmic | 1:93d997d6b232 | 55 | int main() |
pmic | 9:f10b974d01e0 | 56 | { |
pmic | 6:e1fa1a2d7483 | 57 | user_button.fall(&button_fall); |
pmic | 6:e1fa1a2d7483 | 58 | user_button.rise(&button_rise); |
pmic | 6:e1fa1a2d7483 | 59 | loop_timer.start(); |
pmic | 6:e1fa1a2d7483 | 60 | |
pmic | 10:c5d85e35758c | 61 | /* enable hardwaredriver dc motors */ |
pmic | 10:c5d85e35758c | 62 | enable_motors = 1; |
pmic | 10:c5d85e35758c | 63 | /* initialize pwm for motor M3*/ |
pmic | 10:c5d85e35758c | 64 | pwmOut_M3.period(Ts_pwm_s); |
pmic | 6:e1fa1a2d7483 | 65 | /* set pwm output zero at the beginning, range: 0...1 -> u_min...u_max */ |
pmic | 10:c5d85e35758c | 66 | pwmOut_M3.write(0.5); |
pmic | 9:f10b974d01e0 | 67 | |
pmic | 10:c5d85e35758c | 68 | /* enable servos, you can also disable them */ |
pmic | 10:c5d85e35758c | 69 | servo_S1.Enable(servoOutput_mus_S1, servoPeriod_mus); |
pmic | 10:c5d85e35758c | 70 | servo_S2.Enable(servoOutput_mus_S2, servoPeriod_mus); |
pmic | 6:e1fa1a2d7483 | 71 | |
pmic | 1:93d997d6b232 | 72 | while (true) { |
pmic | 6:e1fa1a2d7483 | 73 | |
pmic | 6:e1fa1a2d7483 | 74 | loop_timer.reset(); |
pmic | 6:e1fa1a2d7483 | 75 | |
pmic | 6:e1fa1a2d7483 | 76 | /* ------------- start hacking ------------- -------------*/ |
pmic | 6:e1fa1a2d7483 | 77 | |
pmic | 6:e1fa1a2d7483 | 78 | if (executeMainTask) { |
pmic | 6:e1fa1a2d7483 | 79 | |
pmic | 6:e1fa1a2d7483 | 80 | /* read analog input */ |
pmic | 6:e1fa1a2d7483 | 81 | dist = analogIn.read() * 3.3f; |
pmic | 6:e1fa1a2d7483 | 82 | |
pmic | 10:c5d85e35758c | 83 | /* command a speed to dc motors M1 and M2*/ |
pmic | 10:c5d85e35758c | 84 | speedController_M1.setDesiredSpeedRPS( 1.0f); |
pmic | 10:c5d85e35758c | 85 | speedController_M2.setDesiredSpeedRPS(-0.5f); |
pmic | 10:c5d85e35758c | 86 | /* write output voltage to motor M3 */ |
pmic | 10:c5d85e35758c | 87 | pwmOut_M3.write(0.75); |
pmic | 6:e1fa1a2d7483 | 88 | |
pmic | 10:c5d85e35758c | 89 | /* command servo position via output time, this needs to be calibrated */ |
pmic | 10:c5d85e35758c | 90 | servo_S1.SetPosition(servoOutput_mus_S1); |
pmic | 10:c5d85e35758c | 91 | servo_S2.SetPosition(servoOutput_mus_S2); |
pmic | 10:c5d85e35758c | 92 | if (servoOutput_mus_S1 <= servoPeriod_mus & servo_counter%loops_per_second == 0 & servo_counter != 0) { |
pmic | 10:c5d85e35758c | 93 | servoOutput_mus_S1 += 100; |
pmic | 8:9bb806a7f585 | 94 | } |
pmic | 10:c5d85e35758c | 95 | if (servoOutput_mus_S2 <= servoPeriod_mus & servo_counter%loops_per_second == 0 & servo_counter != 0) { |
pmic | 10:c5d85e35758c | 96 | servoOutput_mus_S2 += 100; |
pmic | 8:9bb806a7f585 | 97 | } |
pmic | 10:c5d85e35758c | 98 | servo_counter++; |
pmic | 6:e1fa1a2d7483 | 99 | |
pmic | 6:e1fa1a2d7483 | 100 | /* visual feedback that the main task is executed */ |
pmic | 6:e1fa1a2d7483 | 101 | led = !led; |
pmic | 9:f10b974d01e0 | 102 | |
pmic | 1:93d997d6b232 | 103 | } else { |
pmic | 6:e1fa1a2d7483 | 104 | |
pmic | 6:e1fa1a2d7483 | 105 | dist = 0.0f; |
pmic | 1:93d997d6b232 | 106 | |
pmic | 10:c5d85e35758c | 107 | speedController_M1.setDesiredSpeedRPS(0.0f); |
pmic | 10:c5d85e35758c | 108 | speedController_M2.setDesiredSpeedRPS(0.0f); |
pmic | 10:c5d85e35758c | 109 | pwmOut_M3.write(0.5); |
pmic | 6:e1fa1a2d7483 | 110 | |
pmic | 10:c5d85e35758c | 111 | servoOutput_mus_S1 = 0; |
pmic | 10:c5d85e35758c | 112 | servoOutput_mus_S2 = 0; |
pmic | 10:c5d85e35758c | 113 | servo_S1.SetPosition(servoOutput_mus_S1); |
pmic | 10:c5d85e35758c | 114 | servo_S2.SetPosition(servoOutput_mus_S2); |
pmic | 6:e1fa1a2d7483 | 115 | |
pmic | 6:e1fa1a2d7483 | 116 | led = 0; |
pmic | 1:93d997d6b232 | 117 | } |
pmic | 6:e1fa1a2d7483 | 118 | |
pmic | 10:c5d85e35758c | 119 | /* do only output via serial what's really necessary (this makes your code slow)*/ |
pmic | 10:c5d85e35758c | 120 | printf("%3.3f, %3d, %3d, %3d, %3.3f, %3.3f;\r\n", |
pmic | 10:c5d85e35758c | 121 | dist, |
pmic | 10:c5d85e35758c | 122 | servoOutput_mus_S1, |
pmic | 10:c5d85e35758c | 123 | servoOutput_mus_S2, |
pmic | 10:c5d85e35758c | 124 | encoderCounter_M3.read(), |
pmic | 10:c5d85e35758c | 125 | speedController_M1.getSpeedRPS(), |
pmic | 10:c5d85e35758c | 126 | speedController_M2.getSpeedRPS()); |
pmic | 8:9bb806a7f585 | 127 | |
pmic | 6:e1fa1a2d7483 | 128 | /* ------------- stop hacking ------------- -------------*/ |
pmic | 6:e1fa1a2d7483 | 129 | |
pmic | 6:e1fa1a2d7483 | 130 | int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count(); |
pmic | 6:e1fa1a2d7483 | 131 | int dT_loop_ms = Ts_ms - T_loop_ms; |
pmic | 6:e1fa1a2d7483 | 132 | thread_sleep_for(dT_loop_ms); |
pmic | 1:93d997d6b232 | 133 | } |
pmic | 1:93d997d6b232 | 134 | } |
pmic | 6:e1fa1a2d7483 | 135 | |
pmic | 6:e1fa1a2d7483 | 136 | void button_fall() |
pmic | 6:e1fa1a2d7483 | 137 | { |
pmic | 6:e1fa1a2d7483 | 138 | user_button_timer.reset(); |
pmic | 6:e1fa1a2d7483 | 139 | user_button_timer.start(); |
pmic | 6:e1fa1a2d7483 | 140 | } |
pmic | 6:e1fa1a2d7483 | 141 | |
pmic | 6:e1fa1a2d7483 | 142 | void button_rise() |
pmic | 6:e1fa1a2d7483 | 143 | { |
pmic | 6:e1fa1a2d7483 | 144 | int t_button_ms = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 6:e1fa1a2d7483 | 145 | user_button_timer.stop(); |
pmic | 8:9bb806a7f585 | 146 | if (t_button_ms > 200) { |
pmic | 6:e1fa1a2d7483 | 147 | executeMainTask = !executeMainTask; |
pmic | 8:9bb806a7f585 | 148 | } |
pmic | 6:e1fa1a2d7483 | 149 | } |