Projekt code für Hfecter
Dependencies: PM2_Libary PM2_Example_PES_board
main.cpp@12:b87a1e165fb8, 2021-04-19 (annotated)
- Committer:
- jashaalm
- Date:
- Mon Apr 19 09:37:44 2021 +0000
- Revision:
- 12:b87a1e165fb8
- Parent:
- 11:5053ce50a993
- Child:
- 13:35f55464048e
First Commit;
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" |
jashaalm | 12:b87a1e165fb8 | 9 | #define AUFLOESUNG 0.25412f |
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 */ |
jashaalm | 12:b87a1e165fb8 | 24 | AnalogIn adc_vbat(ADC_VBAT); |
pmic | 6:e1fa1a2d7483 | 25 | AnalogIn analogIn(PC_2); |
pmic | 6:e1fa1a2d7483 | 26 | float dist = 0.0f; |
jashaalm | 12:b87a1e165fb8 | 27 | float batt = 0.0f; |
pmic | 6:e1fa1a2d7483 | 28 | |
pmic | 6:e1fa1a2d7483 | 29 | /* create enable dc motor digital out object */ |
pmic | 6:e1fa1a2d7483 | 30 | DigitalOut enable_motors(PB_15); |
pmic | 10:c5d85e35758c | 31 | /* create pwm objects */ |
pmic | 10:c5d85e35758c | 32 | FastPWM pwmOut_M1(PB_13); |
pmic | 10:c5d85e35758c | 33 | FastPWM pwmOut_M2(PA_9); |
pmic | 10:c5d85e35758c | 34 | FastPWM pwmOut_M3(PA_10); |
pmic | 10:c5d85e35758c | 35 | double Ts_pwm_s = 0.00005; // this needs to be a double value (no f at the end) |
pmic | 6:e1fa1a2d7483 | 36 | /* create encoder read objects */ |
pmic | 10:c5d85e35758c | 37 | EncoderCounter encoderCounter_M1(PA_6, PC_7); |
pmic | 10:c5d85e35758c | 38 | EncoderCounter encoderCounter_M2(PB_6, PB_7); |
pmic | 10:c5d85e35758c | 39 | EncoderCounter encoderCounter_M3(PA_0, PA_1); |
pmic | 10:c5d85e35758c | 40 | /* create speed controller objects, only M1 and M2, M3 is used open-loop */ |
pmic | 10:c5d85e35758c | 41 | float counts_per_turn = 20.0f*78.125f; // counts/turn * gearratio |
pmic | 10:c5d85e35758c | 42 | float kn = 180.0f/12.0f; // (RPM/V) |
pmic | 10:c5d85e35758c | 43 | float max_voltage = 12.0f; // adjust this to 6.0f if only one batterypack is used |
pmic | 10:c5d85e35758c | 44 | SpeedController speedController_M1(counts_per_turn, kn, max_voltage, pwmOut_M1, encoderCounter_M1); |
pmic | 10:c5d85e35758c | 45 | SpeedController speedController_M2(counts_per_turn, kn, max_voltage, pwmOut_M2, encoderCounter_M2); |
pmic | 6:e1fa1a2d7483 | 46 | |
jashaalm | 12:b87a1e165fb8 | 47 | |
jashaalm | 12:b87a1e165fb8 | 48 | |
pmic | 1:93d997d6b232 | 49 | |
pmic | 1:93d997d6b232 | 50 | int main() |
pmic | 9:f10b974d01e0 | 51 | { |
pmic | 6:e1fa1a2d7483 | 52 | user_button.fall(&button_fall); |
pmic | 6:e1fa1a2d7483 | 53 | user_button.rise(&button_rise); |
pmic | 6:e1fa1a2d7483 | 54 | loop_timer.start(); |
pmic | 6:e1fa1a2d7483 | 55 | |
pmic | 10:c5d85e35758c | 56 | /* enable hardwaredriver dc motors */ |
pmic | 10:c5d85e35758c | 57 | enable_motors = 1; |
pmic | 10:c5d85e35758c | 58 | /* initialize pwm for motor M3*/ |
pmic | 10:c5d85e35758c | 59 | pwmOut_M3.period(Ts_pwm_s); |
pmic | 6:e1fa1a2d7483 | 60 | /* set pwm output zero at the beginning, range: 0...1 -> u_min...u_max */ |
pmic | 10:c5d85e35758c | 61 | pwmOut_M3.write(0.5); |
pmic | 9:f10b974d01e0 | 62 | |
pmic | 6:e1fa1a2d7483 | 63 | |
pmic | 1:93d997d6b232 | 64 | while (true) { |
jashaalm | 12:b87a1e165fb8 | 65 | batt = adc_vbat.read()* (3.3f/AUFLOESUNG); |
pmic | 6:e1fa1a2d7483 | 66 | loop_timer.reset(); |
pmic | 6:e1fa1a2d7483 | 67 | |
pmic | 6:e1fa1a2d7483 | 68 | /* ------------- start hacking ------------- -------------*/ |
pmic | 6:e1fa1a2d7483 | 69 | |
pmic | 6:e1fa1a2d7483 | 70 | if (executeMainTask) { |
pmic | 6:e1fa1a2d7483 | 71 | |
pmic | 6:e1fa1a2d7483 | 72 | /* read analog input */ |
pmic | 6:e1fa1a2d7483 | 73 | dist = analogIn.read() * 3.3f; |
pmic | 6:e1fa1a2d7483 | 74 | |
pmic | 10:c5d85e35758c | 75 | /* command a speed to dc motors M1 and M2*/ |
jashaalm | 11:5053ce50a993 | 76 | speedController_M1.setDesiredSpeedRPS( 2.0f); |
jashaalm | 12:b87a1e165fb8 | 77 | // speedController_M2.setDesiredSpeedRPS(-0.5f); |
pmic | 10:c5d85e35758c | 78 | /* write output voltage to motor M3 */ |
pmic | 10:c5d85e35758c | 79 | pwmOut_M3.write(0.75); |
pmic | 6:e1fa1a2d7483 | 80 | |
jashaalm | 12:b87a1e165fb8 | 81 | |
pmic | 6:e1fa1a2d7483 | 82 | /* visual feedback that the main task is executed */ |
pmic | 6:e1fa1a2d7483 | 83 | led = !led; |
pmic | 9:f10b974d01e0 | 84 | |
pmic | 1:93d997d6b232 | 85 | } else { |
pmic | 6:e1fa1a2d7483 | 86 | |
pmic | 6:e1fa1a2d7483 | 87 | dist = 0.0f; |
pmic | 1:93d997d6b232 | 88 | |
pmic | 10:c5d85e35758c | 89 | speedController_M1.setDesiredSpeedRPS(0.0f); |
jashaalm | 12:b87a1e165fb8 | 90 | // speedController_M2.setDesiredSpeedRPS(0.0f); |
pmic | 10:c5d85e35758c | 91 | pwmOut_M3.write(0.5); |
pmic | 6:e1fa1a2d7483 | 92 | |
jashaalm | 12:b87a1e165fb8 | 93 | |
pmic | 6:e1fa1a2d7483 | 94 | |
pmic | 6:e1fa1a2d7483 | 95 | led = 0; |
pmic | 1:93d997d6b232 | 96 | } |
pmic | 6:e1fa1a2d7483 | 97 | |
pmic | 10:c5d85e35758c | 98 | /* do only output via serial what's really necessary (this makes your code slow)*/ |
jashaalm | 12:b87a1e165fb8 | 99 | printf("%3.3f, %3.3f\n, %3d, %3.3f, %3.3f;\r\n", |
pmic | 10:c5d85e35758c | 100 | dist, |
jashaalm | 12:b87a1e165fb8 | 101 | batt, |
pmic | 10:c5d85e35758c | 102 | encoderCounter_M3.read(), |
pmic | 10:c5d85e35758c | 103 | speedController_M1.getSpeedRPS(), |
pmic | 10:c5d85e35758c | 104 | speedController_M2.getSpeedRPS()); |
pmic | 8:9bb806a7f585 | 105 | |
pmic | 6:e1fa1a2d7483 | 106 | /* ------------- stop hacking ------------- -------------*/ |
pmic | 6:e1fa1a2d7483 | 107 | |
pmic | 6:e1fa1a2d7483 | 108 | int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count(); |
pmic | 6:e1fa1a2d7483 | 109 | int dT_loop_ms = Ts_ms - T_loop_ms; |
pmic | 6:e1fa1a2d7483 | 110 | thread_sleep_for(dT_loop_ms); |
pmic | 1:93d997d6b232 | 111 | } |
pmic | 1:93d997d6b232 | 112 | } |
pmic | 6:e1fa1a2d7483 | 113 | |
pmic | 6:e1fa1a2d7483 | 114 | void button_fall() |
pmic | 6:e1fa1a2d7483 | 115 | { |
pmic | 6:e1fa1a2d7483 | 116 | user_button_timer.reset(); |
pmic | 6:e1fa1a2d7483 | 117 | user_button_timer.start(); |
pmic | 6:e1fa1a2d7483 | 118 | } |
pmic | 6:e1fa1a2d7483 | 119 | |
pmic | 6:e1fa1a2d7483 | 120 | void button_rise() |
pmic | 6:e1fa1a2d7483 | 121 | { |
pmic | 6:e1fa1a2d7483 | 122 | int t_button_ms = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 6:e1fa1a2d7483 | 123 | user_button_timer.stop(); |
pmic | 8:9bb806a7f585 | 124 | if (t_button_ms > 200) { |
pmic | 6:e1fa1a2d7483 | 125 | executeMainTask = !executeMainTask; |
pmic | 8:9bb806a7f585 | 126 | } |
pmic | 6:e1fa1a2d7483 | 127 | } |