wpi-32u4-library
Romi32U4Motors.cpp
Go to the documentation of this file.
1// Adapted from a library by Pololu Corporation. For more information, see http://www.pololu.com/
2
3#include <Romi32U4Motors.h>
4#include <FastGPIO.h>
5#include <avr/io.h>
6
7// define the motor pins here
8#define PWM_L 10
9#define PWM_R 9
10#define DIR_L 16
11#define DIR_R 15
12
13#define IR_EMITTER_PIN 11 // this should go elsewhere or disappear
14
29{
30 Serial.println("initMotors()");
31
36
38
39 noInterrupts(); //disable interupts while we set Timer1 registers
40
41 TCCR1A = 0xAA; //0b10101010; //Fast PWM + outputs enabled
42 TCCR1B = 0x19; //0b00011001; //Fast PWM
43 ICR1 = 420; //runs at 38kHz; lowers speed for given effort by 5% from Pololu version
44
45 //set all three outputs to zero
46 OCR1A = 0;
47 OCR1B = 0;
48 OCR1C = 0; //can be used to create 38 kHz signal on pin 11
49
50 interrupts(); //re-enable interrupts
51
52 Serial.println("/initMotors()");
53}
54
62void LeftMotor::setEffort(int16_t effort)
63{
64 bool reverse = 0;
65
66 if (effort < 0)
67 {
68 effort = -effort; // Make speed a positive quantity.
69 reverse = 1; // Preserve the direction.
70 }
71 if (effort > maxEffort)
72 {
73 effort = maxEffort;
74 }
75
76 OCR1B = effort;
78}
79
80void RightMotor::setEffort(int16_t effort)
81{
82 bool reverse = 0;
83
84 if (effort < 0)
85 {
86 effort = -effort; // Make speed a positive quantity.
87 reverse = 1; // Preserve the direction.
88 }
89 if (effort > maxEffort)
90 {
91 effort = maxEffort;
92 }
93
94 OCR1A = effort;
96}
97
102{
103 maxEffort = turbo ? 400 : 300;
104}
105
110{
112 {
113 int16_t effort = pidCtrl.calcEffort(targetSpeed - speed);
114 setEffort(effort);
115 }
116}
117
122{
123 targetSpeed = target;
124
125 if(ctrlMode != CTRL_SPEED)
126 {
127 // Reset the error integral if we are switching from another mode
128 // Otherwise, the robot may jump due to residual integral
130 }
131
133}
134
139void Romi32U4Motor::moveFor(int16_t amount)
140{
141 cli();
142 int16_t currPos = count;
143 sei();
144
145 targetPos = currPos + amount;
147}
static void setOutputLow() __attribute__((always_inline))
Configures the pin to be an output driving low.
Definition: FastGPIO.h:226
static void setOutput(bool value) __attribute__((always_inline))
Sets the pin as an output.
Definition: FastGPIO.h:260
void setEffort(int16_t effort)
void resetSum(void)
Definition: PIDcontroller.h:34
float calcEffort(float error)
Used to calculate the effort from the error.
void setEffort(int16_t effort)
Sets the effort for the motor directly. Overloaded for the left and right motors. Use Chassis::setEff...
void setTargetSpeed(float targetSpeed)
PIDController pidCtrl
int16_t maxEffort
int16_t targetPos
volatile CTRL_MODE ctrlMode
void allowTurbo(bool turbo)
Turns turbo mode on or off.
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...
void update(void)
void moveFor(int16_t amount)
static void initMotors()