adjusting for changes in skynet protocol

pull/2/head
TimEvWw 2014-03-04 20:33:42 -01:00
parent 550cda8675
commit 64aeedc9b9
9 changed files with 168 additions and 241 deletions

View File

@ -15,15 +15,15 @@ class Controller
def runFarmBot
check = @bot_dbaccess.checkRefresh
check = @bot_dbaccess.check_refresh
while $shutdown == 0 do
# keep checking the database for new data
puts 'checking schedule'
command = @bot_dbaccess.getCommandToExecute
@bot_dbaccess.saveRefresh
command = @bot_dbaccess.get_command_to_execute
@bot_dbaccess.save_refresh
if command != nil
@ -38,7 +38,7 @@ class Controller
puts 'execute command'
process_command( command )
@bot_dbaccess.setCommandToExecuteStatus('FINISHED')
@bot_dbaccess.set_command_to_execute_status('FINISHED')
else
@ -55,7 +55,7 @@ class Controller
sleep 1
refresh_received = @bot_dbaccess.checkRefresh
refresh_received = @bot_dbaccess.check_refresh
puts 'refresh received' if refresh_received != false
end
@ -76,7 +76,7 @@ class Controller
sleep 1
refresh_received = @bot_dbaccess.checkRefresh
refresh_received = @bot_dbaccess.check_refresh
puts 'refresh received' if refresh_received != false
end
@ -90,17 +90,17 @@ class Controller
cmd.commandlines.each do |command_line|
case command_line.action.upcase
when "MOVE ABSOLUTE"
$bot_hardware.moveAbsolute(command_line.coord_x, command_line.coord_y, command_line.coord_z)
$bot_hardware.move_absolute(command_line.coord_x, command_line.coord_y, command_line.coord_z)
when "MOVE RELATIVE"
$bot_hardware.moveRelative(command_line.coord_x, command_line.coord_y, command_line.coord_z)
$bot_hardware.move_relative(command_line.coord_x, command_line.coord_y, command_line.coord_z)
when "HOME X"
$bot_hardware.moveHomeX
$bot_hardware.move_home_x
when "HOME Y"
$bot_hardware.moveHomeY
$bot_hardware.move_home_y
when "HOME Z"
$bot_hardware.moveHomeZ
$bot_hardware.move_home_z
when "SET SPEED"
$bot_hardware.setSpeed(command_line.speed)
$bot_hardware.set_speed(command_line.speed)
end
end
else

View File

@ -1,81 +0,0 @@
require 'bson'
require 'mongo'
require 'mongoid'
require './app/models/command.rb'
# This class is dedicated to retrieving and inserting commands into the command
# queue for the farm bot
class CommandQueue
def initialize
Mongoid.load!("config/mongo.yml", :development)
@last_command_retrieved = nil
@refresh_value = 0
@refresh_value_new = 0
@new_command = nil
end
def create_new_command(scheduled_time)
@new_command = Command.new
@new_command.scheduled_time = scheduled_time
end
def add_command_line(action, x = 0, y = 0, z = 0, speed = 0, amount = 0)
if @new_command != nil
line = Commandline.new
line.action = action
line.coord_x = x
line.coord_y = y
line.coord_z = z
line.speed = speed
line.amount = amount
if @new_command.commandlines == nil
@new_command.commandlines = [ line ]
else
@new_command.commandlines << line
end
end
end
def save_new_command
if @new_command != nil
@new_command.status = 'test'
@new_command.save
end
increment_refresh
end
def get_command_to_execute
@last_command_retrieved = Command.where(
:status => 'test',
:scheduled_time.ne => nil
).order_by([:scheduled_time,:asc]).first
@last_command_retrieved
end
def set_command_to_execute_status(new_status)
if @last_command_retrieved != nil
@last_command_retrieved.status = new_status
@last_command_retrieved.save
end
end
def check_refresh
r = Refresh.where(:name => 'FarmBotControllerSchedule').first_or_initialize
@refresh_value_new = r.value.to_i
return @refresh_value_new != @refresh_value
end
def save_refresh
@refresh_value = @refresh_value_new
end
def increment_refresh
r = Refresh.where(:name => 'FarmBotControllerSchedule').first_or_initialize
r.value = r.value.to_i + 1
r.save
end
end

