Class library for a L298 H-Bridge

Dependents:   inverted_pendulum_system

Fork of L298HBridge by Riaan Ehlers

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?

UserRevisionLine numberNew 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 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 #ifndef L298HBridge_H
RiaanEhlers 0:39561fe6e4ff 15 #define L298HBridge_H
RiaanEhlers 0:39561fe6e4ff 16
RiaanEhlers 0:39561fe6e4ff 17 #include "mbed.h"
RiaanEhlers 0:39561fe6e4ff 18
RiaanEhlers 2:1c000b6cf863 19 /** Class library for a L298 H-Bridge. The class is written for one H-Bridge of
RiaanEhlers 2:1c000b6cf863 20 * the L298. Constructing the class twice will enable you to use both H-bridges.
RiaanEhlers 0:39561fe6e4ff 21 *
RiaanEhlers 0:39561fe6e4ff 22 * Example:
RiaanEhlers 0:39561fe6e4ff 23 * @code
RiaanEhlers 2:1c000b6cf863 24 * #include "mbed.h"
RiaanEhlers 2:1c000b6cf863 25 * #include "L298HBridge.h"
RiaanEhlers 2:1c000b6cf863 26
RiaanEhlers 2:1c000b6cf863 27 * L298HBridge Motor(PB_4, PC_4, PC_5);
RiaanEhlers 2:1c000b6cf863 28
RiaanEhlers 2:1c000b6cf863 29 * int main()
RiaanEhlers 2:1c000b6cf863 30 * {
RiaanEhlers 2:1c000b6cf863 31 * float i;
RiaanEhlers 2:1c000b6cf863 32 * while(1)
RiaanEhlers 2:1c000b6cf863 33 * {
RiaanEhlers 2:1c000b6cf863 34 * Motor.Fwd();
RiaanEhlers 2:1c000b6cf863 35 *
RiaanEhlers 2:1c000b6cf863 36 * for(i=0;i<100;i++)
RiaanEhlers 2:1c000b6cf863 37 * {
RiaanEhlers 2:1c000b6cf863 38 * Motor.Speed(i);
RiaanEhlers 2:1c000b6cf863 39 * wait(0.1);
RiaanEhlers 2:1c000b6cf863 40 * }
RiaanEhlers 2:1c000b6cf863 41 *
RiaanEhlers 2:1c000b6cf863 42 * for(i=100;i>25;i--)
RiaanEhlers 2:1c000b6cf863 43 * {
RiaanEhlers 2:1c000b6cf863 44 * Motor.Speed(i);
RiaanEhlers 2:1c000b6cf863 45 * wait(0.1);
RiaanEhlers 2:1c000b6cf863 46 * }
RiaanEhlers 2:1c000b6cf863 47 *
RiaanEhlers 2:1c000b6cf863 48 * Motor.Rev();
RiaanEhlers 2:1c000b6cf863 49 *
RiaanEhlers 2:1c000b6cf863 50 * for(i=0;i<100;i++)
RiaanEhlers 2:1c000b6cf863 51 * {
RiaanEhlers 2:1c000b6cf863 52 * Motor.Speed(i);
RiaanEhlers 2:1c000b6cf863 53 * wait(0.1);
RiaanEhlers 2:1c000b6cf863 54 * }
RiaanEhlers 2:1c000b6cf863 55 *
RiaanEhlers 2:1c000b6cf863 56 * for(i=100;i>25;i--)
RiaanEhlers 2:1c000b6cf863 57 * {
RiaanEhlers 2:1c000b6cf863 58 * Motor.Speed(i);
RiaanEhlers 2:1c000b6cf863 59 * wait(0.1);
RiaanEhlers 2:1c000b6cf863 60 * }
RiaanEhlers 2:1c000b6cf863 61 * }
RiaanEhlers 2:1c000b6cf863 62 * }
RiaanEhlers 0:39561fe6e4ff 63 * @endcode
RiaanEhlers 0:39561fe6e4ff 64 */
RiaanEhlers 0:39561fe6e4ff 65
RiaanEhlers 0:39561fe6e4ff 66 class L298HBridge {
RiaanEhlers 0:39561fe6e4ff 67 public:
RiaanEhlers 1:6d242bb216d6 68 /** Create a L298HBridge object connected to the specified pins.
RiaanEhlers 1:6d242bb216d6 69 * Once created, the motor speed will be set to 0 (PWM signal will be 0%) and
RiaanEhlers 2:1c000b6cf863 70 * the motor will be in the stop mode (neither forward or reverse).
RiaanEhlers 2:1c000b6cf863 71 * @param ENPin PwmOut compatible pin used to connect to L298's En(x) pin associated with enabling the H-Bridge.
RiaanEhlers 2:1c000b6cf863 72 * @param FWDPin GPIO pin used to connect to L298's In(x) pin associated with forward direction.
RiaanEhlers 2:1c000b6cf863 73 * @param REVPin GPIO pin used to connect to L298's In(x) pin associated with reverse direction.
RiaanEhlers 0:39561fe6e4ff 74 */
RiaanEhlers 0:39561fe6e4ff 75 L298HBridge(PinName ENPin, PinName FWDPin, PinName REVPin);
RiaanEhlers 0:39561fe6e4ff 76
RiaanEhlers 2:1c000b6cf863 77 /** Configure the H-Bridge to run the motor in the forward direction.
RiaanEhlers 0:39561fe6e4ff 78 * @param None
RiaanEhlers 0:39561fe6e4ff 79 */
RiaanEhlers 0:39561fe6e4ff 80 void Fwd();
RiaanEhlers 0:39561fe6e4ff 81
RiaanEhlers 2:1c000b6cf863 82 /** Configure the H-Bridge to run the motor in the reverse direction.
RiaanEhlers 0:39561fe6e4ff 83 * @param None
RiaanEhlers 0:39561fe6e4ff 84 */
RiaanEhlers 0:39561fe6e4ff 85 void Rev();
RiaanEhlers 0:39561fe6e4ff 86
RiaanEhlers 1:6d242bb216d6 87 /** Switch the H-Bridge off. The H-Bridge is not set to forward or reverse.
RiaanEhlers 0:39561fe6e4ff 88 * @param None
RiaanEhlers 0:39561fe6e4ff 89 */
RiaanEhlers 0:39561fe6e4ff 90 void Stop();
RiaanEhlers 0:39561fe6e4ff 91
RiaanEhlers 2:1c000b6cf863 92 /** Change the motor's speed by adjusting the PWM signal.
RiaanEhlers 2:1c000b6cf863 93 * The value passed to the function can be any value from 0 to 100.
RiaanEhlers 2:1c000b6cf863 94 * Where 0 = 0% and 100 = 100%.
RiaanEhlers 2:1c000b6cf863 95 * @param DutyPercent
RiaanEhlers 0:39561fe6e4ff 96 */
RiaanEhlers 2:1c000b6cf863 97 void Speed(float DutyPercent);
RiaanEhlers 0:39561fe6e4ff 98
RiaanEhlers 0:39561fe6e4ff 99 private:
RiaanEhlers 0:39561fe6e4ff 100 PwmOut _ENPin;
RiaanEhlers 0:39561fe6e4ff 101 DigitalOut _FWDPin, _REVPin;
RiaanEhlers 0:39561fe6e4ff 102 };
RiaanEhlers 0:39561fe6e4ff 103
RiaanEhlers 0:39561fe6e4ff 104 #endif