Class library for a L298 H-Bridge
Dependents: inverted_pendulum_system
Fork of L298HBridge by
L298HBridge.cpp@3:dabd8b6b2bff, 2017-08-25 (annotated)
- Committer:
- dudu941014
- Date:
- Fri Aug 25 21:00:45 2017 +0000
- Revision:
- 3:dabd8b6b2bff
- Parent:
- 2:1c000b6cf863
this code is for the inverted pendulum balancer. it is based the two loop PID controllers. One PID controller is for angle, and another one is for velocity
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dudu941014 | 3:dabd8b6b2bff | 1 | ////////////////////////////////////////////////////////////////////////////////// |
dudu941014 | 3:dabd8b6b2bff | 2 | // Company: edinburgh of university |
dudu941014 | 3:dabd8b6b2bff | 3 | // Engineer: ZEjun DU |
dudu941014 | 3:dabd8b6b2bff | 4 | // |
dudu941014 | 3:dabd8b6b2bff | 5 | // Create Date: 2017/08/20 13:06:52 |
dudu941014 | 3:dabd8b6b2bff | 6 | // Design Name: Inverted Pendulum Balancer |
dudu941014 | 3:dabd8b6b2bff | 7 | // Module Name: motor driver |
dudu941014 | 3:dabd8b6b2bff | 8 | // Tool Versions: “Keil 5” or “Mbed Complie Online” |
dudu941014 | 3:dabd8b6b2bff | 9 | // Description: this part is basic function of movement to control the motor according to PWM!!!!!!! |
dudu941014 | 3:dabd8b6b2bff | 10 | // |
dudu941014 | 3:dabd8b6b2bff | 11 | // |
dudu941014 | 3:dabd8b6b2bff | 12 | ////////////////////////////////////////////////////////////////////////////////// |
dudu941014 | 3:dabd8b6b2bff | 13 | |
RiaanEhlers | 0:39561fe6e4ff | 14 | #include "L298HBridge.h" |
RiaanEhlers | 0:39561fe6e4ff | 15 | #include "mbed.h" |
RiaanEhlers | 0:39561fe6e4ff | 16 | |
dudu941014 | 3:dabd8b6b2bff | 17 | //////////////////////////////////////////////////////////// |
dudu941014 | 3:dabd8b6b2bff | 18 | //this part is a independent function which control the movement based on L298n |
dudu941014 | 3:dabd8b6b2bff | 19 | //////////////////////////////////////////////////////////// |
RiaanEhlers | 0:39561fe6e4ff | 20 | L298HBridge::L298HBridge(PinName ENPin, PinName FWDPin, PinName REVPin) : _ENPin(ENPin), _FWDPin(FWDPin), _REVPin(REVPin) { |
dudu941014 | 3:dabd8b6b2bff | 21 | _ENPin = 0; //PWM = 0% |
dudu941014 | 3:dabd8b6b2bff | 22 | _FWDPin = 0; //PWM = 0% |
dudu941014 | 3:dabd8b6b2bff | 23 | _REVPin = 0; //PWM = 0% |
dudu941014 | 3:dabd8b6b2bff | 24 | _ENPin.period(0.0002); //set PWM period to 2mS as default. |
RiaanEhlers | 0:39561fe6e4ff | 25 | } |
dudu941014 | 3:dabd8b6b2bff | 26 | |
dudu941014 | 3:dabd8b6b2bff | 27 | //////////////////////////////////////////////////////////// |
dudu941014 | 3:dabd8b6b2bff | 28 | //this part is to move forward |
dudu941014 | 3:dabd8b6b2bff | 29 | //////////////////////////////////////////////////////////// |
RiaanEhlers | 1:6d242bb216d6 | 30 | void L298HBridge::Fwd() { |
dudu941014 | 3:dabd8b6b2bff | 31 | _FWDPin = 1;//enable foward pin |
dudu941014 | 3:dabd8b6b2bff | 32 | _REVPin = 0;//disable reverse pin |
RiaanEhlers | 0:39561fe6e4ff | 33 | } |
RiaanEhlers | 0:39561fe6e4ff | 34 | |
dudu941014 | 3:dabd8b6b2bff | 35 | //////////////////////////////////////////////////////////// |
dudu941014 | 3:dabd8b6b2bff | 36 | //this part is to move reserve |
dudu941014 | 3:dabd8b6b2bff | 37 | //////////////////////////////////////////////////////////// |
RiaanEhlers | 0:39561fe6e4ff | 38 | void L298HBridge::Rev() { |
dudu941014 | 3:dabd8b6b2bff | 39 | _FWDPin = 0;//disable foward pin |
dudu941014 | 3:dabd8b6b2bff | 40 | _REVPin = 1;//enable reverse pin |
RiaanEhlers | 0:39561fe6e4ff | 41 | } |
RiaanEhlers | 0:39561fe6e4ff | 42 | |
dudu941014 | 3:dabd8b6b2bff | 43 | //////////////////////////////////////////////////////////// |
dudu941014 | 3:dabd8b6b2bff | 44 | //this part is to stop the movement of the balancer |
dudu941014 | 3:dabd8b6b2bff | 45 | //////////////////////////////////////////////////////////// |
RiaanEhlers | 0:39561fe6e4ff | 46 | void L298HBridge::Stop() { |
dudu941014 | 3:dabd8b6b2bff | 47 | _FWDPin = 0;//disable foward pin |
dudu941014 | 3:dabd8b6b2bff | 48 | _REVPin = 0;//disable reverse pin |
RiaanEhlers | 0:39561fe6e4ff | 49 | } |
dudu941014 | 3:dabd8b6b2bff | 50 | |
dudu941014 | 3:dabd8b6b2bff | 51 | //////////////////////////////////////////////////////////// |
dudu941014 | 3:dabd8b6b2bff | 52 | //this part is to control the force of the motor according to PWM |
dudu941014 | 3:dabd8b6b2bff | 53 | //////////////////////////////////////////////////////////// |
RiaanEhlers | 2:1c000b6cf863 | 54 | void L298HBridge::Speed(float DutyPercent) { |
dudu941014 | 3:dabd8b6b2bff | 55 | _ENPin = DutyPercent /1000;//set PWM pin |
RiaanEhlers | 0:39561fe6e4ff | 56 | } |