Setting and Monitoring Encoders


Major Functions for an encoder

Monitoring counter values of encoders

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);
}

Monitoring periods of encoder outputs.

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);
}

Other Functions

Setting count direction

Set count direction of encoder by using setEncoderPolarity() function.

dcmotor.setEncoderPolarity(pol);

Setting a counter value

Set or initialize counter values of encoder by using setEncoderPosition() function.

dcmotor.setEncoderPosition(pos);

Setting a sampling count

Set a sampling count of encoder by using setEncoderPSR() function.

dcmotor.setEncoderPSR(psr);