Hauptprogramm
Dependencies: ILI9340_Driver_Lib PM2_Libary Lib_DFPlayerMini
main.cpp@12:dfa3591affef, 2021-04-14 (annotated)
- Committer:
- ackerden
- Date:
- Wed Apr 14 14:23:47 2021 +0000
- Revision:
- 12:dfa3591affef
- Parent:
- 11:be62f37f3a98
- Child:
- 13:096e5dc3ac23
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ackerden | 12:dfa3591affef | 1 | /*Zeitinput am Anfang: Startzeit 8:00*/ |
ackerden | 12:dfa3591affef | 2 | |
ackerden | 12:dfa3591affef | 3 | #include <I2C.h> |
pmic | 1:93d997d6b232 | 4 | #include "mbed.h" |
pmic | 1:93d997d6b232 | 5 | #include "platform/mbed_thread.h" |
pmic | 6:e1fa1a2d7483 | 6 | |
ackerden | 12:dfa3591affef | 7 | #include "mbed_rtc_time.h" |
haefeman | 11:be62f37f3a98 | 8 | |
pmic | 6:e1fa1a2d7483 | 9 | /* PM2_Libary */ |
pmic | 1:93d997d6b232 | 10 | #include "EncoderCounter.h" |
pmic | 1:93d997d6b232 | 11 | #include "Servo.h" |
pmic | 3:d22942631cd7 | 12 | #include "SpeedController.h" |
pmic | 9:f10b974d01e0 | 13 | #include "FastPWM.h" |
pmic | 6:e1fa1a2d7483 | 14 | |
pmic | 6:e1fa1a2d7483 | 15 | using namespace std::chrono; |
pmic | 6:e1fa1a2d7483 | 16 | |
pmic | 8:9bb806a7f585 | 17 | InterruptIn user_button(USER_BUTTON); |
pmic | 8:9bb806a7f585 | 18 | DigitalOut led(LED1); |
pmic | 6:e1fa1a2d7483 | 19 | |
ackerden | 12:dfa3591affef | 20 | |
pmic | 6:e1fa1a2d7483 | 21 | bool executeMainTask = false; |
pmic | 6:e1fa1a2d7483 | 22 | Timer user_button_timer, loop_timer; |
pmic | 7:c0f5bb355f41 | 23 | int Ts_ms = 50; |
pmic | 6:e1fa1a2d7483 | 24 | |
pmic | 6:e1fa1a2d7483 | 25 | /* declaration of custom button functions */ |
pmic | 6:e1fa1a2d7483 | 26 | void button_fall(); |
pmic | 6:e1fa1a2d7483 | 27 | void button_rise(); |
pmic | 6:e1fa1a2d7483 | 28 | |
ackerden | 12:dfa3591affef | 29 | |
pmic | 6:e1fa1a2d7483 | 30 | /* create analog input object */ |
ackerden | 12:dfa3591affef | 31 | /*AnalogIn analogIn(PC_2); |
pmic | 6:e1fa1a2d7483 | 32 | float dist = 0.0f; |
ackerden | 12:dfa3591affef | 33 | */ |
pmic | 6:e1fa1a2d7483 | 34 | /* create servo objects */ |
ackerden | 12:dfa3591affef | 35 | /*Servo servo_S1(PB_2); |
pmic | 10:c5d85e35758c | 36 | Servo servo_S2(PC_8); |
pmic | 10:c5d85e35758c | 37 | // Servo servo_S3(PC_6); // not needed in this example |
pmic | 10:c5d85e35758c | 38 | int servoPeriod_mus = 20000; |
pmic | 10:c5d85e35758c | 39 | int servoOutput_mus_S1 = 0; |
pmic | 10:c5d85e35758c | 40 | int servoOutput_mus_S2 = 0; |
ackerden | 12:dfa3591affef | 41 | int servo_counter = 0;*/ |
pmic | 10:c5d85e35758c | 42 | int loops_per_second = static_cast<int>(ceilf(1.0f/(0.001f*(float)Ts_ms))); |
pmic | 1:93d997d6b232 | 43 | |
pmic | 1:93d997d6b232 | 44 | int main() |
pmic | 9:f10b974d01e0 | 45 | { |
ackerden | 12:dfa3591affef | 46 | set_time(1618332129); // Set RTC time to Wed, 28 Oct 2009 11:35:37 |
pmic | 6:e1fa1a2d7483 | 47 | user_button.fall(&button_fall); |
pmic | 6:e1fa1a2d7483 | 48 | user_button.rise(&button_rise); |
pmic | 6:e1fa1a2d7483 | 49 | loop_timer.start(); |
pmic | 6:e1fa1a2d7483 | 50 | |
pmic | 9:f10b974d01e0 | 51 | |
pmic | 10:c5d85e35758c | 52 | /* enable servos, you can also disable them */ |
ackerden | 12:dfa3591affef | 53 | //servo_S1.Enable(servoOutput_mus_S1, servoPeriod_mus); |
ackerden | 12:dfa3591affef | 54 | //servo_S2.Enable(servoOutput_mus_S2, servoPeriod_mus); |
pmic | 6:e1fa1a2d7483 | 55 | |
pmic | 1:93d997d6b232 | 56 | while (true) { |
pmic | 6:e1fa1a2d7483 | 57 | |
pmic | 6:e1fa1a2d7483 | 58 | loop_timer.reset(); |
ackerden | 12:dfa3591affef | 59 | |
ackerden | 12:dfa3591affef | 60 | |
pmic | 6:e1fa1a2d7483 | 61 | |
ackerden | 12:dfa3591affef | 62 | |
pmic | 6:e1fa1a2d7483 | 63 | |
pmic | 6:e1fa1a2d7483 | 64 | if (executeMainTask) { |
ackerden | 12:dfa3591affef | 65 | time_t seconds = time(NULL); |
ackerden | 12:dfa3591affef | 66 | |
ackerden | 12:dfa3591affef | 67 | printf("Time as seconds since January 1, 1970 = %u\n", (unsigned int)seconds); |
ackerden | 12:dfa3591affef | 68 | |
ackerden | 12:dfa3591affef | 69 | printf("Time as a basic string = %s", ctime(&seconds)); |
ackerden | 12:dfa3591affef | 70 | |
ackerden | 12:dfa3591affef | 71 | char buffer[32]; |
ackerden | 12:dfa3591affef | 72 | strftime(buffer, 32, "%I:%M %p\n", localtime(&seconds)); |
ackerden | 12:dfa3591affef | 73 | printf("Time as a custom formatted string = %s", buffer); |
ackerden | 12:dfa3591affef | 74 | |
ackerden | 12:dfa3591affef | 75 | ThisThread::sleep_for(50); |
pmic | 6:e1fa1a2d7483 | 76 | |
pmic | 6:e1fa1a2d7483 | 77 | /* read analog input */ |
ackerden | 12:dfa3591affef | 78 | //dist = analogIn.read() * 3.3f; |
pmic | 6:e1fa1a2d7483 | 79 | |
pmic | 10:c5d85e35758c | 80 | /* command servo position via output time, this needs to be calibrated */ |
ackerden | 12:dfa3591affef | 81 | /*servo_S1.SetPosition(servoOutput_mus_S1); |
pmic | 10:c5d85e35758c | 82 | servo_S2.SetPosition(servoOutput_mus_S2); |
pmic | 10:c5d85e35758c | 83 | if (servoOutput_mus_S1 <= servoPeriod_mus & servo_counter%loops_per_second == 0 & servo_counter != 0) { |
pmic | 10:c5d85e35758c | 84 | servoOutput_mus_S1 += 100; |
pmic | 8:9bb806a7f585 | 85 | } |
pmic | 10:c5d85e35758c | 86 | if (servoOutput_mus_S2 <= servoPeriod_mus & servo_counter%loops_per_second == 0 & servo_counter != 0) { |
pmic | 10:c5d85e35758c | 87 | servoOutput_mus_S2 += 100; |
pmic | 8:9bb806a7f585 | 88 | } |
ackerden | 12:dfa3591affef | 89 | servo_counter++;*/ |
pmic | 6:e1fa1a2d7483 | 90 | |
pmic | 6:e1fa1a2d7483 | 91 | /* visual feedback that the main task is executed */ |
pmic | 6:e1fa1a2d7483 | 92 | led = !led; |
pmic | 9:f10b974d01e0 | 93 | |
pmic | 1:93d997d6b232 | 94 | } else { |
pmic | 6:e1fa1a2d7483 | 95 | |
ackerden | 12:dfa3591affef | 96 | //dist = 0.0f; |
pmic | 1:93d997d6b232 | 97 | |
ackerden | 12:dfa3591affef | 98 | //servoOutput_mus_S1 = 0; |
ackerden | 12:dfa3591affef | 99 | //servoOutput_mus_S2 = 0; |
ackerden | 12:dfa3591affef | 100 | //servo_S1.SetPosition(servoOutput_mus_S1); |
ackerden | 12:dfa3591affef | 101 | //servo_S2.SetPosition(servoOutput_mus_S2); |
pmic | 6:e1fa1a2d7483 | 102 | |
pmic | 6:e1fa1a2d7483 | 103 | led = 0; |
pmic | 1:93d997d6b232 | 104 | } |
pmic | 6:e1fa1a2d7483 | 105 | |
pmic | 10:c5d85e35758c | 106 | /* do only output via serial what's really necessary (this makes your code slow)*/ |
ackerden | 12:dfa3591affef | 107 | //printf("%3.3f, %3d;\r\n", |
ackerden | 12:dfa3591affef | 108 | //dist, |
ackerden | 12:dfa3591affef | 109 | //servoOutput_mus_S1, |
ackerden | 12:dfa3591affef | 110 | //servoOutput_mus_S2); |
ackerden | 12:dfa3591affef | 111 | |
pmic | 8:9bb806a7f585 | 112 | |
pmic | 6:e1fa1a2d7483 | 113 | |
pmic | 6:e1fa1a2d7483 | 114 | int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count(); |
pmic | 6:e1fa1a2d7483 | 115 | int dT_loop_ms = Ts_ms - T_loop_ms; |
pmic | 6:e1fa1a2d7483 | 116 | thread_sleep_for(dT_loop_ms); |
pmic | 1:93d997d6b232 | 117 | } |
pmic | 1:93d997d6b232 | 118 | } |
pmic | 6:e1fa1a2d7483 | 119 | |
pmic | 6:e1fa1a2d7483 | 120 | void button_fall() |
pmic | 6:e1fa1a2d7483 | 121 | { |
pmic | 6:e1fa1a2d7483 | 122 | user_button_timer.reset(); |
pmic | 6:e1fa1a2d7483 | 123 | user_button_timer.start(); |
pmic | 6:e1fa1a2d7483 | 124 | } |
pmic | 6:e1fa1a2d7483 | 125 | |
pmic | 6:e1fa1a2d7483 | 126 | void button_rise() |
pmic | 6:e1fa1a2d7483 | 127 | { |
pmic | 6:e1fa1a2d7483 | 128 | int t_button_ms = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 6:e1fa1a2d7483 | 129 | user_button_timer.stop(); |
pmic | 8:9bb806a7f585 | 130 | if (t_button_ms > 200) { |
pmic | 6:e1fa1a2d7483 | 131 | executeMainTask = !executeMainTask; |
pmic | 8:9bb806a7f585 | 132 | } |
pmic | 6:e1fa1a2d7483 | 133 | } |