Connect a motor's encoder to an encoder port of this board and monitor counter values by using getEncoderPosition() function.
dcmotor.getEncoderPosition();
#include <PhpocExpansion.h>
#include <Phpoc.h>
byte spcId = 1;
ExpansionDCMotor dcmotor(spcId, 1);
void setup() {
Serial.begin(9600);
while(!Serial)
;
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
Expansion.begin();
Serial.println(dcmotor.getPID());
Serial.println(dcmotor.getName());
dcmotor.setPeriod(10000);
dcmotor.setWidth(3000);
}
void loop() {
// get the counter value of an encoder
Serial.println(dcmotor.getEncoderPosition());
delay(100);
}
Monitor periods of encoder outputs by using getEncoderPeriod() function.
period = dcmotor.getEncoderPeriod();
The unit of the return values is is microseconds (us). To reduce the error when monitoring periods of encoder outputs, set the encoder sampling count to a large value.
#include <PhpocExpansion.h>
#include <Phpoc.h>
byte spcId = 1;
ExpansionDCMotor dcmotor(spcId, 1);
int width = 3000;
void setup() {
Serial.begin(9600);
while(!Serial)
;
Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
Expansion.begin();
Serial.println(dcmotor.getPID());
Serial.println(dcmotor.getName());
dcmotor.setEncoderPSR(64);
dcmotor.setPeriod(10000);
dcmotor.setWidth(width);
}
void loop() {
if(width > 1000) {
width -= 100;
dcmotor.setWidth(width);
delay(500);
Serial.print(width);
Serial.print("=>");
// get the period of an encoder's output
Serial.println(dcmotor.getEncoderPeriod());
}
else
dcmotor.setWidth(0);
}
Set count direction of encoder by using setEncoderPolarity() function.
dcmotor.setEncoderPolarity(pol);
pol - count direction
pol | polarity |
---|---|
0 or greater than 0 | normal(default) |
less than 0 | reverse |
※ Note : The count direction of encoder is affected by motor's direction of rotation.
count direction of encoder | motor's direction of rotation | count value |
---|---|---|
normal | clockwise | increase |
normal | counterclockwise | decrease |
reverse | counterclockwise | increase |
reverse | clockwise | decrease |
Set or initialize counter values of encoder by using setEncoderPosition() function.
dcmotor.setEncoderPosition(pos);
pos - a counter value of encoder
You can set the counter value from -1000000000 (-1 billion) to +1000000000 (1 billion).
Set a sampling count of encoder by using setEncoderPSR() function.
dcmotor.setEncoderPSR(psr);
psr - sampling count
The encoder sampling count is the number of pulses used to measure the period of the encoder output pulse. The more sampling counts, the smaller the error. The encoder sampling count can be set from 1 to 64.