Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
0c2db2e
add a simulation high level object and make serialsimulation not inhe…
jcarres-mdsol Oct 14, 2012
bf2ffd4
correctly account for local rvmrc
jcarres-mdsol Jan 13, 2013
3b63f43
added initial test for simulation
jcarres-mdsol Jan 13, 2013
8724852
added a formatter to make output more selective and open to send data…
jcarres-mdsol Jan 13, 2013
ef7bae2
update README
jcarres-mdsol Jan 13, 2013
d2141a7
created a subdir simulation, this may be the start of a different gem…
jcarres-mdsol Feb 2, 2013
f458c1a
solved some bugs, now it seems much more correct
jcarres-mdsol Feb 3, 2013
fa5d6d4
abstracted a class robotsimulation and moved all the robot like prope…
jcarres-mdsol Feb 3, 2013
04098d6
Degrees are now exact instead of approximated.
jcarres-mdsol Feb 9, 2013
47bf1a2
metaprogram to modify roomba from the outside and make it live in the…
jcarres-mdsol Feb 9, 2013
5760927
changed params in initializers, we do not need to propagate time
jcarres-mdsol Feb 9, 2013
3827b37
added physics related classes.
jcarres-mdsol Feb 10, 2013
e212648
added a bumper class, still unrealistic and the serial soon will beco…
jcarres-mdsol Feb 12, 2013
8ed2f69
merged master back in
jcarres-mdsol Mar 4, 2013
00e125e
finish the merge + some improvements on comments and such
jcarres-mdsol Mar 5, 2013
307ac84
more realistic bumper
jcarres-mdsol Mar 5, 2013
989bef0
solve several bugs
jcarres-mdsol Mar 5, 2013
e94e785
added physical_shape to robot simulation
jcarres-mdsol Mar 6, 2013
3897e00
removed code managed by Ein physics simulator
JordiPolo Mar 6, 2013
d380353
get rid of own implementation of logger and use standard library, mak…
JordiPolo Mar 10, 2013
b131d6d
revert moving a line around
JordiPolo Mar 10, 2013
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .rvmrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
if [ -f "$0".local ]; then
source "$0".local
if [ -f .rvmrc.local ]; then
source .rvmrc.local
else
rvm use 1.9.3
fi
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ group :assets do
gem 'twitter-bootstrap-rails'
end

group :development,:test do
gem 'debugger'
gem 'ein' #physics simulator
end

group :development do
gem 'thin'
end
Expand Down
11 changes: 11 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,17 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.2.0)
columnize (0.3.6)
commonjs (0.2.5)
daemons (1.1.8)
debugger (1.2.3)
columnize (>= 0.3.1)
debugger-linecache (~> 1.1.1)
debugger-ruby_core_source (~> 1.1.5)
debugger-linecache (1.1.2)
debugger-ruby_core_source (>= 1.1.1)
debugger-ruby_core_source (1.1.6)
ein (0.0.2)
erubis (2.7.0)
eventmachine (0.12.10)
execjs (1.3.0)
Expand Down Expand Up @@ -139,6 +148,8 @@ PLATFORMS

DEPENDENCIES
coffee-rails
debugger
ein
jquery-rails
json
minitest
Expand Down
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,23 @@ Jump in console and give the basic simulation a shot.

````ruby
$ rails c
:001 > earth = World.new
:002 > roo = RoombaSimulation.new
:003 > earth.spawn(roo)
:004 > roo.move(100)
:005 > roo.move(0,120)
:006 > roo.move(1000)
:001 > simulator = Simulator.new
:002 > roo = RoombaSimulation.new(simulator)
:004 > simulator.start
:005 > roo.move(100)
:006 > roo.move(0,90)
:007 > roo.move(1000)
````
You should end up with a bump reading at N:90, X:126 Y:89

X:126 is the center point of Simulated Roomba. Add the radius of Roomba + the radius of the obstacle and it should be the same as the distance between X:126 and the default simulated obstacle.

If you want to get information of each step of the simulation do:

```Ruby
LOGGER.level = Logger::DEBUG
```

Or jump into the rails app and play around.

````
Expand Down
9 changes: 6 additions & 3 deletions app/controllers/simulations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ def index
# GET /simulations/1
# GET /simulations/1.json
def show
#TODO: serialize/deserialize sim from DB
@simulation = Simulation.find(params[:id])
# TODO: This is confusing
# Watch out! Simulator != Simulation .
simulator = Simulator.new
@world = simulator.world