View File

@ -1,25 +0,0 @@
# command message class that is put into the queue to execute by the farmbot
class ControlCommand
def initialize
commandid = "NOP"
end
attr_accessor :lines, :commandid
end
class ControlCommandLine
attr_accessor :action
attr_accessor :xCoord, :xHome
attr_accessor :yCoord, :yHome
attr_accessor :zCoord, :zHome
attr_accessor :quantity, :speed
def parseText( text )
params = text.split(',')
@action = params[0].to_s
@xCoord = params[1].to_i
@yCoord = params[2].to_i
@zCoord = params[3].to_i
end
end

View File

@ -6,6 +6,7 @@ require 'mongoid'
# This class is dedicated to retrieving and inserting commands into the schedule
# queue for the farm bot Mongo is used as the database, Mongoid as the
# databasemapper
class Command
include Mongoid::Document
@ -65,12 +66,12 @@ class DbAccess
end
end
def createNewCommand(scheduled_time)
def create_new_command(scheduled_time)
@new_command = Command.new
@new_command.scheduled_time = scheduled_time
end
def addCommandLine(action, x = 0, y = 0, z = 0, speed = 0, amount = 0)
def add_command_line(action, x = 0, y = 0, z = 0, speed = 0, amount = 0)
if @new_command != nil
line = Commandline.new
line.action = action
@ -87,15 +88,15 @@ class DbAccess
end
end
def saveNewCommand
def save_new_command
if @new_command != nil
@new_command.status = 'test'
@new_command.save
end
incrementRefresh
increment_refresh
end
def getCommandToExecute
def get_command_to_execute
@last_command_retrieved = Command.where(
:status => 'test',
:scheduled_time.ne => nil
@ -103,24 +104,24 @@ class DbAccess
@last_command_retrieved
end
def setCommandToExecuteStatus(new_status)
def set_command_to_execute_status(new_status)
if @last_command_retrieved != nil
@last_command_retrieved.status = new_status
@last_command_retrieved.save
end
end
def checkRefresh
def check_refresh
r = Refresh.where(:name => 'FarmBotControllerSchedule').first_or_initialize
@refresh_value_new = r.value.to_i
return @refresh_value_new != @refresh_value
end
def saveRefresh
def save_refresh
@refresh_value = @refresh_value_new
end
def incrementRefresh
def increment_refresh
r = Refresh.where(:name => 'FarmBotControllerSchedule').first_or_initialize
r.value = r.value.to_i + 1
r.save

View File

@ -18,7 +18,7 @@ class HardwareInterfaceAxis
# set the pins for one motor with sensors
#
def setPinMode()
def set_pin_mode()
# set the pins for motor control to output
@board.set_pin_mode(pin_enb, Firmata::Board::OUTPUT)
@ -38,45 +38,45 @@ class HardwareInterfaceAxis
end
def disableMotor()
def disable_motor()
@board.digital_write(@pin_enb, Firmata::Board::HIGH)
end
def enableMotor()
def enable_motor()
@board.digital_write(@pin_enb, Firmata::Board::LOW)
end
def setDirectionLow()
def set_direction_low()
@board.digital_write(pin_dir, Firmata::Board::LOW)
sleep @sleep_after_pin_set
enableMotor()
enable_motor()
end
def setDirectionHigh()
def set_direction_high()
@board.digital_write(pin_dir, Firmata::Board::HIGH)
sleep @sleep_after_pin_set
enableMotor()
enable_motor()
end
# set the direction and enable pins to prepare for the move to the home position
#
def moveHomeSetDirection()
def move_home_set_direction()
if (invert_axis ^ reverse_home) == false
setDirectionLow()
set_direction_low()
else
setDirectionHigh()
set_direction_high()
end
end
# move the motor until the end stop is reached
#
def moveHome()
def move_home()
moveHomeSetDirection()
move_home_set_direction()
start = Time.now
home = 0
@ -90,23 +90,27 @@ class HardwareInterfaceAxis
if span > @move_home_timeout
home = 1
puts 'move home #{@name} timed out'
puts "move home #{@name} timed out"
end
if (@board.pins[@pin_min].value == 1 and @reverse_home == false) or
(@board.pins[@pin_max].value == 1 and @reverse_home == true )
if @board.pins[@pin_min].value == 1 and @reverse_home == false
home = 1
puts 'end stop home #{@name} reached'
puts "end stop home min #{@name} reached"
end
if @board.pins[@pin_max].value == 1 and @reverse_home == true
home = 1
puts "end stop home max #{@name} reached"
end
if home == 0
setPulseOnPin(@pin_stp)
set_pulse_on_pin(@pin_stp)
end
end
# disable motor driver
disableMotor()
disable_motor()
@pos = 0
@ -114,7 +118,7 @@ class HardwareInterfaceAxis
# set a pulse on a pin with enough sleep time so firmata kan keep up
#
def setPulseOnPin(pin)
def set_pulse_on_pin(pin)
@board.digital_write(pin, Firmata::Board::HIGH)
sleep @sleep_after_pin_set
@board.digital_write(pin, Firmata::Board::LOW)
@ -124,17 +128,17 @@ class HardwareInterfaceAxis
# prepare the move by setting the direction and enable
#
def moveStepsPrepare(steps)
def move_steps_prepare(steps)
@steps = steps
@nr_steps = steps.abs
if (@steps < 0 and @invert_axis == false) or (@steps > 0 and @invert_axis == true)
setDirectionLow()
set_direction_low()
end
if (@steps > 0 and @invert_axis == false) or (@steps < 0 and @invert_axis == true)
setDirectionHigh()
set_direction_high()
end
end
@ -142,16 +146,20 @@ class HardwareInterfaceAxis
# move one motor a step if needed, while checking the end stops
#
def moveSteps()
def move_steps()
# check end stops
if (@board.pins[@pin_min].value == 1 and @steps < 0) or
(@board.pins[@pin_max].value == 1 and @steps > 0)
if @board.pins[@pin_min].value == 1 and @steps < 0
@nr_steps = 0
@pos = @min if @steps < 0
@pos = @max if @steps > 0
puts "end stop #{@name} reached"
@pos = @min
puts "end stop min #{@name} reached"
end
if @board.pins[@pin_max].value == 1 and @steps > 0
@nr_steps = 0
@pos = @max
puts "end stop max #{@name} reached"
end
# check minimum and maximum position
@ -164,7 +172,7 @@ class HardwareInterfaceAxis
# send the step pulses to the motor drivers
if @nr_steps > 0
setPulseOnPin(@pin_stp)
set_pulse_on_pin(@pin_stp)
@pos += 1.0 / @steps_per_unit * (@steps<=>0.0)
@nr_steps -= 1
@ -189,16 +197,16 @@ class HardwareInterface
@axis_z = HardwareInterfaceAxis.new
loadConfig()
connectBoard()
setPinNumbers()
setBoardPinMode()
load_config()
connect_board()
set_pin_numbers()
set_board_pin_mode()
end
# set the hardware pin numbers
#
def setPinNumbers
def set_pin_numbers
@pin_led = 13
@ -227,7 +235,7 @@ class HardwareInterface
# connect to the serial port and start communicating with the arduino/firmata protocol
#
def connectBoard
def connect_board
@boardDevice = "/dev/ttyACM0"
@board = Firmata::Board.new @boardDevice
@ -241,7 +249,7 @@ class HardwareInterface
# load the settings for the hardware
# these are the timeouts and distance settings mainly
def loadConfig
def load_config
@axis_x.move_home_timeout = 15 # seconds after which home command is aborted
@axis_y.move_home_timeout = 15
@ -271,42 +279,42 @@ class HardwareInterface
# set motor driver and end stop pins to input or output output and set enables for the drivers to off
#
def setBoardPinMode
def set_board_pin_mode
@axis_x.setPinMode()
@axis_y.setPinMode()
@axis_z.setPinMode()
@axis_x.set_pin_mode()
@axis_y.set_pin_mode()
@axis_z.set_pin_mode()
end
# move the bot to the home position
#
def moveHomeX
@axis_x.moveHome()
@axis_x.disableMotor()
def move_home_x
@axis_x.move_home()
@axis_x.disable_motor()
end
# move the bot to the home position
#
def moveHomeY
@axis_y.moveHome()
@axis_y.disableMotor()
def move_home_y
@axis_y.move_home()
@axis_y.disable_motor()
end
# move the bot to the home position
#
def moveHomeZ
@axis_z.moveHome()
@axis_z.disableMotor()
def move_home_z
@axis_z.move_home()
@axis_z.disable_motor()
end
def setSpeed( speed )
def set_speed( speed )
end
# move the bot to the give coordinates
#
def moveAbsolute( coord_x, coord_y, coord_z)
def move_absolute( coord_x, coord_y, coord_z)
puts '**move absolute **'
@ -320,13 +328,13 @@ class HardwareInterface
puts "y steps #{steps_y}"
puts "z steps #{steps_z}"
moveSteps(steps_x, steps_y, steps_z )
move_steps(steps_x, steps_y, steps_z )
end
# move the bot a number of units starting from the current position
#
def moveRelative( amount_x, amount_y, amount_z)
def move_relative( amount_x, amount_y, amount_z)
puts '**move relative **'
@ -340,19 +348,19 @@ class HardwareInterface
puts "y steps #{steps_y}"
puts "z steps #{steps_z}"
moveSteps( steps_x, steps_y, steps_z )
move_steps( steps_x, steps_y, steps_z )
end
# drive the motors so the bot is moved a number of steps
#
def moveSteps(steps_x, steps_y, steps_z)
def move_steps(steps_x, steps_y, steps_z)
# set the direction and the enable bit for the motor drivers
@axis_x.moveStepsPrepare(steps_x)
@axis_y.moveStepsPrepare(steps_y)
@axis_z.moveStepsPrepare(steps_z)
@axis_x.move_steps_prepare(steps_x)
@axis_y.move_steps_prepare(steps_y)
@axis_z.move_steps_prepare(steps_z)
# loop until all steps are done
@ -367,16 +375,16 @@ class HardwareInterface
@board.read_and_process
# move the motors
done_x = @axis_x.moveSteps()
done_y = @axis_y.moveSteps()
done_z = @axis_z.moveSteps()
done_x = @axis_x.move_steps()
done_y = @axis_y.move_steps()
done_z = @axis_z.move_steps()
end
# disable motor drivers
@axis_x.disableMotor()
@axis_y.disableMotor()
@axis_z.disableMotor()
@axis_x.disable_motor()
@axis_y.disable_motor()
@axis_z.disable_motor()
end
end

View File

@ -1,5 +1,6 @@
require 'json'
require './lib/database/commandqueue.rb'
#require './lib/database/commandqueue.rb'
require './lib/database/dbaccess.rb'
# Get the JSON command, received through skynet, and send it to the farmbot
# command queue Parses JSON messages received through SkyNet.
@ -8,7 +9,7 @@ class MessageHandler
attr_accessor :message
def initialize
@command_queue = CommandQueue.new
@dbaccess = DbAccess.new
@last_time_stamp = ''
end
@ -32,10 +33,22 @@ class MessageHandler
# "delay" : 6
# }
# }
def handle_message(channel, message)
@message = message
requested_command = message["message_type"].to_s.downcase
def handle_message(message)
puts 'handle_message'
#puts message
#puts message['message']
@message = message['message']
#fromUuid = message['fromUuid']
#puts fromUuid
requested_command = message['message']["message_type"].to_s.downcase
#puts requested_command
if whitelist.include?(requested_command)
#puts 'sending'
self.send(requested_command, message)
else
self.error(message)
@ -49,7 +62,11 @@ class MessageHandler
end
def single_command(message)
time_stamp = message['time_stamp']
puts 'single_command'
#puts message
time_stamp = message['message']['time_stamp']
sender = message['fromUuid']
if time_stamp != @last_time_stamp
@ -57,21 +74,21 @@ class MessageHandler
# send the command to the queue
delay = message['command']['delay']
action = message['command']['action']
x = message['command']['x']
y = message['command']['y']
z = message['command']['z']
speed = message['command']['speed']
amount = message['command']['amount']
delay = message['command']['delay']
delay = message['message']['command']['delay']
action = message['message']['command']['action']
x = message['message']['command']['x']
y = message['message']['command']['y']
z = message['message']['command']['z']
speed = message['message']['command']['speed']
amount = message['message']['command']['amount']
delay = message['message']['command']['delay']
puts "[new command] received at #{Time.now} from #{sender}"
#puts "[#{action}] x: #{x}, y: #{y}, z: #{z}, speed: #{speed}, amount: #{amount} delay: #{delay}"
puts "[#{action}] x: #{x}, y: #{y}, z: #{z}, speed: #{speed}, amount: #{amount} delay: #{delay}"
@command_queue.create_new_command(Time.now + delay.to_i)
@command_queue.add_command_line(action, x.to_i, y.to_i, z.to_i, speed.to_s, amount.to_i)
@command_queue.save_new_command
@dbaccess.create_new_command(Time.now + delay.to_i)
@dbaccess.add_command_line(action, x.to_i, y.to_i, z.to_i, speed.to_s, amount.to_i)
@dbaccess.save_new_command
$skynet.confirmed = false

View File

@ -37,15 +37,19 @@ class Skynet
# Acts as the entry point for message traffic captured from Skynet.im.
# This method is a stub for now until I have time to merge into Tim's
# controller code. Returns a MessageHandler object (a class yet created).
def handle_message(channel, message)
#def handle_message(channel, message)
def handle_message(message)
puts "> message received at #{Time.now}"
#puts message
if message.class.to_s == 'Hash'
@message_handler.handle_message(channel, message)
@message_handler.handle_message(message)
end
if message.class.to_s == 'String'
message_hash = JSON.parse(message)
@message_handler.handle_message(channel, message_hash)
@message_handler.handle_message(message_hash)
end
rescue

View File

@ -22,8 +22,11 @@ module WebSocket
### Routes all skynet messages to handle_event() for interpretation.
def create_message_event
@socket.on :message do |channel, message|
$skynet.handle_message(channel, message)
#@socket.on :message do |channel, message|
# $skynet.handle_message(channel, message)
#end
@socket.on :message do |message|
$skynet.handle_message(message)
end
end

54
menu.rb
View File

@ -71,41 +71,41 @@ while $shutdown == 0 do
# read the file
#TestFileHandler.readCommandFile
when "Z" # Move to home
$bot_dbaccess.createNewCommand(Time.now + $command_delay)
$bot_dbaccess.addCommandLine('HOME Z', 0, 0, 0, 0, 0)
$bot_dbaccess.saveNewCommand
$bot_dbaccess.create_new_command(Time.now + $command_delay)
$bot_dbaccess.add_command_line('HOME Z', 0, 0, 0, 0, 0)
$bot_dbaccess.save_new_command
when "X" # Move to home
$bot_dbaccess.createNewCommand(Time.now + $command_delay)
$bot_dbaccess.addCommandLine('HOME X', 0, 0, 0, 0, 0)
$bot_dbaccess.saveNewCommand
$bot_dbaccess.create_new_command(Time.now + $command_delay)
$bot_dbaccess.add_command_line('HOME X', 0, 0, 0, 0, 0)
$bot_dbaccess.save_new_command
when "C" # Move to home
$bot_dbaccess.createNewCommand(Time.now + $command_delay)
$bot_dbaccess.addCommandLine('HOME Y',0 ,0 ,-$move_size, 0, 0)
$bot_dbaccess.saveNewCommand
$bot_dbaccess.create_new_command(Time.now + $command_delay)
$bot_dbaccess.add_command_line('HOME Y',0 ,0 ,-$move_size, 0, 0)
$bot_dbaccess.save_new_command
when "W" # Move forward
$bot_dbaccess.createNewCommand(Time.now + $command_delay)
$bot_dbaccess.addCommandLine('MOVE RELATIVE',0,$move_size, 0, 0, 0)
$bot_dbaccess.saveNewCommand
$bot_dbaccess.create_new_command(Time.now + $command_delay)
$bot_dbaccess.add_command_line('MOVE RELATIVE',0,$move_size, 0, 0, 0)
$bot_dbaccess.save_new_command
when "S" # Move back
$bot_dbaccess.createNewCommand(Time.now + $command_delay)
$bot_dbaccess.addCommandLine('MOVE RELATIVE',0,-$move_size, 0, 0, 0)
$bot_dbaccess.saveNewCommand
$bot_dbaccess.create_new_command(Time.now + $command_delay)
$bot_dbaccess.add_command_line('MOVE RELATIVE',0,-$move_size, 0, 0, 0)
$bot_dbaccess.save_new_command
when "A" # Move left
$bot_dbaccess.createNewCommand(Time.now + $command_delay)
$bot_dbaccess.addCommandLine('MOVE RELATIVE', -$move_size, 0, 0, 0, 0)
$bot_dbaccess.saveNewCommand
$bot_dbaccess.create_new_command(Time.now + $command_delay)
$bot_dbaccess.add_command_line('MOVE RELATIVE', -$move_size, 0, 0, 0, 0)
$bot_dbaccess.save_new_command
when "D" # Move right
$bot_dbaccess.createNewCommand(Time.now + $command_delay)
$bot_dbaccess.addCommandLine('MOVE RELATIVE', $move_size, 0, 0, 0, 0)
$bot_dbaccess.saveNewCommand
$bot_dbaccess.create_new_command(Time.now + $command_delay)
$bot_dbaccess.add_command_line('MOVE RELATIVE', $move_size, 0, 0, 0, 0)
$bot_dbaccess.save_new_command
when "R" # Move up
$bot_dbaccess.createNewCommand(Time.now + $command_delay)
$bot_dbaccess.addCommandLine('MOVE RELATIVE', 0, 0, $move_size, 0, 0)
$bot_dbaccess.saveNewCommand
$bot_dbaccess.create_new_command(Time.now + $command_delay)
$bot_dbaccess.add_command_line('MOVE RELATIVE', 0, 0, $move_size, 0, 0)
$bot_dbaccess.save_new_command
when "F" # Move down
$bot_dbaccess.createNewCommand(Time.now + $command_delay)
$bot_dbaccess.addCommandLine("MOVE RELATIVE", 0, 0, -$move_size, 0, 0)
$bot_dbaccess.saveNewCommand
$bot_dbaccess.create_new_command(Time.now + $command_delay)
$bot_dbaccess.add_command_line("MOVE RELATIVE", 0, 0, -$move_size, 0, 0)
$bot_dbaccess.save_new_command
end
end