ASMESDC2018
/home/swerve/Github/sdc_2018/omni_robot/Motor.cpp
Go to the documentation of this file.
1 
8 #include "Logger.h"
9 #include "Motor.h"
10 
11 /* CONSTRUCTOR FUNCTIONS */
12 Motor::Motor(int type, int pin_pwm, int pin_a, int pin_b) :
13  _encoder(NO_ENCODER_PIN, NO_ENCODER_PIN) {
14 
15  setupMotor(type, pin_pwm, pin_a, pin_b);
16 
17 }
18 
19 Motor::Motor(int type, int pin_pwm, int pin_a, int pin_b,
20  int encoder_pin_a, int encoder_pin_b) :
21  _encoder(encoder_pin_a, encoder_pin_b) {
22 
23  _encoder.write(0);
24  ENCODER_SETUP = true;
25 
26  setupMotor(type, pin_pwm, pin_a, pin_b);
27 
28 }
29 
30 /* PUBLIC FUNCTIONS */
31 int Motor::setupMotor(int type, int pin_pwm, int pin_a, int pin_b) {
35  // Initialize motor state
36  state.speed = 0; // set initial speed to zero
37  state.direction = MOTOR_FORWARD; // set initial direction to forward
38 
39  // Store motor pins
40  pins.PIN_PWM = pin_pwm;
41 
42  // Setup motor pins
43  pinMode(pins.PIN_PWM, OUTPUT);
44 
45  // Check motor type
46  if ((type < MOTOR_TYPE_MIN) || (type > MOTOR_TYPE_MAX)) {
48  } else {
49  state.type = type;
50  }
51 
52  // Setup motor
53  if (state.type == MOTOR_TYPE_DC) {
54  _setupDCMotor(pin_a, pin_b);
55  } if (state.type == MOTOR_TYPE_BLDC) {
56  _setupBLDCMotor();
57  }
58 
59  ready = true;
60  logger::displayInfo(_getMotorTypeName() + " motor has been setup");
61  return SUCCESS;
62 
63 }
64 
65 void Motor::run(int speed) {
69  if (speed > 0) {
71  } else if (speed < 0) {
73  } else {
75  }
76 
77  if (state.type == MOTOR_TYPE_DC) {
78  return _runDCMotor(speed);
79  } else if (state.type == MOTOR_TYPE_BLDC) {
80  return _runBLDCMotor(speed);
81  } else if (state.type == MOTOR_TYPE_SERVO) {
82  return _runServoMotor(speed);
83  }
84 
85 }
86 
87 long Motor::getPosition(void) {
91  if (!ENCODER_SETUP) {
93  }
94 
95 long int motor_position = _encoder.read(); // get encoder position
96 
97 return motor_position;
98 
99 }
100 
101 /* PUBLIC FUNCTIONS */
102 void Motor::_setupDCMotor(int pin_a, int pin_b) {
103 
104 // Store motor pins
105  pins.PIN_A = pin_a;
106  pins.PIN_B = pin_b;
107 
108 // Setup motor pins
109  pinMode(pins.PIN_A, OUTPUT);
110  pinMode(pins.PIN_B, OUTPUT);
111 
112 }
113 
114 void Motor::_setupBLDCMotor(void) {
115 
116  // Setup BLDC
117  _servo.attach(pins.PIN_PWM, MOTOR_PPM_MIN, MOTOR_PPM_MAX);
118 
119  // Arm ESC's
120  // cli();
121  for (int ppm = 45; ppm < 90; ppm++) {
122  _servo.write(ppm);
123  delay(ESC_ARM_DELAY);
124  }
125  for (int ppm = 90; ppm > 0; ppm--) {
126  _servo.write(ppm);
127  delay(ESC_ARM_DELAY);
128  }
129  for (int ppm = 0; ppm < 45; ppm++) {
130  _servo.write(ppm);
131  delay(ESC_ARM_DELAY);
132  }
133  // sei();
134 
135 state.speed = MOTOR_PPM_OFF; // set initial speed to zero
136 
137 }
138 
139 void Motor::_runDCMotor(int speed) {
140 
141  state.speed = abs(speed);
142 
143  if (state.direction == MOTOR_FORWARD) {
144  digitalWrite(pins.PIN_A, HIGH);
145  digitalWrite(pins.PIN_B, LOW);
146  } else if (state.direction == MOTOR_BRAKE) {
147  digitalWrite(pins.PIN_A, HIGH);
148  digitalWrite(pins.PIN_B, HIGH);
149  } else if (state.direction == MOTOR_REVERSE) {
150  digitalWrite(pins.PIN_A, LOW);
151  digitalWrite(pins.PIN_B, HIGH);
152  }
153 
155  speed = constrain(speed, MOTOR_PWM_MIN, MOTOR_PWM_MAX);
156  analogWrite(pins.PIN_PWM, speed);
157 
158  logger::displayDebug("Running DC motor at speed: " + String(state.speed));
159 
160 }
161 
162 void Motor::_runBLDCMotor(int speed) {
163 
164  _servo.writeMicroseconds(speed);
165 
166 }
167 
168 void Motor::_runServoMotor(int position) {
169 
170  // Constrain position and store position
171  position = max(min(position, MOTOR_PPM_MAX), MOTOR_PPM_MIN);
172  state.speed = position;
173 
174  // Set position of the motor
175  cli();
176  digitalWrite(pins.PIN_PWM, HIGH);
177  delayMicroseconds(state.speed);
178  digitalWrite(pins.PIN_PWM, LOW);
179  sei();
180 
181 }
182 
183 String Motor::_getMotorTypeName(void) {
187  switch (state.type) {
188  case MOTOR_TYPE_DC:
189  return "DC";
190  case MOTOR_TYPE_BLDC:
191  return "Brushless DC";
192  case MOTOR_TYPE_SERVO:
193  return "Servo";
194  }
195 
196  return "";
197 
198 }
199 
200 
#define MOTOR_PPM_MIN
Definition: Motor.h:33
void displayDebug(String)
int void displayInfo(String)
Definition: Logger.cpp:43
void displayInfo(String)
int displayError(int)
Definition: Logger.cpp:36
#define MOTOR_PPM_OFF
Definition: Motor.h:35
#define MOTOR_TYPE_SERVO
Definition: Motor.h:19
long getPosition(void)
Returns the position of the motor.
Definition: Motor.cpp:87
int speed
Definition: Motor.h:43
#define MOTOR_BRAKE
Definition: Motor.h:25
int setupMotor(int, int, int pin_a=0, int pin_b=0)
Sets up the pins of the motor class.
Definition: Motor.cpp:31
Motor(int, int, int pin_a=0, int pin_b=0)
Default constructor.
Definition: Motor.cpp:12
void run(int)
Run the motor at the desired speed and direction or position.
Definition: Motor.cpp:65
#define SUCCESS
Definition: Logger.h:15
#define MOTOR_TYPE_MAX
Definition: Motor.h:22
int type
Definition: Motor.h:42
#define ESC_ARM_DELAY
Definition: Motor.h:37
Header file for Motor class
#define MOTOR_TYPE_MIN
Definition: Motor.h:21
#define ENCODER_SETUP_ERROR
Definition: Logger.h:17
#define MOTOR_TYPE_BLDC
Definition: Motor.h:18
bool ENCODER_SETUP
Definition: Motor.h:103
#define MOTOR_SPEED_MIN
Definition: Motor.h:28
MotorPins pins
Definition: Motor.h:101
#define MOTOR_TYPE_ERROR
Definition: Logger.h:16
#define MOTOR_SPEED_MAX
Definition: Motor.h:29
int displayError(int)
Definition: Logger.cpp:10
bool ready
Definition: Motor.h:56
#define MOTOR_REVERSE
Definition: Motor.h:26
int PIN_A
Definition: Motor.h:49
#define MOTOR_TYPE_DC
Definition: Motor.h:17
Header file for error codes
#define MOTOR_PWM_MIN
Definition: Motor.h:31
#define MOTOR_PPM_MAX
Definition: Motor.h:34
#define NO_ENCODER_PIN
Definition: Motor.h:39
#define MOTOR_FORWARD
Definition: Motor.h:24
int PIN_PWM
Definition: Motor.h:48
#define MOTOR_PWM_MAX
Definition: Motor.h:32
int direction
Definition: Motor.h:44
MotorState state
Definition: Motor.h:100
int PIN_B
Definition: Motor.h:50