#this is super crap, just for debugging ATM, moving out soon
@world = World.new
@roombot = RoombaSimulation.new
@world.spawn(@roombot)
@roombot = RoombaSimulation.new(simulator)

respond_to do |format|
format.html # show.html.erb
Expand Down
2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Application < Rails::Application

# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += %W(#{config.root}/lib #{config.root}/lib/simulator)

# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
Expand Down
13 changes: 13 additions & 0 deletions lib/byte_processing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module ByteProcessing

def signed_integer(bytes)
case bytes.size
when 1
return (bytes[0] & ~(1 << 7)) - (bytes[0] & (1 << 7))
when 2
sixteenbit = bytes[0] << 8 | bytes[1]
return (sixteenbit & ~(1 << 15)) - (sixteenbit & (1 << 15))#http://en.wikipedia.org/wiki/Two%27s_complement#Calculating_two.27s_complement
end
end

end
12 changes: 12 additions & 0 deletions lib/calculations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Calculations
def calculate_spin_time(velocity, degree)
# time = wheelbase * PI / 360degrees * degrees / velocity ABS
# wheelbase might be different for different roombas, consider refactoring
((((Roomba::Specification::WHEELBASE * Math::PI) / 360) * degree.abs).to_f / velocity.to_f).abs
end

#spinning needs some work
def calculate_spin_degree(velocity, time)
((time.to_f * velocity.to_f) / ((Roomba::Specification::WHEELBASE * Math::PI) / 360)) #/ 10**10
end
end
31 changes: 9 additions & 22 deletions lib/roomba.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Roomba
:saturday => '01000000'
}

include ByteProcessing
include Calculations

def initialize(port, latency=0.1, baud=115200, serial=nil)
# baud must be 115200 for communicating with 500 series Roomba and newer (tested with Roomba 770), change to 57600 for 400 series and older
Expand Down Expand Up @@ -65,9 +67,9 @@ def move(distance, degree=0, velocity=200)
set_velocity(velocity)
set_degree(degree)
drive(@velocity_high, @velocity_low, @radius_high, @radius_low)
start_moving = Time.now
time_in_seconds = 10 if time_in_seconds > 10
until (start_moving - Time.now).abs >= time_in_seconds
start_moving = current_time
until (start_moving - current_time).abs >= time_in_seconds
# sensors call sleeps the script for 20ms, max read is 50ms, total time between loops about 65ms
sensors = get_readings(:bumps_and_drops, :wall)
@messages.push sensors
Expand All @@ -77,15 +79,10 @@ def move(distance, degree=0, velocity=200)
sensors
end

def calculate_spin_time(velocity, degree)
# time = wheelbase * PI / 360degrees * degrees / velocity ABS
# wheelbase might be different for different roombas, consider refactoring
((((WHEELBASE * Math::PI) / 360) * degree.abs).to_f / velocity.to_f).abs
end

#spinning needs some work
def calculate_spin_degree(velocity, time)
((time.to_f * velocity.to_f) / ((WHEELBASE * Math::PI) / 360)) / 10**10
#This is overwritten in the roomba_simulation so we can use simulation
#time instead of real world time.
def current_time
Time.now
end

def set_degree(degree)
Expand Down Expand Up @@ -166,7 +163,7 @@ def get_readings(*sensors_requested)
readings[sensor] = {:raw => nil, :formatted => []}
readings[sensor][:raw] = bytes.shift(SENSORS[sensor][:bytes])
readings[sensor][:formatted] = set_readings(sensor, readings[sensor][:raw])
puts "Sensors: #{readings[sensor].inspect}"
#puts "Sensors: #{readings[sensor].inspect}"
end
readings #return hash of readings
end
Expand All @@ -189,16 +186,6 @@ def set_readings(sensor, readings)
end
end

def signed_integer(bytes)
case bytes.size
when 1
return (bytes[0] & ~(1 << 7)) - (bytes[0] & (1 << 7))
when 2
sixteenbit = bytes[0] << 8 | bytes[1]
return (sixteenbit & ~(1 << 15)) - (sixteenbit & (1 << 15))#http://en.wikipedia.org/wiki/Two%27s_complement#Calculating_two.27s_complement
end
end

def motors
motors(1)
sleep 2
Expand Down
Loading