Motor Class
The motor class controls the four DC motors that were used in this project. It was designed to work with the Adafruit Motor Shield
.cpp Code
#include "motor.h"
#include <Adafruit_MotorShield.h> //wrapper library
motor :: motor (Adafruit_MotorShield*shieldPtr, byte motornumber, boolean polarity){
m_motornumber = motornumber; //changes the motor number, based on what number is entered
m_polarity = polarity; //polarity check, negative and positive leads connection
m_motor = shieldPtr -> getMotor(motornumber); //a pointer using the adafruit motorshield library
m_command =0;
m_dutyCycle =0;
}
void motor :: setDrive(unsigned int dutyCycle, unsigned int command){
m_dutyCycle = dutyCycle; //PWM that sets the speed of the motor
m_command = command; //command used for FORWARD,BACKWARD,or RELEASE
if(command == RELEASE)
m_motor -> run(RELEASE);
else if((m_polarity && m_command == FORWARD)||(!m_polarity && m_command == BACKWARD))
m_motor->run(FORWARD); //checks the polarity and sets the wheel to move accordingly
else
m_motor->run(BACKWARD);
m_motor ->setSpeed(m_dutyCycle);
}
unsigned int Motor :: getDrive(){
return(m_dutyCycle); //whats the speed from 0-255
}
void Motor::stop(){
m_motor->setSpeed(0); //stop the wheels
}
.h Code
#include <Adafruit_MotorShield.h>
#ifndef motor_h //if this library isn't included already, include
#define motor_h
class motor{
private:
byte m_motornumber;
boolean m_polarity;
Adafruit_DCMotor*m_motor; //accesses the Adafruit library with a pointer
unsigned int m_command;
unsigned int m_dutyCycle;
public:
motor(Adafruit_MotorShield*shieldPtr,byte motorNumber, boolean polarity);
void setDrive(unsigned int drive, unsigned int command);
void stop();
unsigned int getDrive();
};
#endif