wpi-32u4-library
Romi32U4Encoders.cpp
Go to the documentation of this file.
1// Copyright Pololu Corporation. For more information, see http://www.pololu.com/
2
3#include <Arduino.h>
4#include <Romi32U4Motors.h>
5#include <FastGPIO.h>
6#include <avr/interrupt.h>
7
8#include <pcint.h>
9
10#include <Chassis.h>
11
12#define LEFT_XOR 8
13#define LEFT_B IO_E2
14#define RIGHT_XOR 7
15#define RIGHT_B 23
16
17// declared here to keep the compiler happy; defined at the bottom
18void leftISR(void);
19void rightISR(void);
20
27{
28 Serial.println("initEncoders()");
29 // Set the pins as pulled-up inputs.
34
35 //attach a PC interrupt
36 attachPCInt(PCINT4, leftISR); // pin change interrupts fire on CHANGE
37
38 // Enable interrupt on PE6 for the right encoder. We use attachInterrupt
39 // instead of defining ISR(INT6_vect) ourselves so that this class will be
40 // compatible with other code that uses attachInterrupt.
41 attachInterrupt(4, rightISR, CHANGE);
42
43 // Initialize the variables. It's good to do this after enabling the
44 // interrupts in case the interrupts fired as we were enabling
45 // them.
46 bool lastLeftB = FastGPIO::Pin<LEFT_B>::isInputHigh();
47 bool lastLeftA = FastGPIO::Pin<LEFT_XOR>::isInputHigh() ^ lastLeftB;
48
49 chassis.leftMotor.handleISR(lastLeftA, lastLeftB);
51
52 bool lastRightB = FastGPIO::Pin<LEFT_B>::isInputHigh();
53 bool lastRightA = FastGPIO::Pin<LEFT_XOR>::isInputHigh() ^ lastRightB;
54
55 chassis.rightMotor.handleISR(lastRightA, lastRightB);
57
58 Serial.println("/initEncoders()");
59}
60
65{
66 cli();
67 int16_t tempCount = count;
68 sei();
69 return tempCount;
70}
71
76{
77 cli();
78 int16_t tempCount = count;
79 count = 0;
80 sei();
81 return tempCount;
82}
83
91{
92 int16_t currCount = count;
93 speed = currCount - prevCount;
94 prevCount = currCount;
95}
96
108void Romi32U4Motor::handleISR(bool newA, bool newB)
109{
110 count += (lastA ^ newB) - (newA ^ lastB);
111
112 lastA = newA;
113 lastB = newB;
114
115 if(ctrlMode == CTRL_POS)
116 {
117 if(count == targetPos)
118 {
119 setEffort(0);
121 }
122 }
123}
124
129{
130 bool newLeftB = FastGPIO::Pin<LEFT_B>::isInputHigh();
131 bool newLeftA = FastGPIO::Pin<LEFT_XOR>::isInputHigh() ^ newLeftB;
132
133 chassis.leftMotor.handleISR(newLeftA, newLeftB);
134}
135
140{
141 bool newRightB = FastGPIO::Pin<RIGHT_B>::isInputHigh();
142 bool newRightA = FastGPIO::Pin<RIGHT_XOR>::isInputHigh() ^ newRightB;
143
144 chassis.rightMotor.handleISR(newRightA, newRightB);
145}
Chassis chassis
void rightISR(void)
void leftISR(void)
LeftMotor leftMotor
Definition: Chassis.h:21
RightMotor rightMotor
Definition: Chassis.h:22
static void setInputPulledUp() __attribute__((always_inline))
Sets a pin to be a digital input with the internal pull-up resistor enabled.
Definition: FastGPIO.h:342
static bool isInputHigh() __attribute__((always_inline))
Reads the input value of the pin.
Definition: FastGPIO.h:352
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 calcEncoderDelta(void)
volatile int16_t prevCount
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 attachPCInt(uint8_t pcInt, void(*pcisr)(void))
Attaches a function to a pin change interrupt.
Definition: pcint.cpp:12