Drivers for the mini robot designed for Princeton's MAE 433 course.
Dependencies: mbed-dsp mbed-rtos mbed
Dependents: MAE433_Library_Tester RobotBalancerv2
FixedRefresh.cpp@8:da95e4ccbe4c, 2016-06-30 (annotated)
- Committer:
- Electrotiger
- Date:
- Thu Jun 30 23:52:51 2016 +0000
- Revision:
- 8:da95e4ccbe4c
- Parent:
- 7:28f3a76792cd
Decreased H-Bridge Period to Provide for Even Finer Control.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Electrotiger | 6:076fcae78bac | 1 | /* |
Electrotiger | 6:076fcae78bac | 2 | * FixedRefresh.cpp |
Electrotiger | 6:076fcae78bac | 3 | * |
Electrotiger | 6:076fcae78bac | 4 | * Created on: Jun 26, 2016 |
Electrotiger | 6:076fcae78bac | 5 | * Author: Developer |
Electrotiger | 6:076fcae78bac | 6 | */ |
Electrotiger | 6:076fcae78bac | 7 | |
Electrotiger | 6:076fcae78bac | 8 | #include <FixedRefresh.hpp> |
Electrotiger | 6:076fcae78bac | 9 | |
Electrotiger | 6:076fcae78bac | 10 | |
Electrotiger | 6:076fcae78bac | 11 | |
Electrotiger | 6:076fcae78bac | 12 | FixedRefresh::FixedRefresh() |
Electrotiger | 7:28f3a76792cd | 13 | :waitSemaphore(0) { |
Electrotiger | 6:076fcae78bac | 14 | // Start the timer that measures the time between successive calls. |
Electrotiger | 6:076fcae78bac | 15 | elapsedTime.start(); |
Electrotiger | 6:076fcae78bac | 16 | } |
Electrotiger | 6:076fcae78bac | 17 | |
Electrotiger | 6:076fcae78bac | 18 | FixedRefresh::~FixedRefresh() { |
Electrotiger | 6:076fcae78bac | 19 | // TODO Auto-generated destructor stub |
Electrotiger | 6:076fcae78bac | 20 | } |
Electrotiger | 6:076fcae78bac | 21 | |
Electrotiger | 6:076fcae78bac | 22 | /* More complicated, Thread does not have a wait_us function, necessitating the use of |
Electrotiger | 6:076fcae78bac | 23 | * a semaphore and a callback function to perform the wait operation. |
Electrotiger | 6:076fcae78bac | 24 | */ |
Electrotiger | 6:076fcae78bac | 25 | |
Electrotiger | 6:076fcae78bac | 26 | bool FixedRefresh::refresh_us(uint32_t micros) { |
Electrotiger | 6:076fcae78bac | 27 | /* Capture the elapsed time. */ |
Electrotiger | 7:28f3a76792cd | 28 | uint32_t elapsedTimeCapture = elapsedTime.read_us() + 7; |
Electrotiger | 6:076fcae78bac | 29 | // If the elapsed time is less than the set time, then we can wait. |
Electrotiger | 6:076fcae78bac | 30 | if(elapsedTimeCapture < micros) { |
Electrotiger | 6:076fcae78bac | 31 | uint32_t waitTime = micros - elapsedTimeCapture; |
Electrotiger | 6:076fcae78bac | 32 | // Activate the timer callback. |
Electrotiger | 6:076fcae78bac | 33 | waitTimer.attach_us(this, &FixedRefresh::timerCallback, waitTime); |
Electrotiger | 6:076fcae78bac | 34 | // Call the wait function again. Since we have called wait before, it is locked. |
Electrotiger | 6:076fcae78bac | 35 | // This time, we actually wait for the timerCallback function to release it. |
Electrotiger | 6:076fcae78bac | 36 | waitSemaphore.wait(); |
Electrotiger | 6:076fcae78bac | 37 | return true; |
Electrotiger | 6:076fcae78bac | 38 | } |
Electrotiger | 6:076fcae78bac | 39 | else { |
Electrotiger | 6:076fcae78bac | 40 | return false; |
Electrotiger | 6:076fcae78bac | 41 | } |
Electrotiger | 6:076fcae78bac | 42 | } |
Electrotiger | 6:076fcae78bac | 43 | |
Electrotiger | 6:076fcae78bac | 44 | void FixedRefresh::timerCallback() { |
Electrotiger | 6:076fcae78bac | 45 | elapsedTime.reset(); |
Electrotiger | 6:076fcae78bac | 46 | waitSemaphore.release(); |
Electrotiger | 6:076fcae78bac | 47 | } |
Electrotiger | 6:076fcae78bac | 48 |