the fish that looks like a jet
Dependencies: ADXL345 ADXL345_I2C IMUfilter ITG3200 mbed Servo
motor_controller.cpp@0:ff9bc5f69c57, 2014-01-21 (annotated)
- Committer:
- sandwich
- Date:
- Tue Jan 21 21:32:05 2014 +0000
- Revision:
- 0:ff9bc5f69c57
- Child:
- 5:090ef6275773
pololu motor controller works with sine waveform input. IMU is untested
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sandwich | 0:ff9bc5f69c57 | 1 | #include "motor_controller.h" |
sandwich | 0:ff9bc5f69c57 | 2 | |
sandwich | 0:ff9bc5f69c57 | 3 | PololuMController::PololuMController(PinName pwmport, PinName A, PinName B) |
sandwich | 0:ff9bc5f69c57 | 4 | { |
sandwich | 0:ff9bc5f69c57 | 5 | pwm=new PwmOut(pwmport); |
sandwich | 0:ff9bc5f69c57 | 6 | outA=new DigitalOut(A); |
sandwich | 0:ff9bc5f69c57 | 7 | outB=new DigitalOut(B); |
sandwich | 0:ff9bc5f69c57 | 8 | outA->write(0); |
sandwich | 0:ff9bc5f69c57 | 9 | outB->write(1); |
sandwich | 0:ff9bc5f69c57 | 10 | timestamp=0; |
sandwich | 0:ff9bc5f69c57 | 11 | } |
sandwich | 0:ff9bc5f69c57 | 12 | |
sandwich | 0:ff9bc5f69c57 | 13 | PololuMController::~PololuMController() |
sandwich | 0:ff9bc5f69c57 | 14 | { |
sandwich | 0:ff9bc5f69c57 | 15 | delete pwm; |
sandwich | 0:ff9bc5f69c57 | 16 | delete outA; |
sandwich | 0:ff9bc5f69c57 | 17 | delete outB; |
sandwich | 0:ff9bc5f69c57 | 18 | } |
sandwich | 0:ff9bc5f69c57 | 19 | |
sandwich | 0:ff9bc5f69c57 | 20 | void PololuMController::setspeed(float speed) |
sandwich | 0:ff9bc5f69c57 | 21 | { |
sandwich | 0:ff9bc5f69c57 | 22 | pwm->write(speed); |
sandwich | 0:ff9bc5f69c57 | 23 | return; |
sandwich | 0:ff9bc5f69c57 | 24 | } |
sandwich | 0:ff9bc5f69c57 | 25 | |
sandwich | 0:ff9bc5f69c57 | 26 | void PololuMController::setpolarspeed(float speed) |
sandwich | 0:ff9bc5f69c57 | 27 | { |
sandwich | 0:ff9bc5f69c57 | 28 | if (speed>=0) |
sandwich | 0:ff9bc5f69c57 | 29 | { |
sandwich | 0:ff9bc5f69c57 | 30 | outA->write(0); |
sandwich | 0:ff9bc5f69c57 | 31 | outB->write(1); |
sandwich | 0:ff9bc5f69c57 | 32 | pwm->write(abs(speed)); |
sandwich | 0:ff9bc5f69c57 | 33 | } |
sandwich | 0:ff9bc5f69c57 | 34 | else |
sandwich | 0:ff9bc5f69c57 | 35 | { |
sandwich | 0:ff9bc5f69c57 | 36 | outA->write(1); |
sandwich | 0:ff9bc5f69c57 | 37 | outB->write(0); |
sandwich | 0:ff9bc5f69c57 | 38 | pwm->write(abs(speed)); |
sandwich | 0:ff9bc5f69c57 | 39 | } |
sandwich | 0:ff9bc5f69c57 | 40 | return; |
sandwich | 0:ff9bc5f69c57 | 41 | } |
sandwich | 0:ff9bc5f69c57 | 42 | |
sandwich | 0:ff9bc5f69c57 | 43 | void PololuMController::reverse() |
sandwich | 0:ff9bc5f69c57 | 44 | { |
sandwich | 0:ff9bc5f69c57 | 45 | outA->write(!(outA->read())); |
sandwich | 0:ff9bc5f69c57 | 46 | outB->write(!(outB->read())); |
sandwich | 0:ff9bc5f69c57 | 47 | return; |
sandwich | 0:ff9bc5f69c57 | 48 | } |
sandwich | 0:ff9bc5f69c57 | 49 | |
sandwich | 0:ff9bc5f69c57 | 50 | void PololuMController::drive_sinusoidal(float cur_time, float a, float w, float phi) |
sandwich | 0:ff9bc5f69c57 | 51 | { |
sandwich | 0:ff9bc5f69c57 | 52 | setpolarspeed(a*sin(w*cur_time+phi)); |
sandwich | 0:ff9bc5f69c57 | 53 | return; |
sandwich | 0:ff9bc5f69c57 | 54 | } |