Create this program

Dependencies:   mbed HCSR04 HMC6352 PID TextLCD

Committer:
KoiShin_Sakana
Date:
Fri Aug 07 06:34:36 2015 +0000
Revision:
3:901d18b901b4
Parent:
2:a8bbf677e774
Child:
5:e07e380ddb93
update communication.h and infrared.h; create dribbler.h, kicker.h and line.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 3:901d18b901b4 16 ~Motor();
KoiShin_Sakana 2:a8bbf677e774 17 int setLimit(float powerLimit = 1);
KoiShin_Sakana 2:a8bbf677e774 18 void setFlequency(float correctFlequency);
KoiShin_Sakana 2:a8bbf677e774 19 void run(float power); // input -1 ~ 1
KoiShin_Sakana 2:a8bbf677e774 20 void brake(void);
KoiShin_Sakana 2:a8bbf677e774 21
KoiShin_Sakana 2:a8bbf677e774 22 private:
KoiShin_Sakana 2:a8bbf677e774 23 float limit;
KoiShin_Sakana 2:a8bbf677e774 24 PwmOut* powerLevel;
KoiShin_Sakana 2:a8bbf677e774 25 DigitalOut normal;
KoiShin_Sakana 2:a8bbf677e774 26 DigitalOut reverse;
KoiShin_Sakana 2:a8bbf677e774 27 void rotateNormal(void);
KoiShin_Sakana 2:a8bbf677e774 28 void rotateReverse(void);
KoiShin_Sakana 2:a8bbf677e774 29 };
KoiShin_Sakana 2:a8bbf677e774 30
KoiShin_Sakana 2:a8bbf677e774 31 // initialize
KoiShin_Sakana 2:a8bbf677e774 32 Motor::Motor(PinName normalDirect, PinName reverseDirect, PinName pwm):
KoiShin_Sakana 2:a8bbf677e774 33 normal(normalDirect), reverse(reverseDirect) {
KoiShin_Sakana 2:a8bbf677e774 34 powerLevel = new PwmOut(pwm);
KoiShin_Sakana 2:a8bbf677e774 35 }
KoiShin_Sakana 2:a8bbf677e774 36
KoiShin_Sakana 3:901d18b901b4 37 Motor::~Motor() {
KoiShin_Sakana 3:901d18b901b4 38 delete powerLevel;
KoiShin_Sakana 3:901d18b901b4 39 }
KoiShin_Sakana 3:901d18b901b4 40
KoiShin_Sakana 2:a8bbf677e774 41 // set flequency
KoiShin_Sakana 2:a8bbf677e774 42 void Motor::setFlequency(float correctFlequency) {
KoiShin_Sakana 2:a8bbf677e774 43 powerLevel->period(1 / correctFlequency);
KoiShin_Sakana 2:a8bbf677e774 44 }
KoiShin_Sakana 2:a8bbf677e774 45
KoiShin_Sakana 2:a8bbf677e774 46 // set power limit
KoiShin_Sakana 2:a8bbf677e774 47 int Motor::setLimit(float powerLimit) {
KoiShin_Sakana 2:a8bbf677e774 48 if (powerLimit > 1 || powerLimit < -1) return 1;
KoiShin_Sakana 2:a8bbf677e774 49
KoiShin_Sakana 2:a8bbf677e774 50 limit = powerLimit;
KoiShin_Sakana 2:a8bbf677e774 51 return 0;
KoiShin_Sakana 2:a8bbf677e774 52 }
KoiShin_Sakana 2:a8bbf677e774 53
KoiShin_Sakana 2:a8bbf677e774 54 // run moter
KoiShin_Sakana 2:a8bbf677e774 55 void Motor::run(float power) {
KoiShin_Sakana 2:a8bbf677e774 56 if (power > 1) power = 1;
KoiShin_Sakana 2:a8bbf677e774 57 if (power < -1) power = -1;
KoiShin_Sakana 2:a8bbf677e774 58 if (power < 0) {
KoiShin_Sakana 2:a8bbf677e774 59 powerLevel->write(-power * limit);
KoiShin_Sakana 2:a8bbf677e774 60 rotateReverse();
KoiShin_Sakana 2:a8bbf677e774 61 } else {
KoiShin_Sakana 2:a8bbf677e774 62 powerLevel->write(power * limit);
KoiShin_Sakana 2:a8bbf677e774 63 rotateNormal();
KoiShin_Sakana 2:a8bbf677e774 64 }
KoiShin_Sakana 2:a8bbf677e774 65 }
KoiShin_Sakana 2:a8bbf677e774 66
KoiShin_Sakana 2:a8bbf677e774 67 // rotate in the clockwise
KoiShin_Sakana 2:a8bbf677e774 68 void Motor::rotateNormal(void) {
KoiShin_Sakana 2:a8bbf677e774 69 normal = 1;
KoiShin_Sakana 2:a8bbf677e774 70 reverse = 0;
KoiShin_Sakana 2:a8bbf677e774 71 }
KoiShin_Sakana 2:a8bbf677e774 72
KoiShin_Sakana 2:a8bbf677e774 73 // rotate in the anticlockwise
KoiShin_Sakana 2:a8bbf677e774 74 void Motor::rotateReverse(void) {
KoiShin_Sakana 2:a8bbf677e774 75 normal = 0;
KoiShin_Sakana 2:a8bbf677e774 76 reverse = 1;
KoiShin_Sakana 2:a8bbf677e774 77 }
KoiShin_Sakana 2:a8bbf677e774 78
KoiShin_Sakana 2:a8bbf677e774 79 // brake
KoiShin_Sakana 2:a8bbf677e774 80 void Motor::brake(void) {
KoiShin_Sakana 2:a8bbf677e774 81 normal = 1;
KoiShin_Sakana 2:a8bbf677e774 82 reverse = 1;
KoiShin_Sakana 2:a8bbf677e774 83 powerLevel->write(limit);
KoiShin_Sakana 2:a8bbf677e774 84 }
KoiShin_Sakana 2:a8bbf677e774 85
KoiShin_Sakana 2:a8bbf677e774 86 #endif
KoiShin_Sakana 2:a8bbf677e774 87
KoiShin_Sakana 2:a8bbf677e774 88 /*
KoiShin_Sakana 2:a8bbf677e774 89 * example program
KoiShin_Sakana 2:a8bbf677e774 90
KoiShin_Sakana 2:a8bbf677e774 91 #include "mbed.h"
KoiShin_Sakana 2:a8bbf677e774 92 #include "motor.h"
KoiShin_Sakana 2:a8bbf677e774 93
KoiShin_Sakana 2:a8bbf677e774 94 int main(void) {
KoiShin_Sakana 2:a8bbf677e774 95 Motor motor(D4, D7, D9);
KoiShin_Sakana 2:a8bbf677e774 96 motor.setLimit(0.8);
KoiShin_Sakana 2:a8bbf677e774 97 while (1) {
KoiShin_Sakana 2:a8bbf677e774 98 motor.run(-0.2); // Then D4->Low D7->High D9(duty) = 0.2 * 0.8 = 0.16
KoiShin_Sakana 2:a8bbf677e774 99 motor.run(2); // Then D4->High D7->Low D9(duty) = 1.0 * 0.8 = 0.8
KoiShin_Sakana 2:a8bbf677e774 100 }
KoiShin_Sakana 2:a8bbf677e774 101 }
KoiShin_Sakana 2:a8bbf677e774 102 */
KoiShin_Sakana 2:a8bbf677e774 103