LoginSignup
3
2

More than 1 year has passed since last update.

myCobot Pi を GUIのSliderで動作させる

Last updated at Posted at 2021-07-26

はじめに

https://qiita.com/yosyda/items/2f60bad45002eafcbfc0
で、全く知識無しの状態で myCobot Pi の動作確認だけはしたのですが、その後もいろいろ試していたところ、GUIのスライダー操作で動作することまでは確認できました。
正しい手順なのかはまだ自信がありませんが、メモしておかないと忘れそうなので、ここまでをまとめておきます。

roscore

ROSを動作させるための基本ドライバ(?)のようです。
デフォルトでパスが通っているので、

$ roscore

とターミナルから実行すればOKです。

2021-07-27-002411_655x545_scrot.png

control_slider.py

~/ros_catkin_ws/src/myCobotROS/scripts にpythonスクリプトがあるのですが、標準状態だと
出力ポートの設定が /dev/ttyUSB になっているため、myCobot Piでは動作しないようです。
myBlockly のpython出力と同じくポートを /dev/ttyAMA0、ボーレートを 1000000 に書き換えたものを
control_slider2.py として用意し、そちらを代わりに起動しました。
(もしかすると、さらに正しい起動手順があるのかもしれませんが・・・)

#!/usr/bin/env python2
# from std_msgs.msg import String
import time, subprocess

import rospy
from sensor_msgs.msg import JointState

from pymycobot.mycobot import MyCobot


def callback(data):
    #rospy.loginfo(rospy.get_caller_id() + "%s", data.position)
    # print(data.position)
    data_list = []
    print(data.position)
    for index, value in enumerate(data.position):
        data_list.append(value)

    mc.send_radians(data_list, 80)
    # time.sleep(0.5)

def listener():
    rospy.init_node('control_slider', anonymous=True)

    rospy.Subscriber("joint_states", JointState, callback)

    # spin() simply keeps python from exiting until this node is stopped
    rospy.spin()

if __name__ == '__main__':
    print('start')
    # ==========================
    # Replace port and baudrate
    # ==========================
    mc = MyCobot('/dev/ttyAMA0',1000000)
    print(mc)
    listener()

control.launch よりも前に起動しておきます。
2021-07-27-011442_655x97_scrot.png

control.launch

$ roslaunch myCobotROS control.launch

とターミナルから実行します。

2021-07-27-003220_655x801_scrot.png
2021-07-27-003245_342x536_scrot.png

正常に実行されると、GUIのスライダーが表示され、先程のcontrol_slider2.pyのウインドウにログが流れ始めます。
2021-07-27-003407_655x417_scrot.png

この状態でGUIのスライダーを動かすと、本体も同様に連動して動くことが確認できました。
image.png

Centerボタンを押すと直立し、Randomizeボタンを押すとランダムな位置に動きます・・・が、机の上のものをなぎ倒したりするので、事前に卓上の片付けをおすすめします。油断していて、Airpods Proが吹き飛びました。

What's next?

$ rviz

で、rvizアプリが立ち上がるのですが、まだ何もmyCobotのモデルが表示されていないので、次はこれを何とかしたいです。
2021-07-27-012413_1200x846_scrot.png

2021/07/27 追記です。 rvizを立ち上げて、File>Open Config を開き ~/ros_catkin_es/src/myCobotROS/config/mycobot.rviz を開いたところ、無事rvizにmyCobotのモデルもSliderと連動表示できました。

image.png

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2