Create this program

Dependencies:   mbed HCSR04 HMC6352 PID TextLCD

Committer:
KoiShin_Sakana
Date:
Thu Aug 06 00:19:29 2015 +0000
Revision:
2:a8bbf677e774
Child:
3:901d18b901b4
create communication.h, control.h, infrared.h, main.cpp, pinDef.h and ultrasonic.h; update compass.h and motor.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
KoiShin_Sakana 2:a8bbf677e774 1 /**
KoiShin_Sakana 2:a8bbf677e774 2 * @file : motor.h (1.0)
KoiShin_Sakana 2:a8bbf677e774 3 * @brief : control moter
KoiShin_Sakana 2:a8bbf677e774 4 * @author : Yohei SAITO(, Shinnosuke KOIKE)
KoiShin_Sakana 2:a8bbf677e774 5 * @date : 2015/08/04
KoiShin_Sakana 2:a8bbf677e774 6 */
KoiShin_Sakana 2:a8bbf677e774 7
KoiShin_Sakana 2:a8bbf677e774 8 #ifndef MOTOR_H
KoiShin_Sakana 2:a8bbf677e774 9 #define MOTOR_H
KoiShin_Sakana 2:a8bbf677e774 10
KoiShin_Sakana 2:a8bbf677e774 11 #include "mbed.h"
KoiShin_Sakana 2:a8bbf677e774 12
KoiShin_Sakana 2:a8bbf677e774 13 class Motor {
KoiShin_Sakana 2:a8bbf677e774 14 public:
KoiShin_Sakana 2:a8bbf677e774 15 Motor(PinName normalDirect, PinName reverseDirect, PinName pwm);
KoiShin_Sakana 2:a8bbf677e774 16 int setLimit(float powerLimit = 1);
KoiShin_Sakana 2:a8bbf677e774 17 void setFlequency(float correctFlequency);
KoiShin_Sakana 2:a8bbf677e774 18 void run(float power); // input -1 ~ 1
KoiShin_Sakana 2:a8bbf677e774 19 void brake(void);
KoiShin_Sakana 2:a8bbf677e774 20
KoiShin_Sakana 2:a8bbf677e774 21 private:
KoiShin_Sakana 2:a8bbf677e774 22 float limit;
KoiShin_Sakana 2:a8bbf677e774 23 PwmOut* powerLevel;
KoiShin_Sakana 2:a8bbf677e774 24 DigitalOut normal;
KoiShin_Sakana 2:a8bbf677e774 25 DigitalOut reverse;
KoiShin_Sakana 2:a8bbf677e774 26 void rotateNormal(void);
KoiShin_Sakana 2:a8bbf677e774 27 void rotateReverse(void);
KoiShin_Sakana 2:a8bbf677e774 28 };
KoiShin_Sakana 2:a8bbf677e774 29
KoiShin_Sakana 2:a8bbf677e774 30 // initialize
KoiShin_Sakana 2:a8bbf677e774 31 Motor::Motor(PinName normalDirect, PinName reverseDirect, PinName pwm):
KoiShin_Sakana 2:a8bbf677e774 32 normal(normalDirect), reverse(reverseDirect) {
KoiShin_Sakana 2:a8bbf677e774 33 powerLevel = new PwmOut(pwm);
KoiShin_Sakana 2:a8bbf677e774 34 }
KoiShin_Sakana 2:a8bbf677e774 35
KoiShin_Sakana 2:a8bbf677e774 36 // set flequency
KoiShin_Sakana 2:a8bbf677e774 37 void Motor::setFlequency(float correctFlequency) {
KoiShin_Sakana 2:a8bbf677e774 38 powerLevel->period(1 / correctFlequency);
KoiShin_Sakana 2:a8bbf677e774 39 }
KoiShin_Sakana 2:a8bbf677e774 40
KoiShin_Sakana 2:a8bbf677e774 41 // set power limit
KoiShin_Sakana 2:a8bbf677e774 42 int Motor::setLimit(float powerLimit) {
KoiShin_Sakana 2:a8bbf677e774 43 if (powerLimit > 1 || powerLimit < -1) return 1;
KoiShin_Sakana 2:a8bbf677e774 44
KoiShin_Sakana 2:a8bbf677e774 45 limit = powerLimit;
KoiShin_Sakana 2:a8bbf677e774 46 return 0;
KoiShin_Sakana 2:a8bbf677e774 47 }
KoiShin_Sakana 2:a8bbf677e774 48
KoiShin_Sakana 2:a8bbf677e774 49 // run moter
KoiShin_Sakana 2:a8bbf677e774 50 void Motor::run(float power) {
KoiShin_Sakana 2:a8bbf677e774 51 if (power > 1) power = 1;
KoiShin_Sakana 2:a8bbf677e774 52 if (power < -1) power = -1;
KoiShin_Sakana 2:a8bbf677e774 53 if (power < 0) {
KoiShin_Sakana 2:a8bbf677e774 54 powerLevel->write(-power * limit);
KoiShin_Sakana 2:a8bbf677e774 55 rotateReverse();
KoiShin_Sakana 2:a8bbf677e774 56 } else {
KoiShin_Sakana 2:a8bbf677e774 57 powerLevel->write(power * limit);
KoiShin_Sakana 2:a8bbf677e774 58 rotateNormal();
KoiShin_Sakana 2:a8bbf677e774 59 }
KoiShin_Sakana 2:a8bbf677e774 60 }
KoiShin_Sakana 2:a8bbf677e774 61
KoiShin_Sakana 2:a8bbf677e774 62 // rotate in the clockwise
KoiShin_Sakana 2:a8bbf677e774 63 void Motor::rotateNormal(void) {
KoiShin_Sakana 2:a8bbf677e774 64 normal = 1;
KoiShin_Sakana 2:a8bbf677e774 65 reverse = 0;
KoiShin_Sakana 2:a8bbf677e774 66 }
KoiShin_Sakana 2:a8bbf677e774 67
KoiShin_Sakana 2:a8bbf677e774 68 // rotate in the anticlockwise
KoiShin_Sakana 2:a8bbf677e774 69 void Motor::rotateReverse(void) {
KoiShin_Sakana 2:a8bbf677e774 70 normal = 0;
KoiShin_Sakana 2:a8bbf677e774 71 reverse = 1;
KoiShin_Sakana 2:a8bbf677e774 72 }
KoiShin_Sakana 2:a8bbf677e774 73
KoiShin_Sakana 2:a8bbf677e774 74 // brake
KoiShin_Sakana 2:a8bbf677e774 75 void Motor::brake(void) {
KoiShin_Sakana 2:a8bbf677e774 76 normal = 1;
KoiShin_Sakana 2:a8bbf677e774 77 reverse = 1;
KoiShin_Sakana 2:a8bbf677e774 78 powerLevel->write(limit);
KoiShin_Sakana 2:a8bbf677e774 79 }
KoiShin_Sakana 2:a8bbf677e774 80
KoiShin_Sakana 2:a8bbf677e774 81 #endif
KoiShin_Sakana 2:a8bbf677e774 82
KoiShin_Sakana 2:a8bbf677e774 83 /*
KoiShin_Sakana 2:a8bbf677e774 84 * example program
KoiShin_Sakana 2:a8bbf677e774 85
KoiShin_Sakana 2:a8bbf677e774 86 #include "mbed.h"
KoiShin_Sakana 2:a8bbf677e774 87 #include "motor.h"
KoiShin_Sakana 2:a8bbf677e774 88
KoiShin_Sakana 2:a8bbf677e774 89 int main(void) {
KoiShin_Sakana 2:a8bbf677e774 90 Motor motor(D4, D7, D9);
KoiShin_Sakana 2:a8bbf677e774 91 motor.setLimit(0.8);
KoiShin_Sakana 2:a8bbf677e774 92 while (1) {
KoiShin_Sakana 2:a8bbf677e774 93 motor.run(-0.2); // Then D4->Low D7->High D9(duty) = 0.2 * 0.8 = 0.16
KoiShin_Sakana 2:a8bbf677e774 94 motor.run(2); // Then D4->High D7->Low D9(duty) = 1.0 * 0.8 = 0.8
KoiShin_Sakana 2:a8bbf677e774 95 }
KoiShin_Sakana 2:a8bbf677e774 96 }
KoiShin_Sakana 2:a8bbf677e774 97 */
KoiShin_Sakana 2:a8bbf677e774 98