offer some APIs to control the movement of motor. Suitable with Seeed motor shield

Fork of MotorDriver by wei zou

Committer:
lawliet
Date:
Wed Feb 12 03:07:34 2014 +0000
Revision:
1:9c93f514f369
Parent:
0:6bd25809e2e0
update documents

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lawliet 0:6bd25809e2e0 1 /*
lawliet 0:6bd25809e2e0 2 MotorDriver.cpp
lawliet 0:6bd25809e2e0 3 2014 Copyright (c) Seeed Technology Inc. All right reserved.
lawliet 0:6bd25809e2e0 4
lawliet 0:6bd25809e2e0 5 Author:lawliet.zou@gmail.com
lawliet 0:6bd25809e2e0 6 2014-02-11
lawliet 0:6bd25809e2e0 7
lawliet 0:6bd25809e2e0 8 This library is free software; you can redistribute it and/or
lawliet 0:6bd25809e2e0 9 modify it under the terms of the GNU Lesser General Public
lawliet 0:6bd25809e2e0 10 License as published by the Free Software Foundation; either
lawliet 0:6bd25809e2e0 11 version 2.1 of the License, or (at your option) any later version.
lawliet 0:6bd25809e2e0 12
lawliet 0:6bd25809e2e0 13 This library is distributed in the hope that it will be useful,
lawliet 0:6bd25809e2e0 14 but WITHOUT ANY WARRANTY; without even the implied warranty of
lawliet 0:6bd25809e2e0 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
lawliet 0:6bd25809e2e0 16 Lesser General Public License for more details.
lawliet 0:6bd25809e2e0 17
lawliet 0:6bd25809e2e0 18 You should have received a copy of the GNU Lesser General Public
lawliet 0:6bd25809e2e0 19 License along with this library; if not, write to the Free Software
lawliet 0:6bd25809e2e0 20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
lawliet 0:6bd25809e2e0 21 */
lawliet 0:6bd25809e2e0 22
lawliet 0:6bd25809e2e0 23 #include "MotorDriver.h"
lawliet 0:6bd25809e2e0 24
lawliet 0:6bd25809e2e0 25 void MotorDriver::init()
lawliet 0:6bd25809e2e0 26 {
lawliet 0:6bd25809e2e0 27 stop();
lawliet 0:6bd25809e2e0 28 /*Configure the motor A to control the wheel at the left side.*/
lawliet 0:6bd25809e2e0 29 configure(MOTOR_POSITION_LEFT,MOTORA);
lawliet 0:6bd25809e2e0 30 /*Configure the motor B to control the wheel at the right side.*/
lawliet 0:6bd25809e2e0 31 configure(MOTOR_POSITION_RIGHT,MOTORB);
lawliet 0:6bd25809e2e0 32 setSpeed(0,MOTORA);
lawliet 0:6bd25809e2e0 33 setSpeed(0,MOTORB);
lawliet 0:6bd25809e2e0 34 setDirection(MOTOR_ANTICLOCKWISE,MOTORA);
lawliet 0:6bd25809e2e0 35 setDirection(MOTOR_CLOCKWISE,MOTORB);
lawliet 0:6bd25809e2e0 36 }
lawliet 0:6bd25809e2e0 37 void MotorDriver::configure(uint8_t position, uint8_t motorID)
lawliet 0:6bd25809e2e0 38 {
lawliet 0:6bd25809e2e0 39 if(motorID == MOTORA)motorA.position = position;
lawliet 0:6bd25809e2e0 40 else motorB.position = position;
lawliet 0:6bd25809e2e0 41 }
lawliet 0:6bd25809e2e0 42
lawliet 0:6bd25809e2e0 43 void MotorDriver::setSpeed(uint8_t speed, uint8_t motorID)
lawliet 0:6bd25809e2e0 44 {
lawliet 0:6bd25809e2e0 45 if(motorID == MOTORA) motorA.speed = speed;
lawliet 0:6bd25809e2e0 46 else if(motorID == MOTORB) motorB.speed = speed;
lawliet 0:6bd25809e2e0 47 }
lawliet 0:6bd25809e2e0 48 void MotorDriver::setDirection(uint8_t direction, uint8_t motorID)
lawliet 0:6bd25809e2e0 49 {
lawliet 0:6bd25809e2e0 50 if(motorID == MOTORA)motorA.direction= direction;
lawliet 0:6bd25809e2e0 51 else if(motorID == MOTORB)motorB.direction = direction;
lawliet 0:6bd25809e2e0 52 }
lawliet 1:9c93f514f369 53
lawliet 0:6bd25809e2e0 54 void MotorDriver::rotate(uint8_t direction, uint8_t motor_position)
lawliet 0:6bd25809e2e0 55 {
lawliet 0:6bd25809e2e0 56 if(motor_position == motorA.position) {
lawliet 0:6bd25809e2e0 57 rotateWithID(direction,MOTORA);
lawliet 0:6bd25809e2e0 58 }
lawliet 0:6bd25809e2e0 59 if(motor_position == motorB.position) {
lawliet 0:6bd25809e2e0 60 rotateWithID(direction,MOTORB);
lawliet 0:6bd25809e2e0 61 }
lawliet 0:6bd25809e2e0 62 }
lawliet 1:9c93f514f369 63
lawliet 0:6bd25809e2e0 64 void MotorDriver::rotateWithID(uint8_t direction, uint8_t motorID)
lawliet 0:6bd25809e2e0 65 {
lawliet 0:6bd25809e2e0 66 if(motorID == MOTORA) {
lawliet 0:6bd25809e2e0 67 _speedA.Enable(motorA.speed*100,MOTOR_PERIOD);
lawliet 0:6bd25809e2e0 68 _int1 = (MOTOR_CLOCKWISE == direction)?LOW:HIGH;
lawliet 0:6bd25809e2e0 69 _int2 = !_int1;
lawliet 0:6bd25809e2e0 70 } else if(motorID == MOTORB) {
lawliet 0:6bd25809e2e0 71 _speedB.Enable(motorB.speed*100,MOTOR_PERIOD);
lawliet 0:6bd25809e2e0 72 _int3 = (MOTOR_CLOCKWISE == direction)?LOW:HIGH;
lawliet 0:6bd25809e2e0 73 _int4 = !_int3;
lawliet 0:6bd25809e2e0 74 }
lawliet 0:6bd25809e2e0 75 }
lawliet 0:6bd25809e2e0 76
lawliet 0:6bd25809e2e0 77 void MotorDriver::goForward()
lawliet 0:6bd25809e2e0 78 {
lawliet 0:6bd25809e2e0 79 rotate(MOTOR_ANTICLOCKWISE,MOTOR_POSITION_LEFT);
lawliet 0:6bd25809e2e0 80 rotate(MOTOR_CLOCKWISE,MOTOR_POSITION_RIGHT);
lawliet 0:6bd25809e2e0 81 }
lawliet 0:6bd25809e2e0 82 void MotorDriver::goBackward()
lawliet 0:6bd25809e2e0 83 {
lawliet 0:6bd25809e2e0 84 rotate(MOTOR_ANTICLOCKWISE,MOTOR_POSITION_RIGHT);
lawliet 0:6bd25809e2e0 85 rotate(MOTOR_CLOCKWISE,MOTOR_POSITION_LEFT);
lawliet 0:6bd25809e2e0 86 }
lawliet 0:6bd25809e2e0 87 void MotorDriver::goLeft()
lawliet 0:6bd25809e2e0 88 {
lawliet 0:6bd25809e2e0 89 rotate(MOTOR_CLOCKWISE,MOTOR_POSITION_RIGHT);
lawliet 0:6bd25809e2e0 90 rotate(MOTOR_CLOCKWISE,MOTOR_POSITION_LEFT);
lawliet 0:6bd25809e2e0 91 }
lawliet 0:6bd25809e2e0 92 void MotorDriver::goRight()
lawliet 0:6bd25809e2e0 93 {
lawliet 0:6bd25809e2e0 94 rotate(MOTOR_ANTICLOCKWISE,MOTOR_POSITION_RIGHT);
lawliet 0:6bd25809e2e0 95 rotate(MOTOR_ANTICLOCKWISE,MOTOR_POSITION_LEFT);
lawliet 0:6bd25809e2e0 96 }
lawliet 0:6bd25809e2e0 97
lawliet 0:6bd25809e2e0 98 void MotorDriver::stop()
lawliet 0:6bd25809e2e0 99 {
lawliet 0:6bd25809e2e0 100 _speedA.Disable();
lawliet 0:6bd25809e2e0 101 _speedB.Disable();
lawliet 0:6bd25809e2e0 102 }
lawliet 0:6bd25809e2e0 103
lawliet 0:6bd25809e2e0 104 void MotorDriver::stop(uint8_t motorID)
lawliet 0:6bd25809e2e0 105 {
lawliet 0:6bd25809e2e0 106 if(motorID == MOTORA)_speedA.Disable();
lawliet 0:6bd25809e2e0 107 else if(motorID == MOTORB)_speedB.Disable();
lawliet 0:6bd25809e2e0 108 }
lawliet 0:6bd25809e2e0 109