Skip to content

Instantly share code, notes, and snippets.

@poqudrof
Last active August 6, 2017 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poqudrof/f3282ce1e7b2a4e9cbdc61a8c5ea1696 to your computer and use it in GitHub Desktop.
Save poqudrof/f3282ce1e7b2a4e9cbdc61a8c5ea1696 to your computer and use it in GitHub Desktop.
Robus Ruby work in progress
# coding: utf-8
require 'rubygems'
require 'rubyserial'
require 'json'
require 'eventmachine'
require 'em-websocket-client'
require 'osc-ruby'
$osc = OSC::Client.new('localhost', 4559 )
$osc.send(OSC::Message.new( "/greeting" , "hullo!" ))
module Robus
class RobusConnection
attr_reader :message, :json_message
attr_reader :connection
attr_reader :address
end
class WSConnection < RobusConnection
def set_message(msg)
if msg == nil or msg.to_s == ""
puts "No message"
else
m = msg.to_s.dup
# puts "update msg " + m
@message = m
# puts @message
# puts self
begin
@json_message = JSON.parse(@message)
p = @json_message["modules"][1]["position"]
$osc.send(OSC::Message.new( "/potar" , p )) if $osc != nil
rescue => e
puts "Error" + e
end
end
end
def initialize(host = '192.168.43.120', port = 9342)
@connected = true
@address = 'ws://'+ host + ":" + port.to_s
Thread.new do
EM.run do
puts "EM"
conn = EventMachine::WebSocketClient.connect(@address)
conn.callback do
puts "Callback ?"
# conn.send_msg "Hello!"
# conn.send_msg "done"
end
conn.errback do |e|
puts "Got error: #{e}"
end
# conn.close_connection
conn.stream do |msg|
if msg == nil
puts "nil msg"
else
set_message msg
end
end
conn.disconnect do
puts "gone"
EM::stop_event_loop
end
@connection = conn
end
end
end
# def send(message)
# @connection.send message.to_json
# end
end
class SerialConnection < RobusConnection
def initialize(address = '/dev/ttyACM0')
@address = address
@connected = true
@connection = Serial.new address, 57600
@thread = Thread.new do
while @connected do
begin
get_message
rescue => e
puts e
end
sleep 0.03 ## todo: frequency ?
end
end
end
def send(message)
begin
@connection.write(message.to_json + "\r")
rescue => e
puts "Erreur à l'envoi" + e.to_s
end
end
def stop; @connected = false ; end
def get_message
@message = ""
b = @connection.getbyte
while b != nil
@message << [b].pack("c*")
@message << @connection.read(b)
b = @connection.getbyte
end
begin
@json_message = JSON.parse @message
rescue
end
end
end
class Robot
attr_reader :modules
attr_reader :connection
def initialize(address)
## TODO: parse the address
@connection = SerialConnection.new(address) if address.start_with? "/dev"
@connection = WSConnection.new(address) if address.start_with? "192"
sleep 2
@modules = []
start_robot
end
def start_robot
json = @connection.json_message
if json != nil
json["modules"].each do |mod|
update_module mod
end
end
end
def update_module(mod)
# is a new module
id = mod["id"]
if @modules[id] == nil
@modules[id] = Module.new(self, mod)
else
@modules[id].update(mod)
end
end
def send_command(message)
@connection.send(message)
end
end
class Module
attr_reader :alias, :id, :type
attr_reader :robot
def initialize(robot, json_data)
@type = json_data["type"]
@id = json_data["id"]
@alias = json_data["alias"]
@robot = robot
m = self
@robot.send(:define_singleton_method, @alias) do
m
end
end
## check name etc...
def update(json_data)
@value = json_data["value"]
end
def value
@value
end
def value= (v)
@value = v
message = { "modules" => { @alias.to_s => { "value" => v } }}
@robot.send_command(message)
end
def position
@position
end
def position= (v)
@position = v
message = { "modules" => { @alias.to_s => { "target_position" => v } }}
@robot.send_command(message)
puts "Message sent " + message.to_json.to_s
end
def to_s
@alias.to_s + " " + @id.to_s + " " + @value.to_s
end
end
end
# $robot = Robus::Robot.new '192.168.43.120'
## robot = Robus::Robot.new '/dev/ttyACM0'
# robot.gloo_servo.position = -10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment