Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "MotorDriver.h"
00024
00025 void MotorDriver::init()
00026 {
00027 stop();
00028
00029 configure(MOTOR_POSITION_LEFT, MOTORA);
00030
00031 configure(MOTOR_POSITION_RIGHT, MOTORB);
00032 setSpeed(0, MOTORA);
00033 setSpeed(0, MOTORB);
00034 setDirection(MOTOR_ANTICLOCKWISE, MOTORA);
00035 setDirection(MOTOR_CLOCKWISE, MOTORB);
00036 }
00037 void MotorDriver::configure(uint8_t position, uint8_t motorID)
00038 {
00039 if (motorID == MOTORA)motorA.position = position;
00040 else motorB.position = position;
00041 }
00042
00043 void MotorDriver::setSpeed(uint8_t speed, uint8_t motorID)
00044 {
00045 if (motorID == MOTORA) motorA.speed = speed;
00046 else if (motorID == MOTORB) motorB.speed = speed;
00047 }
00048 void MotorDriver::setDirection(uint8_t direction, uint8_t motorID)
00049 {
00050 if (motorID == MOTORA)motorA.direction = direction;
00051 else if (motorID == MOTORB)motorB.direction = direction;
00052 }
00053
00054 void MotorDriver::rotate(uint8_t direction, uint8_t motor_position)
00055 {
00056 if (motor_position == motorA.position)
00057 {
00058 rotateWithID(direction, MOTORA);
00059 }
00060 if (motor_position == motorB.position)
00061 {
00062 rotateWithID(direction, MOTORB);
00063 }
00064 }
00065
00066 void MotorDriver::rotateWithID(uint8_t direction, uint8_t motorID)
00067 {
00068 if (motorID == MOTORA)
00069 {
00070 _speedA.Enable(motorA.speed * 100, MOTOR_PERIOD);
00071 _int1 = (MOTOR_CLOCKWISE == direction) ? LOW : HIGH;
00072 _int2 = !_int1;
00073 }
00074 else if (motorID == MOTORB)
00075 {
00076 _speedB.Enable(motorB.speed * 100, MOTOR_PERIOD);
00077 _int3 = (MOTOR_CLOCKWISE == direction) ? LOW : HIGH;
00078 _int4 = !_int3;
00079 }
00080 }
00081
00082 void MotorDriver::goForward()
00083 {
00084 rotate(MOTOR_ANTICLOCKWISE, MOTOR_POSITION_LEFT);
00085 rotate(MOTOR_CLOCKWISE, MOTOR_POSITION_RIGHT);
00086 }
00087 void MotorDriver::goBackward()
00088 {
00089 rotate(MOTOR_ANTICLOCKWISE, MOTOR_POSITION_RIGHT);
00090 rotate(MOTOR_CLOCKWISE, MOTOR_POSITION_LEFT);
00091 }
00092 void MotorDriver::goLeft()
00093 {
00094 rotate(MOTOR_CLOCKWISE, MOTOR_POSITION_RIGHT);
00095 rotate(MOTOR_CLOCKWISE, MOTOR_POSITION_LEFT);
00096 }
00097 void MotorDriver::goRight()
00098 {
00099 rotate(MOTOR_ANTICLOCKWISE, MOTOR_POSITION_RIGHT);
00100 rotate(MOTOR_ANTICLOCKWISE, MOTOR_POSITION_LEFT);
00101 }
00102
00103 void MotorDriver::stop()
00104 {
00105 _speedA.Disable();
00106 _speedB.Disable();
00107 }
00108
00109 void MotorDriver::stop(uint8_t motorID)
00110 {
00111 if (motorID == MOTORA)_speedA.Disable();
00112 else if (motorID == MOTORB)_speedB.Disable();
00113 }
00114