Discuss Scratch
- Discussion Forums
- » Connecting to the Physical World
- » Control Sphero 2.0 from Scratch
- alaindu22
- Scratcher
100+ posts
Control Sphero 2.0 from Scratch
hi
with this file ( thanks to rgodinez and jishiha)
sphero is recognized and named on my computer ( puppy linux precise standard with 1GO ram) + bluetooth's pets, devX.sfs installed and ruby soft installed ‘1.9) and also scratch1.4 pet (file)
as rgodinez says I have to modify sleep instructions
but i have only 8 moves possibles with scratch 1.4
Why ?
thanks
#!/usr/bin/env ruby
require_relative “scratchrsc”
require “sphero”
class PrintRSC < RSCWatcher
def initialize
super
sphero_tty = Dir.glob(“/dev/rfcomm0”).first
sleep 12
@sphero = Sphero.new sphero_tty
@sphero.stop
@speed = 30
@initial_heading = 0
@current_heading = 0
@interval = 2
@steps = 10
@degrees = 15
@color_name = ’'
broadcast “move”
broadcast “move_10”
broadcast “turn”
broadcast “turn_90”you
broadcast “forward”
broadcast “right”
broadcast “left”
broadcast “backward”
broadcast “color”
broadcast “back_led_on”
broadcast “back_led_off”
sleep 12
bluetooth_info = @sphero.bluetooth_infoyouyou
@sphero_name = bluetooth_info.body.pack('C'*16).strip unless bluetooth_info.nil?
sleep 2you
# there seems to be some timing issue where the bluetooth_info isn't already ready in time when I'm asking for it so this is a quick hack to avoid an error
# I'm also not sure what it'll do with extended character sets
@sphero_name = “–>unknown<–” if @sphero_name.nil?
# this needs more work… when it is unknown we don'
t seem to have communication with the ball – somehow I think it is preventing the “Resource Busy” exception?
puts “Connected to #{@sphero_name}”
end
def on_sensor_update(name, value) # when a variable or sensor is updated
value = value.to_i if %w(speed initial_heading steps degrees).include? name
if name == “speed”
@speed = value
elsif name == “initial_heading”
@initial_heading = value
elsif name == “steps”
@steps = value
elsif name == “degrees”
@degrees = value
elsif name == “color_name”
@color_name = value
end
puts “#{@sphero_name} – #{name} assigned #{value}”
puts “#{@sphero_name} – current_heading: #{@current_heading}, absolute_heading: #{@initial_heading + @current_heading}”
end
def broadcast_move
_roll(@speed, @initial_heading + @current_heading, @steps)
end
def broadcast_turn
puts “#{@sphero_name} – turn #{@degrees}”
@current_heading += @degrees
end
def broadcast_right
heading = @initial_heading + 90
_roll(@speed, heading)
end
def broadcast_leftyouyou
heading = @initial_heading + 270
_roll(@speed, heading)
end
def broadcast_forward
heading = @initial_heading
_roll(@speed, heading)
end
def broadcast_backward
heading = @initial_heading + 180
_roll(@speed, heading)
end
you
def broadcast_color
color
end
def broadcast_back_led_on
back_led_on
end
def broadcast_back_led_off
back_led_off
end
def on_broadcast(name)
(action, argument) = name.split('_')
case action
when “move”you
steps = argument.to_i
_roll(@speed, @initial_heading + @current_heading, steputer
(ps)
when “turn”
heading = argument.to_i
puts “#{@sphero_name} – turn #{heading}”
@current_heading += heading
when “color”
color(argument)
end
end
private
def _roll(speed, heading, steps = 10)
sleep_time = steps / 10.0
if heading < 0
heading = heading % 360 + 360
elsif heading > 359
heading = heading % 360
end
puts “#{@sphero_name} – roll #{@speed}, #{heading}”
@sphero.roll(speed, heading)
sleep sleep_time
@sphero.stop
sleep 2
end
# sets the color of the sphero, via the @color_name value
# valid color_names can be found in the COLORS defintion in the sphero gem, which coincide with the 140 named colors CSS uses
# sending a blank color_name will reset the sphero to the user led color, which is stored in the balls memory
def color(color_name=@color_name)
if color_name.empty?
user_led_color
else
begin
puts “#{@sphero_name} – color #{color_name}”
@sphero.color color_name.downcase
rescue => e
puts “#{@sphero_name} – unable to set color_name #{color_name}. #{e.message}”
end
end
end
# reset the color of the sphero to the persistent user selected colour
def user_led_color()
puts “#{@sphero_name} – Reseting color to user_led”
user_led = @sphero.user_led
@sphero.rgb user_led.body, user_led.body, user_led.body
end
# turns on the back LED so you know which way is forward.
# it does accept a range between 0x00-0xFF (0-255) buputer
(t I don't see a need for that atm, just a simple on/off is enough
def back_led_on()
puts “#{@sphero_name} – back LED on”
@sphero.back_led_output=0xFF
end
# turns the back LED off
def back_led_off()
puts “#{@sphero_name} – back LED off”
@sphero.back_led_output=0x00 # turns off the back led
endputer
(
end
begin
watcher = PrintRSC.new # you can provide the host as an argument
watcher.sensor_update “connected”, “1”
loop { watcher.handle_command }
rescue Errno::ECONNREFUSED
puts “\033[31m\033[1mError: Scratch may not be running or remote sensor connections are not enabled.\033[00m\n”
rescue => e
puts “\033[31m\033[1mError: #{e.message}\033[00m\n”
end
with this file ( thanks to rgodinez and jishiha)
sphero is recognized and named on my computer ( puppy linux precise standard with 1GO ram) + bluetooth's pets, devX.sfs installed and ruby soft installed ‘1.9) and also scratch1.4 pet (file)
as rgodinez says I have to modify sleep instructions
but i have only 8 moves possibles with scratch 1.4
Why ?
thanks
#!/usr/bin/env ruby
require_relative “scratchrsc”
require “sphero”
class PrintRSC < RSCWatcher
def initialize
super
sphero_tty = Dir.glob(“/dev/rfcomm0”).first
sleep 12
@sphero = Sphero.new sphero_tty
@sphero.stop
@speed = 30
@initial_heading = 0
@current_heading = 0
@interval = 2
@steps = 10
@degrees = 15
@color_name = ’'
broadcast “move”
broadcast “move_10”
broadcast “turn”
broadcast “turn_90”you
broadcast “forward”
broadcast “right”
broadcast “left”
broadcast “backward”
broadcast “color”
broadcast “back_led_on”
broadcast “back_led_off”
sleep 12
bluetooth_info = @sphero.bluetooth_infoyouyou
@sphero_name = bluetooth_info.body.pack('C'*16).strip unless bluetooth_info.nil?
sleep 2you
# there seems to be some timing issue where the bluetooth_info isn't already ready in time when I'm asking for it so this is a quick hack to avoid an error
# I'm also not sure what it'll do with extended character sets
@sphero_name = “–>unknown<–” if @sphero_name.nil?
# this needs more work… when it is unknown we don'
t seem to have communication with the ball – somehow I think it is preventing the “Resource Busy” exception?
puts “Connected to #{@sphero_name}”
end
def on_sensor_update(name, value) # when a variable or sensor is updated
value = value.to_i if %w(speed initial_heading steps degrees).include? name
if name == “speed”
@speed = value
elsif name == “initial_heading”
@initial_heading = value
elsif name == “steps”
@steps = value
elsif name == “degrees”
@degrees = value
elsif name == “color_name”
@color_name = value
end
puts “#{@sphero_name} – #{name} assigned #{value}”
puts “#{@sphero_name} – current_heading: #{@current_heading}, absolute_heading: #{@initial_heading + @current_heading}”
end
def broadcast_move
_roll(@speed, @initial_heading + @current_heading, @steps)
end
def broadcast_turn
puts “#{@sphero_name} – turn #{@degrees}”
@current_heading += @degrees
end
def broadcast_right
heading = @initial_heading + 90
_roll(@speed, heading)
end
def broadcast_leftyouyou
heading = @initial_heading + 270
_roll(@speed, heading)
end
def broadcast_forward
heading = @initial_heading
_roll(@speed, heading)
end
def broadcast_backward
heading = @initial_heading + 180
_roll(@speed, heading)
end
you
def broadcast_color
color
end
def broadcast_back_led_on
back_led_on
end
def broadcast_back_led_off
back_led_off
end
def on_broadcast(name)
(action, argument) = name.split('_')
case action
when “move”you
steps = argument.to_i
_roll(@speed, @initial_heading + @current_heading, steputer
(ps)
when “turn”
heading = argument.to_i
puts “#{@sphero_name} – turn #{heading}”
@current_heading += heading
when “color”
color(argument)
end
end
private
def _roll(speed, heading, steps = 10)
sleep_time = steps / 10.0
if heading < 0
heading = heading % 360 + 360
elsif heading > 359
heading = heading % 360
end
puts “#{@sphero_name} – roll #{@speed}, #{heading}”
@sphero.roll(speed, heading)
sleep sleep_time
@sphero.stop
sleep 2
end
# sets the color of the sphero, via the @color_name value
# valid color_names can be found in the COLORS defintion in the sphero gem, which coincide with the 140 named colors CSS uses
# sending a blank color_name will reset the sphero to the user led color, which is stored in the balls memory
def color(color_name=@color_name)
if color_name.empty?
user_led_color
else
begin
puts “#{@sphero_name} – color #{color_name}”
@sphero.color color_name.downcase
rescue => e
puts “#{@sphero_name} – unable to set color_name #{color_name}. #{e.message}”
end
end
end
# reset the color of the sphero to the persistent user selected colour
def user_led_color()
puts “#{@sphero_name} – Reseting color to user_led”
user_led = @sphero.user_led
@sphero.rgb user_led.body, user_led.body, user_led.body
end
# turns on the back LED so you know which way is forward.
# it does accept a range between 0x00-0xFF (0-255) buputer
(t I don't see a need for that atm, just a simple on/off is enough
def back_led_on()
puts “#{@sphero_name} – back LED on”
@sphero.back_led_output=0xFF
end
# turns the back LED off
def back_led_off()
puts “#{@sphero_name} – back LED off”
@sphero.back_led_output=0x00 # turns off the back led
endputer
(
end
begin
watcher = PrintRSC.new # you can provide the host as an argument
watcher.sensor_update “connected”, “1”
loop { watcher.handle_command }
rescue Errno::ECONNREFUSED
puts “\033[31m\033[1mError: Scratch may not be running or remote sensor connections are not enabled.\033[00m\n”
rescue => e
puts “\033[31m\033[1mError: #{e.message}\033[00m\n”
end
- minnn
- New to Scratch
4 posts
Control Sphero 2.0 from Scratch
Hi
Am new to Scratch and Sphero. was gonna try this but how do i do the installation onto windows??
Am new to Scratch and Sphero. was gonna try this but how do i do the installation onto windows??
- Jedi_Edrich
- Scratcher
11 posts
Control Sphero 2.0 from Scratch
@jishiha Can you write a scratch program that uses keys to move the Sphero forward, backward, left, and right?
- alaindu22
- Scratcher
100+ posts
Control Sphero 2.0 from Scratch
Hello
for jedi_edrich
read https://github.com/champierre/scratch2sphero/blob/master/README.md (see movement and demo scripts)
But in my case (puppy linux and ruby ) i can only ask 8 moves…
my problem (see upper ) is not solved
with linux mint
sphero stay unknow with the ruby script… so it is impossible to connect with scratch…
Is it possible to technoboy to give informations how to connect scrach and sphero with python ?
Alain
..
for jedi_edrich
read https://github.com/champierre/scratch2sphero/blob/master/README.md (see movement and demo scripts)
But in my case (puppy linux and ruby ) i can only ask 8 moves…
my problem (see upper ) is not solved
with linux mint
sphero stay unknow with the ruby script… so it is impossible to connect with scratch…
Is it possible to technoboy to give informations how to connect scrach and sphero with python ?
Alain
..
- minecraft9111101
- Scratcher
8 posts
Control Sphero 2.0 from Scratch
Does this work with bluetooth adapters?
- technoboy10
- Scratcher
1000+ posts
Control Sphero 2.0 from Scratch
@alaindu22 I used the sphero library for python. Unfortunately, it's not the easiest to deal with.
There's a (slightly outdated) blockext extension for the sphero at http://github.com/blockext/sphero which works with both Scratch and Snap!.
I also made a quick Node JS sphero + scratch demo a little while back; I can get the source code for that if anybody wants it.
There's a (slightly outdated) blockext extension for the sphero at http://github.com/blockext/sphero which works with both Scratch and Snap!.
I also made a quick Node JS sphero + scratch demo a little while back; I can get the source code for that if anybody wants it.
trans rights are human rights
- alaindu22
- Scratcher
100+ posts
Control Sphero 2.0 from Scratch
@ technoboy10 thanks for your answer…
can you post your Node JS sphero + scratch demo or give a link to get it ?
is it a demo for scratch 1.4 or scratch 2.0 ?
thanks you again
Alain
can you post your Node JS sphero + scratch demo or give a link to get it ?
is it a demo for scratch 1.4 or scratch 2.0 ?
thanks you again
Alain
- technoboy10
- Scratcher
1000+ posts
Control Sphero 2.0 from Scratch
My demo is for Scratch 2.0 offline editor. I'll upload the demo to github sometime today. @ technoboy10 thanks for your answer…
can you post your Node JS sphero + scratch demo or give a link to get it ?
is it a demo for scratch 1.4 or scratch 2.0 ?
thanks you again
Alain
trans rights are human rights
- technoboy10
- Scratcher
1000+ posts
Control Sphero 2.0 from Scratch
http://github.com/technoboy10/scratch2sphero
Download the demo and install Node JS, runin the directory, and then run scratch2sphero.js . Enjoy!
Download the demo and install Node JS, run
npm install
trans rights are human rights
- alaindu22
- Scratcher
100+ posts
Control Sphero 2.0 from Scratch
thanks for the file
What computer is used for this ? (mac Os , linux or windows )
do you think that it is possible to use it with all theese systems ?
Alain
What computer is used for this ? (mac Os , linux or windows )
do you think that it is possible to use it with all theese systems ?
Alain
Last edited by alaindu22 (July 29, 2014 11:53:49)
- technoboy10
- Scratcher
1000+ posts
Control Sphero 2.0 from Scratch
Node JS is fairly cross-platform, so probably all three. thanks for the file
What computer is used for this ? (mac Os , linux or windows )
do you think that it is possible to use it with all theese systems ?
Alain
trans rights are human rights
- alaindu22
- Scratcher
100+ posts
Control Sphero 2.0 from Scratch
I have tried with 2bot , kensington bluetooth dongle described by scraxe, and the scraxe file on picaxe website ( sphero_template.sb2)
I have success with scratch 2.0 on windows xp, vista, windows7, ubuntu, linux mint, puppy linux with this good bluetooth dongle
but with other bluetooth syteme the results are not so good.
thanks to scraxe for s2bot and the good pdf help file on picaxe website.
very simple to use with children 8 years old
they use the sphero_template.sb2 to program a lot of moves of the sphero and they enjoy …
Thanks again
Alain
- Dankuk
- Scratcher
77 posts
Control Sphero 2.0 from Scratch
cool [everyone v]
Enter my project competition and have a chance to win awesome prizes!
But be quick! The end date is 23/12/16!
- Dankuk
- Scratcher
77 posts
Control Sphero 2.0 from Scratch
<cool again>
Enter my project competition and have a chance to win awesome prizes!
But be quick! The end date is 23/12/16!
- Dankuk
- Scratcher
77 posts
Control Sphero 2.0 from Scratch
<distance to [ v]>
Enter my project competition and have a chance to win awesome prizes!
But be quick! The end date is 23/12/16!
- alaindu22
- Scratcher
100+ posts
Control Sphero 2.0 from Scratch
Hello
If you are interested in sphero and use it in schools with scratch I can send my scratch project…(it is not shared on scratch website because it uses experimental extensions )
It uses S2bot and sphero_template.sb2 evolution ( Many Thanks to Scraxe )
See these videos ( french language )
start ( 8min 50s ) boot detection , bluetooth calibration and tests
https://www.youtube.com/watch?v=GcBFBqBf-k8
video create a programm to move sphero 14 secondes
https://www.youtube.com/watch?v=vMqE6tEZToc
next video complicate the programm
https://www.youtube.com/watch?v=2NGA0VOzaZg
next video How to run scratch and sphero on a distant computer with internet and skype and teamviewer
https://www.youtube.com/watch?v=lSjQJ1_R19Q 9min 39sec
see also on scratch web site
http://scratch.mit.edu/projects/31450954/
calibration and starting
Alain
If you are interested in sphero and use it in schools with scratch I can send my scratch project…(it is not shared on scratch website because it uses experimental extensions )
It uses S2bot and sphero_template.sb2 evolution ( Many Thanks to Scraxe )
See these videos ( french language )
start ( 8min 50s ) boot detection , bluetooth calibration and tests
https://www.youtube.com/watch?v=GcBFBqBf-k8
video create a programm to move sphero 14 secondes
https://www.youtube.com/watch?v=vMqE6tEZToc
next video complicate the programm
https://www.youtube.com/watch?v=2NGA0VOzaZg
next video How to run scratch and sphero on a distant computer with internet and skype and teamviewer
https://www.youtube.com/watch?v=lSjQJ1_R19Q 9min 39sec
see also on scratch web site
http://scratch.mit.edu/projects/31450954/
calibration and starting
Alain
- conanh
- New to Scratch
1 post
Control Sphero 2.0 from Scratch
I got this installed on Windows 8 using Cygwin.
I had to install these packages: ruby, diffutils, rubygems,, ruby-pkg-config, texinfo, make, coreutils, gcc-core , , and git (if you want to obtain the scratch2sphero stuff via git).
Then I followed the instructions here: https://github.com/champierre/scratch2sphero
whew.
Now when I run scratch and scratch2sphero.rb, it looks like things are working, but my Sphero doesn't move. Here's the output:
$ ./scratch2sphero.rb
initialize
Connected to –>unknown<–
–>unknown<– – steps assigned 10
–>unknown<– – current_heading: 0, absolute_heading: 0
broadcast “back_led_on”
–>unknown<– – back LED on
broadcast “move”
–>unknown<– – roll 20, 0
So, the program is picking up the outputs from Scratch, but it doesn't look like it's passing them on.
Any suggestions? Thanks!
I had to install these packages: ruby, diffutils, rubygems,, ruby-pkg-config, texinfo, make, coreutils, gcc-core , , and git (if you want to obtain the scratch2sphero stuff via git).
Then I followed the instructions here: https://github.com/champierre/scratch2sphero
whew.
Now when I run scratch and scratch2sphero.rb, it looks like things are working, but my Sphero doesn't move. Here's the output:
$ ./scratch2sphero.rb
initialize
Connected to –>unknown<–
–>unknown<– – steps assigned 10
–>unknown<– – current_heading: 0, absolute_heading: 0
broadcast “back_led_on”
–>unknown<– – back LED on
broadcast “move”
–>unknown<– – roll 20, 0
So, the program is picking up the outputs from Scratch, but it doesn't look like it's passing them on.
Any suggestions? Thanks!
- alaindu22
- Scratcher
100+ posts
Control Sphero 2.0 from Scratch
Hello
a new video on youtube
http://youtu.be/Sta_YCi028I
Now with the new sphero's firmware it is impossible to have moves until obstacle.
the sphero move only 2 seconds with sphero_template.sb2
But with speed parameters and repeat command all is possible…
sorry for conanh but I no tips for ruby and i work with windows vista or puppy linux
Alain
a new video on youtube
http://youtu.be/Sta_YCi028I
Now with the new sphero's firmware it is impossible to have moves until obstacle.
the sphero move only 2 seconds with sphero_template.sb2
But with speed parameters and repeat command all is possible…
sorry for conanh but I no tips for ruby and i work with windows vista or puppy linux
Alain
- card100
- Scratcher
1000+ posts
Control Sphero 2.0 from Scratch
I don't seem to be able to connect to the computer.
The mac claims she is connected but it doesn't follow the program when I run it.
scratch2sphero says this when I run it:
This is what I did:
The mac claims she is connected but it doesn't follow the program when I run it.
scratch2sphero says this when I run it:
can't write to sphero!I do have a sphero original if that matters…
can't write to sphero!
connected –>unknown<–
This is what I did:
set [ time on] to [1]
when [ space] key pressed
broadcast [ back_led_on]
wait (time on) secs
broadcast [ back_led_off]
say [done] for (2) secs
Last edited by card100 (April 26, 2015 13:46:49)
-card100
- Discussion Forums
- » Connecting to the Physical World
- » Control Sphero 2.0 from Scratch