LoginSignup
0

More than 3 years have passed since last update.

Rotrics DexArmを使ってみた #2

Posted at

Rotrics DexArmを使ってみた #1の続きです。SDKが出てくる気配がないので、シリアル通信でGCodeを送ってなんとかします。意外と応答が素直だから、これで良いのかという気もしてきました。


#!/usr/bin/env python3
import serial, time

#Serial setting
s = serial.Serial("/dev/ttyACM0",115200)

def w(gcode):
    '''
    Send GCode to Dexarm and wait for the response.
    '''
    print(gcode)
    s.write(gcode.encode('utf-8')+b'\r\n')
    r = None
    while r == None or "echo:" in r:
        r = s.readline().decode('utf-8').strip()
        print(r)  # "ok", "echo:busy" or "beyond limit.."

def wait(ms):
    w('G4 P'+str(ms))

def move(x,y,z):
    cood = ""
    if x != None: cood += "X"+str(x)
    if y != None: cood += "Y"+str(y)
    if z != None: cood += "Z"+str(z)
    w('G0 '+cood)

z1 = 0
z2 = 30

def pick():
    move(None,None,z1)
    w('G0 Z0')
    w('M1000')
    wait(500)
    move(None,None,z2)

def drop():
    move(None,None,z1)
    w('M1001')
    move(None,None,z2)
    wait(500)
    w('M1003')

# Main
def main():
    # Initialize
    w("M1111")
    time.sleep(1)
    move(100,0,30)

    # Pick and move objects in different speeds.
    w("G0 F3000")
    move(100,200,z2)
    pick()
    move(0,-200,z2)
    drop()
    w("G0 F6000")
    move(50,200,z2)
    pick()
    move(50,-200,z2)
    drop()
    w("G0 F8000")
    move(0,200,z2)
    pick()
    move(100,-200,z2)
    drop()

    # back to center
    move(200,0,0)
    s.close()
    print("Fin.")

#Main loop
if __name__ == '__main__':
    main()

よく考えたらPythonに固執する必要もないので、Rubyで書き直し始めています。続きは実機が手に入ったら書きます。基本的な操作をgemとかにまとめても良いかもしれませんね。

#!/usr/bin/env ruby
# sudo apt install ruby-dev -y
# sudo gem install serialport

require 'serialport'

s = SerialPort.new('/dev/ttyACM0', 115200, 8, 1, 0) # device, rate, data, stop, parity

def w(gcode)
    puts gcode
    s.puts gcode + '\r\n'
    r = nil
    while r == nil || r.start_with?("echo:") do
        # "ok", "echo:busy" or "beyond limit.."
        r = s.readline.chomp.strip
        puts r
    end
end

w("M1111")
sleep(1)
s.close

puts "fin"

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
What you can do with signing up
0