wpi-32u4-library
Romi32U4Motors.h
Go to the documentation of this file.
1// Adapted from a library by Pololu Corporation. For more information, see http://www.pololu.com/
2
5#pragma once
6
7#include <Arduino.h>
8#include <stdint.h>
9#include <PIDController.h>
10
26{
27protected:
28 // Used to control the motors in different ways
31
32 // this is the 'speed' of the motor, in "encoder counts / 16 ms interval"
33 volatile int16_t speed = 0;
34
35 // used to set target speed or position (or both)
36 float targetSpeed = 0;
37 int16_t targetPos = 0;
38
39 // Maximum effort (to protect the gear boxes). Can be changed by setting turbo mode
40 int16_t maxEffort = 300;
41
42 // keeps track of encoder changes
43 volatile int16_t prevCount = 0;
44 volatile int16_t count = 0;
45 volatile int16_t lastA = 0;
46 volatile int16_t lastB = 0;
47
48 // We build a PID controller into the object for controlling speed
50
51 friend class Chassis;
52
53public:
54 Romi32U4Motor(void) : pidCtrl(5, 0.5) {} // These values work well for motor control
55
59 static void init()
60 {
61 initMotors();
63 }
64
65 void setPIDCoeffients(float kp, float ki)
66 {
67 pidCtrl.setKp(kp);
68 pidCtrl.setKi(ki);
69 }
70
71protected:
72 // Used to set up motors and encoders. Do not call directly; they are called from init(), which
73 // is called from Chassis::init()
74 static void initMotors();
75 static void initEncoders();
76
83 virtual void setEffort(int16_t effort) = 0;
84
93 int16_t getCount(void);
94 int16_t getAndResetCount(void);
95
96 void setTargetSpeed(float targetSpeed);
97 void moveFor(int16_t amount);
98 bool checkComplete(void) {return ctrlMode == CTRL_DIRECT;}
99
100 void update(void);
101 void calcEncoderDelta(void);
102
103public:
117 void allowTurbo(bool turbo);
118
122 inline void handleISR(bool newA, bool newB);
123};
124
131{
132protected:
133 void setEffort(int16_t effort);
134
135public:
137 void setMotorEffort(int16_t effort)
138 {
140 setEffort(effort);
141 }
142};
143
150{
151protected:
152 void setEffort(int16_t effort);
153
154public:
156 void setMotorEffort(int16_t effort)
157 {
159 setEffort(effort);
160 }
161};
void setEffort(int16_t effort)
void setMotorEffort(int16_t effort)
A generic PID controller.
Definition: PIDcontroller.h:10
float setKp(float k)
Definition: PIDcontroller.h:30
float setKi(float k)
Definition: PIDcontroller.h:31
void setEffort(int16_t effort)
Sets the effort for the motor directly. Overloaded for the left and right motors. Use Chassis::setEff...
void setMotorEffort(int16_t effort)
void setTargetSpeed(float targetSpeed)
PIDController pidCtrl
int16_t maxEffort
int16_t targetPos
volatile CTRL_MODE ctrlMode
int16_t getAndResetCount(void)
int16_t getCount(void)
volatile int16_t lastA
void handleISR(bool newA, bool newB)
static void initEncoders()
void allowTurbo(bool turbo)
Turns turbo mode on or off.
void calcEncoderDelta(void)
volatile int16_t prevCount
bool checkComplete(void)
volatile int16_t count
volatile int16_t speed
virtual void setEffort(int16_t effort)=0
Sets the effort for the motor directly. Overloaded for the left and right motors. Use Chassis::setEff...
volatile int16_t lastB
void setPIDCoeffients(float kp, float ki)
static void init()
void update(void)
void moveFor(int16_t amount)
static void initMotors()