wayneandlayne
New member
Hello everyone,
I have been trying to integrate the PJRC Encoder class inside my own motor control class library, but I've been having some troubles, probably related to interrupts or volatile variables or atomic access of multi-byte variables or something like that. This is with stock Arduino 1.0.5 on Ubuntu 12.04, using a LEGO Mindstorms NXT motor as the encoder inputs.
Code posted here: https://github.com/matthewbeckler/EncoderWrapper
EncoderWrapper.h
EncoderWrapper.cpp
You can see that this class is very simple. It has a local instance of Encoder (_encoder), which is initialized in the initializer list of the constructor. The getPosition() function is a single-line wrapper around Encoder's read() function.
Here is a quick sketch to demonstrate the issue:
When I run the sketch with an encoder connected to pins 3 and 4 on the Arduino, the second Serial.println() statement prints the correct position value from the Encoder instance. The first Serial.println() statement prints garbage that seems to increment over time? Here's some sample output:
So, that's pretty weird. I'm not sure why using the wrapper function produces garbage data, but calling the read() function directly works perfectly. Does anyone have any idea?
Thanks much,
Matthew Beckler
Wayne and Layne, LLC
I have been trying to integrate the PJRC Encoder class inside my own motor control class library, but I've been having some troubles, probably related to interrupts or volatile variables or atomic access of multi-byte variables or something like that. This is with stock Arduino 1.0.5 on Ubuntu 12.04, using a LEGO Mindstorms NXT motor as the encoder inputs.
Code posted here: https://github.com/matthewbeckler/EncoderWrapper
EncoderWrapper.h
Code:
// Arduino header files
#include <stdint.h>
#include "Arduino.h"
// Library header files
#include <Encoder.h>
class EncoderWrapper
{
public:
EncoderWrapper(uint8_t pin1, uint8_t pin2);
// Read the encoder's current position.
int32_t getPosition(void);
// Write the encoder's current position
void setPosition(int32_t pos);
// Tracks the position of the motor
Encoder _encoder;
};
EncoderWrapper.cpp
Code:
#include "EncoderWrapper.h"
EncoderWrapper::EncoderWrapper(uint8_t pin1, uint8_t pin2):
_encoder(pin1, pin2)
{
// Nothing to do here
}
// Read the encoder's current position.
int32_t EncoderWrapper::getPosition(void)
{
return _encoder.read();
}
// Write the encoder's current position
void EncoderWrapper::setPosition(int32_t pos)
{
_encoder.write(pos);
}
You can see that this class is very simple. It has a local instance of Encoder (_encoder), which is initialized in the initializer list of the constructor. The getPosition() function is a single-line wrapper around Encoder's read() function.
Here is a quick sketch to demonstrate the issue:
Code:
#include <Encoder.h>
#include <EncoderWrapper.h>
EncoderWrapper w = EncoderWrapper(3, 4);
void setup(void)
{
Serial.begin(115200);
}
void loop(void)
{
Serial.println("-----------------------------");
Serial.println(w.getPosition(), HEX);
Serial.println(w._encoder.read(), HEX);
delay(100);
}
When I run the sketch with an encoder connected to pins 3 and 4 on the Arduino, the second Serial.println() statement prints the correct position value from the Encoder instance. The first Serial.println() statement prints garbage that seems to increment over time? Here's some sample output:
Code:
-----------------------------
620154
0
-----------------------------
C50154
0
-----------------------------
1270154
0
-----------------------------
12E
1
-----------------------------
1EC0154
C
-----------------------------
24E0154
24
-----------------------------
12E
3F
-----------------------------
12E
5B
-----------------------------
3760154
76
-----------------------------
3D80154
92
-----------------------------
12E
AD
-----------------------------
49D0154
C4
-----------------------------
5000154
D4
-----------------------------
5620154
D8
-----------------------------
5C50154
CD
-----------------------------
12E
B6
-----------------------------
68A0154
93
-----------------------------
12E
6A
-----------------------------
12E
40
-----------------------------
12E
1A
-----------------------------
12E
FFFFFFF8
-----------------------------
12E
FFFFFFD8
-----------------------------
12E
FFFFFFBC
-----------------------------
12E
FFFFFFA4
-----------------------------
12E
FFFFFF94
-----------------------------
12E
FFFFFF8E
So, that's pretty weird. I'm not sure why using the wrapper function produces garbage data, but calling the read() function directly works perfectly. Does anyone have any idea?
Thanks much,
Matthew Beckler
Wayne and Layne, LLC