Setting and Monitoring Encoders


The command for setting and monitoring encoders is enc.

The related commands are setting count direction, setting counter value, monitoring counter values and monitoring periods.

Setting count direction

The command to set the count direction is set pol.

"dc1 enc set pol (polarity)"

Specify the count direction (+ or -) for polarity. The default value is +.

For an encoder with one sensor, specify 0 for polarity. In this case, the value of pos always increases.

Direction Direction of rotation Counter value
+ forward increase
+ reverse decrease
- forward decrease
- reverse increase
0 forward increase
0 reverse increase
spc_request_dev($sid, "dc1 enc set pol +");
spc_request_dev($sid, "dc1 enc set pol -");

Setting counter values

The command to set the encoder counter value is set pos.

"dc1 enc set pos (value)"

Specify the counter value for value.

You can set the counter value from -1000000000 (-1 billion) to +1000000000 (1 billion).

spc_request_dev($sid, "dc1 enc set pos -5000");
spc_request_dev($sid, "dc1 enc set pos 3000");

Setting sampling count

The command to set the encoder sampling count is set psr.

"dc1 enc set psr (value)"

Specify a sampling count to value.

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.

spc_request_dev($sid, "dc1 enc set psr 16");

Monitoring counter values of encoders

The command to monitor counter values of encoders is get pos.

"dc1 enc get pos"

<?php
include "/lib/sd_spc.php";
spc_reset();
spc_sync_baud(115200);
$sid = 1;

spc_request_dev($sid, "dc1 pwm set period 10000");
spc_request_dev($sid, "dc1 pwm set width 1000");

while(1)
{
    $count = spc_request_dev($sid, "dc1 enc get pos");
    echo "$count\r\n";
}
?>

Monitoring periods of encoder outputs.

The command to monitor periods of encoder outputs is get period.

"dc1 enc get period"

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.

<?php
include "/lib/sd_spc.php";
spc_reset();
spc_sync_baud(115200);
$sid = 1;

spc_request_dev($sid, "dc1 pwm set period 10000");
spc_request_dev($sid, "dc1 pwm set width 1000");
spc_request_dev($sid, "dc1 enc set psr 4");

while(1)
{
    $count = spc_request_dev($sid, "dc1 enc get period");
    echo "$count\r\n";
}
?>