amin omar
/
Motorslam
Motor class for the robot
Motorslam.h@0:550e01736339, 2016-05-04 (annotated)
- Committer:
- aminomar
- Date:
- Wed May 04 18:47:50 2016 +0000
- Revision:
- 0:550e01736339
motor;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aminomar | 0:550e01736339 | 1 | /** |
aminomar | 0:550e01736339 | 2 | header file for motor, edit later |
aminomar | 0:550e01736339 | 3 | EDIT THE PWM FOR BOTH MOTOR |
aminomar | 0:550e01736339 | 4 | motorInit() |
aminomar | 0:550e01736339 | 5 | */ |
aminomar | 0:550e01736339 | 6 | #ifndef MOTORSLAM_H |
aminomar | 0:550e01736339 | 7 | #define MOTORSLAM_H |
aminomar | 0:550e01736339 | 8 | |
aminomar | 0:550e01736339 | 9 | //mbed libraries// |
aminomar | 0:550e01736339 | 10 | #include "mbed.h" |
aminomar | 0:550e01736339 | 11 | |
aminomar | 0:550e01736339 | 12 | #define DEFAULT_PWM_PERIOD (0.001) // 1 KHz |
aminomar | 0:550e01736339 | 13 | #define DEFAULT_PWM_PULSEWIDTH (0.50) // 50% duty cycle |
aminomar | 0:550e01736339 | 14 | |
aminomar | 0:550e01736339 | 15 | /** Interface created to control left and right motors of SLAM robot |
aminomar | 0:550e01736339 | 16 | * TB6612FNG is the motor driver(H-bridge,dual motor driver) |
aminomar | 0:550e01736339 | 17 | */ |
aminomar | 0:550e01736339 | 18 | |
aminomar | 0:550e01736339 | 19 | class Motorslam { |
aminomar | 0:550e01736339 | 20 | public: //Any class can refer to the field or call the method in it |
aminomar | 0:550e01736339 | 21 | /** Create an interface to connect TB6612FNG with mbed LPC1768 |
aminomar | 0:550e01736339 | 22 | * @param pwmLeft, PWM output from mbed is channelled to Left Motor to control speed |
aminomar | 0:550e01736339 | 23 | * @param leftIn1, DigitalOut1 pin of mbed set to HIGH(1) to move motor forward |
aminomar | 0:550e01736339 | 24 | * @param leftIn2, DigitalOut2 pin of mbed set to HIGH(1) to move motor backwards |
aminomar | 0:550e01736339 | 25 | */ |
aminomar | 0:550e01736339 | 26 | Motorslam( PinName pinPwmLeft, PinName pinLeftIn1, PinName pinLeftIn2, |
aminomar | 0:550e01736339 | 27 | PinName pinPwmRight, PinName pinRightIn1, PinName pinRightIn2, |
aminomar | 0:550e01736339 | 28 | PinName pinNStby); |
aminomar | 0:550e01736339 | 29 | |
aminomar | 0:550e01736339 | 30 | /** Set the speed of the motor |
aminomar | 0:550e01736339 | 31 | * |
aminomar | 0:550e01736339 | 32 | * @param speed The speed of the motor is normalised between -1.0 and 1.0 |
aminomar | 0:550e01736339 | 33 | */ |
aminomar | 0:550e01736339 | 34 | |
aminomar | 0:550e01736339 | 35 | void init(); |
aminomar | 0:550e01736339 | 36 | void setPwmLeft(float fPeriod, float fPulsewidth); |
aminomar | 0:550e01736339 | 37 | void setPwmLeftperiod(float fPeriod); |
aminomar | 0:550e01736339 | 38 | void setPwmLeftpulsewidth(float fPulsewidth); |
aminomar | 0:550e01736339 | 39 | |
aminomar | 0:550e01736339 | 40 | void setPwmRight(float fPeriod, float fPulsewidth); |
aminomar | 0:550e01736339 | 41 | void setPwmRightperiod(float fPeriod); |
aminomar | 0:550e01736339 | 42 | void setPwmRightpulsewidth(float fPulsewidth); |
aminomar | 0:550e01736339 | 43 | |
aminomar | 0:550e01736339 | 44 | void standby(void); |
aminomar | 0:550e01736339 | 45 | |
aminomar | 0:550e01736339 | 46 | void motorLeft_stop(void); |
aminomar | 0:550e01736339 | 47 | void motorLeft_cw(void); |
aminomar | 0:550e01736339 | 48 | void motorLeft_ccw(void); |
aminomar | 0:550e01736339 | 49 | |
aminomar | 0:550e01736339 | 50 | void motorRight_stop(void); |
aminomar | 0:550e01736339 | 51 | void motorRight_cw(void); |
aminomar | 0:550e01736339 | 52 | void motorRight_ccw(void); |
aminomar | 0:550e01736339 | 53 | |
aminomar | 0:550e01736339 | 54 | void moveStop(void); |
aminomar | 0:550e01736339 | 55 | void moveForward(void); |
aminomar | 0:550e01736339 | 56 | void moveBackward(void); |
aminomar | 0:550e01736339 | 57 | void moveLeft(void); |
aminomar | 0:550e01736339 | 58 | void moveRight(void); |
aminomar | 0:550e01736339 | 59 | |
aminomar | 0:550e01736339 | 60 | |
aminomar | 0:550e01736339 | 61 | private: |
aminomar | 0:550e01736339 | 62 | PwmOut pwmLeft; |
aminomar | 0:550e01736339 | 63 | DigitalOut leftIn1; |
aminomar | 0:550e01736339 | 64 | DigitalOut leftIn2; |
aminomar | 0:550e01736339 | 65 | PwmOut pwmRight; |
aminomar | 0:550e01736339 | 66 | DigitalOut rightIn1; |
aminomar | 0:550e01736339 | 67 | DigitalOut rightIn2; |
aminomar | 0:550e01736339 | 68 | DigitalOut nStby; |
aminomar | 0:550e01736339 | 69 | }; |
aminomar | 0:550e01736339 | 70 | #endif |
aminomar | 0:550e01736339 | 71 | |
aminomar | 0:550e01736339 | 72 | /** |
aminomar | 0:550e01736339 | 73 | * #include "mbed.h" |
aminomar | 0:550e01736339 | 74 | * #include "Motorslam.h" |
aminomar | 0:550e01736339 | 75 | * |
aminomar | 0:550e01736339 | 76 | * TB6612FNG motor( p22, p5, p6, |
aminomar | 0:550e01736339 | 77 | * p21, p7, p8, |
aminomar | 0:550e01736339 | 78 | * p18 ); |
aminomar | 0:550e01736339 | 79 | * float fPwmPeriod; |
aminomar | 0:550e01736339 | 80 | * float fPwmPulsewidth; |
aminomar | 0:550e01736339 | 81 | * |
aminomar | 0:550e01736339 | 82 | * int main() |
aminomar | 0:550e01736339 | 83 | * { |
aminomar | 0:550e01736339 | 84 | * fPwmPeriod = 0.001f; // 1KHz |
aminomar | 0:550e01736339 | 85 | * fPwmPulsewidth = 0.50; // 50% duty cycle |
aminomar | 0:550e01736339 | 86 | * motor.setPwmLeftperiod(fPwmPeriod); |
aminomar | 0:550e01736339 | 87 | * motor.setPwmLeftpulsewidth(fPwmPulsewidth); |
aminomar | 0:550e01736339 | 88 | * motor.setPwmRightperiod(fPwmPeriod); |
aminomar | 0:550e01736339 | 89 | * motor.setPwmRightpulsewidth(fPwmPulsewidth); |
aminomar | 0:550e01736339 | 90 | * |
aminomar | 0:550e01736339 | 91 | * while(1) |
aminomar | 0:550e01736339 | 92 | * { |
aminomar | 0:550e01736339 | 93 | * motor.motorLeft_ccw(); |
aminomar | 0:550e01736339 | 94 | * wait(2); |
aminomar | 0:550e01736339 | 95 | * motor.motorLeft_cw(); |
aminomar | 0:550e01736339 | 96 | * wait(2); |
aminomar | 0:550e01736339 | 97 | * motor.motorLeft_stop(); |
aminomar | 0:550e01736339 | 98 | * wait(2); |
aminomar | 0:550e01736339 | 99 | * motor.motorRight_ccw(); |
aminomar | 0:550e01736339 | 100 | * wait(2); |
aminomar | 0:550e01736339 | 101 | * motor.motorRight_cw(); |
aminomar | 0:550e01736339 | 102 | * wait(2); |
aminomar | 0:550e01736339 | 103 | * motor.motorRight_stop(); |
aminomar | 0:550e01736339 | 104 | * wait(2); |
aminomar | 0:550e01736339 | 105 | * } |
aminomar | 0:550e01736339 | 106 | * } |
aminomar | 0:550e01736339 | 107 | */ |
aminomar | 0:550e01736339 | 108 | |
aminomar | 0:550e01736339 | 109 | //OR// |
aminomar | 0:550e01736339 | 110 | |
aminomar | 0:550e01736339 | 111 | /** |
aminomar | 0:550e01736339 | 112 | #include "mbed.h" |
aminomar | 0:550e01736339 | 113 | #include "Motorslam.h" |
aminomar | 0:550e01736339 | 114 | |
aminomar | 0:550e01736339 | 115 | Motorslam motor( p22, p5, p6, |
aminomar | 0:550e01736339 | 116 | p21, p7, p8, |
aminomar | 0:550e01736339 | 117 | p18 ); |
aminomar | 0:550e01736339 | 118 | float fPwmPeriod; |
aminomar | 0:550e01736339 | 119 | float fPwmPulsewidth; |
aminomar | 0:550e01736339 | 120 | |
aminomar | 0:550e01736339 | 121 | void moveStop(void); |
aminomar | 0:550e01736339 | 122 | void moveForward(void); |
aminomar | 0:550e01736339 | 123 | void moveBackward(void); |
aminomar | 0:550e01736339 | 124 | void moveLeft(void); |
aminomar | 0:550e01736339 | 125 | void moveRight(void); |
aminomar | 0:550e01736339 | 126 | |
aminomar | 0:550e01736339 | 127 | int main() |
aminomar | 0:550e01736339 | 128 | { |
aminomar | 0:550e01736339 | 129 | fPwmPeriod = 0.001f; // 1KHz |
aminomar | 0:550e01736339 | 130 | fPwmPulsewidth = 0.50; // 50% duty cycle |
aminomar | 0:550e01736339 | 131 | motor.setPwmLeftperiod(fPwmPeriod); |
aminomar | 0:550e01736339 | 132 | motor.setPwmRightperiod(fPwmPeriod); |
aminomar | 0:550e01736339 | 133 | motor.setPwmLeftpulsewidth(fPwmPulsewidth); |
aminomar | 0:550e01736339 | 134 | motor.setPwmRightpulsewidth(fPwmPulsewidth); |
aminomar | 0:550e01736339 | 135 | |
aminomar | 0:550e01736339 | 136 | while(1) |
aminomar | 0:550e01736339 | 137 | { |
aminomar | 0:550e01736339 | 138 | moveForward(); |
aminomar | 0:550e01736339 | 139 | wait(2); |
aminomar | 0:550e01736339 | 140 | moveBackward(); |
aminomar | 0:550e01736339 | 141 | wait(2); |
aminomar | 0:550e01736339 | 142 | moveStop(); |
aminomar | 0:550e01736339 | 143 | } |
aminomar | 0:550e01736339 | 144 | } |
aminomar | 0:550e01736339 | 145 | |
aminomar | 0:550e01736339 | 146 | void moveStop(void) |
aminomar | 0:550e01736339 | 147 | { |
aminomar | 0:550e01736339 | 148 | motor.standby(); |
aminomar | 0:550e01736339 | 149 | } |
aminomar | 0:550e01736339 | 150 | |
aminomar | 0:550e01736339 | 151 | void moveForward(void) |
aminomar | 0:550e01736339 | 152 | { |
aminomar | 0:550e01736339 | 153 | motor.motorLeft_cw(); |
aminomar | 0:550e01736339 | 154 | motor.motorRight_cw(); |
aminomar | 0:550e01736339 | 155 | } |
aminomar | 0:550e01736339 | 156 | |
aminomar | 0:550e01736339 | 157 | void moveBackward(void) |
aminomar | 0:550e01736339 | 158 | { |
aminomar | 0:550e01736339 | 159 | motor.motorLeft_ccw(); |
aminomar | 0:550e01736339 | 160 | motor.motorRight_ccw(); |
aminomar | 0:550e01736339 | 161 | } |
aminomar | 0:550e01736339 | 162 | void moveLeft(void) |
aminomar | 0:550e01736339 | 163 | { |
aminomar | 0:550e01736339 | 164 | motor.motorLeft_ccw(); |
aminomar | 0:550e01736339 | 165 | motor.motorRight_cw(); |
aminomar | 0:550e01736339 | 166 | } |
aminomar | 0:550e01736339 | 167 | void moveRight(void) |
aminomar | 0:550e01736339 | 168 | { |
aminomar | 0:550e01736339 | 169 | motor.motorLeft_cw(); |
aminomar | 0:550e01736339 | 170 | motor.motorRight_ccw(); |
aminomar | 0:550e01736339 | 171 | } |
aminomar | 0:550e01736339 | 172 | */ |