Controlling Motors and Setting PWM


The command for controlling motors and setting PWM is pwm.

The related commands are setting polarity, setting direction of rotation, setting period and HIGH duration of PWM.

Setting PWM polarity

The command to set the PWM polarity is set pol.

"dc1 pwm set pol (polarity)"

Specify the polarity (+ or -) for polarity. The default value is +. When set to -, the polarity is reversed.

spc_request_dev($sid, "dc1 pwm set pol +"); // normal polarity
spc_request_dev($sid, "dc1 pwm set pol -"); // reverse plarity

Setting the direction of rotation

The command to set the direction of rotation is set dir.

"dc1 pwm set dir (direction)"

spc_request_dev($sid, "dc1 pwm set dir +"); // forward
spc_request_dev($sid, "dc1 pwm set dir -"); // reverse

Specify the direction (+ or -) for direction. The default value is +. When set to -, the direction of rotation is reversed.

The direction of rotation is affected by both set pol and set dir.

The value of set pol The value of set dir Direction of rotation
+ + clockwise
+ - counter clockwise
- + counter clockwise
- - clockwise

Setting PWM period

The command to set the PWM period is set period.

"dc1 pwm set period (period_us)"

Specify the period for period_us. The unit is micro-second.

spc_request_dev($sid, "dc1 pwm set period 10000"); // period: 10 ms

Controlling motors(Setting HIGH duration of PWM)

The command to set HIGH duration for motor control is set width.

The HIGH duration is the time during which the HIGH signal is output within one cycle of the PWM signal. Setting the HIGH duration determines the duty cycle of the PWM signal.

Duty Cycle(%) = HIGH duration / period * 100

In addition, PWM output starts simultaneously with this setting, so this command drives the motor.

<?php
include "/lib/sd_spc.php";
spc_reset();
spc_sync_baud(115200);

$sid = 1;
$width = 3000;

spc_request_dev($sid, "dc1 pwm set pol +");
spc_request_dev($sid, "dc1 pwm set dir +");
spc_request_dev($sid, "dc1 pwm set period 10000");
spc_request_dev($sid, "dc1 pwm set width $width");

while(1)
{
    $width -= 100;

    if($width <= 0)
        break;

    spc_request_dev($sid, "dc1 pwm set width $width");
    usleep(100000);
}
?>

Setting decay mode

The command to set the decay mode is set decay.

"dc1 pwm set decay (mode)"

Specify the decay mode for mode.

mode description
fast fast decay
slow slow decay
spc_request_dev($sid, "dc1 pwm set decay fast"); // fast decay
spc_request_dev($sid, "dc1 pwm set decay slow"); // slow decay