I have started on a new project, I am playing with a platform that has 4 separate fixed motors (one for each wheel) and want to eventually have something that drives itself around. I figure I should start with just controlling the wheels...
I have just learned some OO C++ and want to put it to use.
I have written a basic Motor (or should that be Wheel?) class but I'm a bit confused regarding encoders. Would the interrupt routine be part of the Motor class?
Here's what I have so far...
Any suggestions for improvements and additions is welcomed.
I added the wrong file before, here is the Motor library
#ifndef Motor_h
#define Motor_h
class Motor {
private:
int _pin;
char _speed;
int _currPos;
int _lastPos;
bool _direction;
public:
Motor();
~Motor();
int getPin(); // returns pin number
void setPin(int); // sets pin number
int getSpeed(); // return an int from 0-1023
void setSpeed(char); // values from 0-255
bool getDirection(); // return 1 for forward, 0 for reverse
void setDirection(bool);// set Direction (1 = forwards, 0 = reverse)
int getPosition();
Motor(int pin){ // constructor
setPin(pin);
_speed = 0;
_direction = 1;
}
}; // end Motor class
/*********************************************
* Implemetation of Class methods here
**********************************************/
int Motor::getPin() {
return this->_pin;
}
void Motor::setPin(int pin){
_pin = pin;
pinMode(_pin, OUTPUT);
}
int Motor::getSpeed() {
int vel = analogRead(this->_pin);
return vel;
}
void Motor::setSpeed(char speed) {
_speed = map(this->getPin(),0,1024, 0, 255);
analogWrite(this->_pin, _speed);
}
bool Motor::getDirection() {
return this->_direction;
}
void Motor::setDirection(bool dir) {
_direction = dir;
}
int Motor::getPosition() {
return this->_currPos;
}
#endif