Basic Library for Pololu DRV8835
Dependents: ME503_VehicleAssembly
DRV8835.cpp@2:6e781e956958, 2017-09-13 (annotated)
- Committer:
- DrCoyle
- Date:
- Wed Sep 13 20:52:48 2017 +0000
- Revision:
- 2:6e781e956958
- Parent:
- 1:edc8af84b968
Update that includes streaming of USB to Matlab
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DrCoyle | 0:0902ae053e3f | 1 | /* File: DRV8835.cpp |
DrCoyle | 0:0902ae053e3f | 2 | * Adopted from work by Cameron Isbell |
DrCoyle | 0:0902ae053e3f | 3 | * |
DrCoyle | 0:0902ae053e3f | 4 | * Description: library for DRV8835 Motor Driver |
DrCoyle | 0:0902ae053e3f | 5 | * Assumptions: A is left and B is right |
DrCoyle | 0:0902ae053e3f | 6 | */ |
DrCoyle | 0:0902ae053e3f | 7 | |
DrCoyle | 0:0902ae053e3f | 8 | #include "mbed.h" |
DrCoyle | 0:0902ae053e3f | 9 | #include "DRV8835.h" |
DrCoyle | 0:0902ae053e3f | 10 | |
DrCoyle | 0:0902ae053e3f | 11 | |
DrCoyle | 0:0902ae053e3f | 12 | DRV8835::DRV8835( PinName pinPwmL, PinName pinLin, |
DrCoyle | 0:0902ae053e3f | 13 | PinName pinPwmR, PinName pinRin) : |
DrCoyle | 0:0902ae053e3f | 14 | pwmL(pinPwmL), |
DrCoyle | 0:0902ae053e3f | 15 | Lin(pinLin), |
DrCoyle | 0:0902ae053e3f | 16 | pwmR(pinPwmR), |
DrCoyle | 0:0902ae053e3f | 17 | Rin(pinRin) |
DrCoyle | 0:0902ae053e3f | 18 | { |
DrCoyle | 0:0902ae053e3f | 19 | Lin = 0; |
DrCoyle | 0:0902ae053e3f | 20 | Rin = 0; |
DrCoyle | 0:0902ae053e3f | 21 | pwmL.period(DRV8835_PWM_PERIOD_DEFAULT); |
DrCoyle | 0:0902ae053e3f | 22 | pwmL = DRV8835_PWM_PULSEWIDTH_DEFAULT; |
DrCoyle | 0:0902ae053e3f | 23 | pwmR.period(DRV8835_PWM_PERIOD_DEFAULT); |
DrCoyle | 0:0902ae053e3f | 24 | pwmR = DRV8835_PWM_PULSEWIDTH_DEFAULT; |
DrCoyle | 0:0902ae053e3f | 25 | motorL_stop(); |
DrCoyle | 0:0902ae053e3f | 26 | motorR_stop(); |
DrCoyle | 0:0902ae053e3f | 27 | } |
DrCoyle | 0:0902ae053e3f | 28 | |
DrCoyle | 1:edc8af84b968 | 29 | void DRV8835::stop() |
DrCoyle | 1:edc8af84b968 | 30 | { |
DrCoyle | 1:edc8af84b968 | 31 | motorL_stop(); |
DrCoyle | 1:edc8af84b968 | 32 | motorR_stop(); |
DrCoyle | 1:edc8af84b968 | 33 | } |
DrCoyle | 0:0902ae053e3f | 34 | |
DrCoyle | 0:0902ae053e3f | 35 | void DRV8835::motorL_stop(void) |
DrCoyle | 0:0902ae053e3f | 36 | { |
DrCoyle | 0:0902ae053e3f | 37 | pwmL = 0; |
DrCoyle | 0:0902ae053e3f | 38 | } |
DrCoyle | 0:0902ae053e3f | 39 | |
DrCoyle | 1:edc8af84b968 | 40 | void DRV8835::motorR_stop(void) |
DrCoyle | 1:edc8af84b968 | 41 | { |
DrCoyle | 1:edc8af84b968 | 42 | pwmR = 0; |
DrCoyle | 1:edc8af84b968 | 43 | } |
DrCoyle | 1:edc8af84b968 | 44 | |
DrCoyle | 1:edc8af84b968 | 45 | void DRV8835::setSpeeds(float Right,float Left) |
DrCoyle | 1:edc8af84b968 | 46 | { |
DrCoyle | 1:edc8af84b968 | 47 | //Set Right Speed and Direction |
DrCoyle | 1:edc8af84b968 | 48 | if(Right<0) |
DrCoyle | 1:edc8af84b968 | 49 | { |
DrCoyle | 2:6e781e956958 | 50 | motorR_rev(Right*-1); |
DrCoyle | 1:edc8af84b968 | 51 | } else { |
DrCoyle | 2:6e781e956958 | 52 | motorR_fwd(Right); |
DrCoyle | 1:edc8af84b968 | 53 | } |
DrCoyle | 1:edc8af84b968 | 54 | |
DrCoyle | 1:edc8af84b968 | 55 | //Set Left Speed and Direction |
DrCoyle | 1:edc8af84b968 | 56 | if(Left<0) |
DrCoyle | 1:edc8af84b968 | 57 | { |
DrCoyle | 2:6e781e956958 | 58 | motorL_rev(Left*-1); |
DrCoyle | 1:edc8af84b968 | 59 | } else { |
DrCoyle | 2:6e781e956958 | 60 | motorL_fwd(Left); |
DrCoyle | 1:edc8af84b968 | 61 | } |
DrCoyle | 1:edc8af84b968 | 62 | } |
DrCoyle | 2:6e781e956958 | 63 | |
DrCoyle | 2:6e781e956958 | 64 | void DRV8835::motorL_fwd(float fPulsewidth) |
DrCoyle | 0:0902ae053e3f | 65 | { |
DrCoyle | 0:0902ae053e3f | 66 | Lin = 0; |
DrCoyle | 0:0902ae053e3f | 67 | pwmL = fPulsewidth; |
DrCoyle | 0:0902ae053e3f | 68 | } |
DrCoyle | 2:6e781e956958 | 69 | void DRV8835::motorL_rev(float fPulsewidth) |
DrCoyle | 0:0902ae053e3f | 70 | { |
DrCoyle | 0:0902ae053e3f | 71 | Lin = 1; |
DrCoyle | 0:0902ae053e3f | 72 | pwmL = fPulsewidth; |
DrCoyle | 0:0902ae053e3f | 73 | } |
DrCoyle | 0:0902ae053e3f | 74 | |
DrCoyle | 0:0902ae053e3f | 75 | void DRV8835::motorR_fwd(float fPulsewidth) |
DrCoyle | 0:0902ae053e3f | 76 | { |
DrCoyle | 2:6e781e956958 | 77 | Rin = 1; |
DrCoyle | 2:6e781e956958 | 78 | pwmR = fPulsewidth; |
DrCoyle | 2:6e781e956958 | 79 | } |
DrCoyle | 2:6e781e956958 | 80 | void DRV8835::motorR_rev(float fPulsewidth) |
DrCoyle | 2:6e781e956958 | 81 | { |
DrCoyle | 0:0902ae053e3f | 82 | Rin = 0; |
DrCoyle | 0:0902ae053e3f | 83 | pwmR = fPulsewidth; |
DrCoyle | 0:0902ae053e3f | 84 | } |
DrCoyle | 0:0902ae053e3f | 85 |