Drivers for the mini robot designed for Princeton's MAE 433 course.

Dependencies:   mbed-dsp mbed-rtos mbed

Dependents:   MAE433_Library_Tester RobotBalancerv2

Committer:
Electrotiger
Date:
Wed Jun 29 19:04:28 2016 +0000
Revision:
6:076fcae78bac
Child:
7:28f3a76792cd
Added FixedRefresh class.

Who changed what in which revision?

UserRevisionLine numberNew 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 6:076fcae78bac 13 :waitSemaphore(1) {
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 6:076fcae78bac 28 uint32_t elapsedTimeCapture = elapsedTime.read_us() + 12;
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 // Call the semaphore's wait function, thereby locking it.
Electrotiger 6:076fcae78bac 33 waitSemaphore.wait();
Electrotiger 6:076fcae78bac 34 // Activate the timer callback.
Electrotiger 6:076fcae78bac 35 waitTimer.attach_us(this, &FixedRefresh::timerCallback, waitTime);
Electrotiger 6:076fcae78bac 36 // Call the wait function again. Since we have called wait before, it is locked.
Electrotiger 6:076fcae78bac 37 // This time, we actually wait for the timerCallback function to release it.
Electrotiger 6:076fcae78bac 38 waitSemaphore.wait();
Electrotiger 6:076fcae78bac 39 return true;
Electrotiger 6:076fcae78bac 40 }
Electrotiger 6:076fcae78bac 41 else {
Electrotiger 6:076fcae78bac 42 return false;
Electrotiger 6:076fcae78bac 43 }
Electrotiger 6:076fcae78bac 44 }
Electrotiger 6:076fcae78bac 45
Electrotiger 6:076fcae78bac 46 void FixedRefresh::timerCallback() {
Electrotiger 6:076fcae78bac 47 elapsedTime.reset();
Electrotiger 6:076fcae78bac 48 waitSemaphore.release();
Electrotiger 6:076fcae78bac 49 waitSemaphore.release();
Electrotiger 6:076fcae78bac 50 }
Electrotiger 6:076fcae78bac 51