Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

cybergearをpythonで弄る その3:一軸だけ速度で動かす

shuzo-kino.hateblo.jp
の続き
今度は速度を変更

実際のところ

# モーターの有効化
motor1.set_run_mode(motor1.RunModes.SPEED_MODE)

motor1.enable()

## 速度パラメータを指定
motor1.write_single_param("spd_ref", value=0)


# ゼロ点に移動
motor1.write_single_param("loc_ref", value=0.0)

motor1.write_single_param("spd_ref", value=1)


# 動作メイン
start_time = time.time()
test_time = 15
speed = 2
motor1.enable()
while True:
    elapsed_time = time.time() - start_time
    if elapsed_time >= 10:
        logging.warning(f"{test_time} seconds have passed. End test")
        motor1.set_0_pos()
        motor1.disable()
        break
    _, pos, vel, torque_ext = motor1.write_single_param("spd_ref", value=speed)
    if abs(torque_ext) > 0.2:
        speed = -speed
        motor1.write_single_param("spd_ref", value=speed)
        logging.warning(f"reverse on torque_ext: {torque_ext:.2f}, vel:{vel:.2f}")
    time.sleep(0.01)