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:
Sat Jun 25 00:37:31 2016 +0000
Revision:
4:2d38ad348e0d
Parent:
0:9afc272fa65f
Child:
8:da95e4ccbe4c
Included warning message about accelerometer.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Electrotiger 0:9afc272fa65f 1 /*
Electrotiger 0:9afc272fa65f 2 * HBridge.cpp
Electrotiger 0:9afc272fa65f 3 *
Electrotiger 0:9afc272fa65f 4 * Created on: Jun 4, 2016
Electrotiger 0:9afc272fa65f 5 * Author: Developer
Electrotiger 0:9afc272fa65f 6 */
Electrotiger 0:9afc272fa65f 7
Electrotiger 0:9afc272fa65f 8 #include <HBridge.hpp>
Electrotiger 0:9afc272fa65f 9 #include <cmath>
Electrotiger 0:9afc272fa65f 10
Electrotiger 0:9afc272fa65f 11 HBridge::HBridge(PinName PWMPin, PinName DirectionPin)
Electrotiger 0:9afc272fa65f 12 : PWMOut(PWMPin), DirectionOut(DirectionPin) {
Electrotiger 0:9afc272fa65f 13 // 10 ms period == 100 Hz works well.
Electrotiger 4:2d38ad348e0d 14 PWMOut.period_ms(1);
Electrotiger 0:9afc272fa65f 15 }
Electrotiger 0:9afc272fa65f 16
Electrotiger 0:9afc272fa65f 17 HBridge::~HBridge() {
Electrotiger 0:9afc272fa65f 18 // TODO Auto-generated destructor stub
Electrotiger 0:9afc272fa65f 19 }
Electrotiger 0:9afc272fa65f 20
Electrotiger 0:9afc272fa65f 21 void HBridge::write(float percent) {
Electrotiger 0:9afc272fa65f 22 // Write the percent value as a PWM out.
Electrotiger 0:9afc272fa65f 23 PWMOut.write(std::abs(percent));
Electrotiger 0:9afc272fa65f 24 // Write the direction as the sign of percent.
Electrotiger 0:9afc272fa65f 25 DirectionOut.write(percent >= 0);
Electrotiger 0:9afc272fa65f 26 // Set the output has changed flag to 1.
Electrotiger 0:9afc272fa65f 27 outputHasChanged = true;
Electrotiger 0:9afc272fa65f 28 }
Electrotiger 0:9afc272fa65f 29
Electrotiger 0:9afc272fa65f 30 float HBridge::read() {
Electrotiger 0:9afc272fa65f 31 static float prevValue;
Electrotiger 0:9afc272fa65f 32 if (outputHasChanged) {
Electrotiger 0:9afc272fa65f 33 outputHasChanged = false;
Electrotiger 0:9afc272fa65f 34 prevValue = (DirectionOut.read()) ? PWMOut.read() : -PWMOut.read();
Electrotiger 0:9afc272fa65f 35 }
Electrotiger 0:9afc272fa65f 36 return prevValue;
Electrotiger 0:9afc272fa65f 37 }