Discuss Scratch
- Discussion Forums
- » Connecting to the Physical World
- » Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
- megakuku
-
Scratcher
9 posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
@itchy20
I can reproduce it with these steps:
- load the extension s2a_fm.s2e (from https://github.com/MrYsLab/s2a_fm)
- open the “More blocks” tab (the part of the library that contains the extension blocks)
- Add a ‘move servo’-block to the script, fill in the pin and click it
- Minimize scratch
- Wait
After some time Scratch will be frozen… I even think you don't need to add a block in the script to get it locked.
I know about Snap!, but we use Scratch in our CoderDojo. It would be nice if we don't have to switch tools to build some cool stuff with arduino's ;-) We cannot rely on a good internet connection, so we would have to take an older version (BYOB). Or use S4A.cat (which seems to be more limited)
A bug free version of Scratch would be very nice ;-)
I can reproduce it with these steps:
- load the extension s2a_fm.s2e (from https://github.com/MrYsLab/s2a_fm)
- open the “More blocks” tab (the part of the library that contains the extension blocks)
- Add a ‘move servo’-block to the script, fill in the pin and click it
- Minimize scratch
- Wait
After some time Scratch will be frozen… I even think you don't need to add a block in the script to get it locked.
I know about Snap!, but we use Scratch in our CoderDojo. It would be nice if we don't have to switch tools to build some cool stuff with arduino's ;-) We cannot rely on a good internet connection, so we would have to take an older version (BYOB). Or use S4A.cat (which seems to be more limited)
A bug free version of Scratch would be very nice ;-)
- itchy20
-
Scratcher
100+ posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
@megakuku
This is the first report of this type of behavior I have received and I am not able to reproduce it here. By the way, there is an error in the the way you are using s2a_fm, in that before executing the move servo block you should first enable the digital pin as a servo pin. Leaving that step out should not freeze Scratch or crash s2a_fm. What you should see is a warning on the console where s2a_fm was started that looks something like:
DEBUG: 2014-03-11 20:15:57.893661: set_servo_position 5 90
set_servo_position: Pin 5 was not enabled for SERVO operations.
To try and isolate this problem, could you please try the following:
1. Enable debugging with the s2a_fm debugging block.
2, Initialize the digital pin for servo mode
3. Execute the move servo block.
If Scratch still freezes, I am assuming you can still bring up a web browser. If that is true, enter the following URL into your browser:
http://localhost:50209/set_servo_position/5/90
Replace the 5 in this line with the pin number you are using and go to that URL.
Make sure you have the http:// each time you execute from the browser. Please check the file called s2a_fm_debugging.log in the log directory of s2a_fm to see if an entry was made at the time you executed the URL in your browser.
If you see an entry in the log, then s2a_fm is still alive and the freeze is definitely in Scratch. If not, let me know and I will try to come up with a strategy to figure out what might be going on.
This is the first report of this type of behavior I have received and I am not able to reproduce it here. By the way, there is an error in the the way you are using s2a_fm, in that before executing the move servo block you should first enable the digital pin as a servo pin. Leaving that step out should not freeze Scratch or crash s2a_fm. What you should see is a warning on the console where s2a_fm was started that looks something like:
DEBUG: 2014-03-11 20:15:57.893661: set_servo_position 5 90
set_servo_position: Pin 5 was not enabled for SERVO operations.
To try and isolate this problem, could you please try the following:
1. Enable debugging with the s2a_fm debugging block.
2, Initialize the digital pin for servo mode
3. Execute the move servo block.
If Scratch still freezes, I am assuming you can still bring up a web browser. If that is true, enter the following URL into your browser:
http://localhost:50209/set_servo_position/5/90
Replace the 5 in this line with the pin number you are using and go to that URL.
Make sure you have the http:// each time you execute from the browser. Please check the file called s2a_fm_debugging.log in the log directory of s2a_fm to see if an entry was made at the time you executed the URL in your browser.
If you see an entry in the log, then s2a_fm is still alive and the freeze is definitely in Scratch. If not, let me know and I will try to come up with a strategy to figure out what might be going on.
- megakuku
-
Scratcher
9 posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
Hi itchy20,
I did some further testing to narrow down the steps you need to reproduce it:
- Start scratch
- Import s2a_fm.s2e
- Wait
Probably important to know: the http is not started, so as you mention I guess it is probably a scratch issue…
I've tried it on different systems:
- Windows 8.1 64 bit with Adobe Air 4 => Hangs
- Windows 8.1 64 bit with Adobe Air 3 => Hangs
- Windows 8.1 64 bit with Adobe Air 13 (labs) => Hangs
- Windows 7 32 bit with Adobe Air 4 => Hangs
On 2 systems I could not reproduce it:
- On a virtual machine with Windows XP & Adobe Air 4
- On a macbook with OS X 10.9
I did some further testing to narrow down the steps you need to reproduce it:
- Start scratch
- Import s2a_fm.s2e
- Wait
Probably important to know: the http is not started, so as you mention I guess it is probably a scratch issue…
I've tried it on different systems:
- Windows 8.1 64 bit with Adobe Air 4 => Hangs
- Windows 8.1 64 bit with Adobe Air 3 => Hangs
- Windows 8.1 64 bit with Adobe Air 13 (labs) => Hangs
- Windows 7 32 bit with Adobe Air 4 => Hangs
On 2 systems I could not reproduce it:
- On a virtual machine with Windows XP & Adobe Air 4
- On a macbook with OS X 10.9
- megakuku
-
Scratcher
9 posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
Just tried with the example scratch extension from the draft spec, and that makes the offline editor freeze too… So it definitily seems to be a scratch bug…
{ “extensionName”: “Extension Example”,
“extensionPort”: 12345,
“blockSpecs”: [
,
,
,
]
}
{ “extensionName”: “Extension Example”,
“extensionPort”: 12345,
“blockSpecs”: [
,
,
,
]
}
- itchy20
-
Scratcher
100+ posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
@megakuku
Thank you for taking the time to test and letting me know your results.
Thank you for taking the time to test and letting me know your results.
- RichardDL
-
Scratcher
16 posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
@itcy20 Thanks, I've made some progress, but not running yet. As I'm running the installs, ‘print’ is failing and I'm editing them to ‘print()’. The problem I can't fix is on the command ‘.\s2a_fm.py’ it fails with “C:\Programs\Python33\lib\site-packages\PyMata\pymata.py”, line 26 … from pymata_serial import PyMataSerial ImportError: No module named ‘pymata_serial’. But I can see the file pymata_serial.py in that same folder. ?
- itchy20
-
Scratcher
100+ posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
@RichardDL
Both s2a_fm and PyMata require using Python version 2 and it looks like you are running version 3. Converting a version 2 program is non-trivial, so just changing the print statements is not sufficient, but a good first step. I would suggest using Python 2.7.6 and it should work properly. Let me know if you are still having problems after going to version 2.
Both s2a_fm and PyMata require using Python version 2 and it looks like you are running version 3. Converting a version 2 program is non-trivial, so just changing the print statements is not sufficient, but a good first step. I would suggest using Python 2.7.6 and it should work properly. Let me know if you are still having problems after going to version 2.
- RichardDL
-
Scratcher
16 posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
@itchy20 - It works! I have very ‘blink’ program running in Scratch but guess I can do anything I want. A class of 30 (9 yrs) should be very impressed. It's heavy on CPU use, I wouldn't mind losing some performance to reduce this.
- Metrology1234
-
New Scratcher
7 posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
Please can you explain a bit more about this comment in your code
# we can't use the standard send_response since we don't conform to its
# standards, so we craft our own response handler here
def send_resp(self, response):
“”“
This method sends Scratch an HTTP response to an HTTP GET command.
”“”
crlf = “\r\n”
# http_response = str(response + crlf)
http_response = “HTTP/1.1 200 OK” + crlf
etc
I am trying to use c#.net HTML server to talk to scratch and it always send back an error. Is this the reason why?
# we can't use the standard send_response since we don't conform to its
# standards, so we craft our own response handler here
def send_resp(self, response):
“”“
This method sends Scratch an HTTP response to an HTTP GET command.
”“”
crlf = “\r\n”
# http_response = str(response + crlf)
http_response = “HTTP/1.1 200 OK” + crlf
etc
I am trying to use c#.net HTML server to talk to scratch and it always send back an error. Is this the reason why?
- itchy20
-
Scratcher
100+ posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
I am not familiar with the C#.net server, but if you have the source, look at the HTTP send_response message that it is constructing and ultimately sending to Scratch. If the message format is different from the one constructed below, then you will most likely need to override the existing send_response message method, which is exactly what I had to do for s2a_fm. Scratch did not like the “standard” response that the python library constructed as a response.
Looking at the s2a_fm python method, the send response is constructed concatenating all of the pieces shown below.:
crlf = “\r\n”
# http_response = str(response + crlf)
http_response = “HTTP/1.1 200 OK” + crlf
http_response += “Content-Type: text/html; charset=ISO-8859-1” + crlf
http_response += “Content-Length” + str(len(response)) + crlf
http_response += “Access-Control-Allow-Origin: *” + crlf
http_response += crlf
#add the response to the nonsense above
if response != ‘okay’:
http_response += str(response + crlf)
# send it out the door to Scratch
self.wfile.write(http_response)
What error are you receiving back?
Looking at the s2a_fm python method, the send response is constructed concatenating all of the pieces shown below.:
crlf = “\r\n”
# http_response = str(response + crlf)
http_response = “HTTP/1.1 200 OK” + crlf
http_response += “Content-Type: text/html; charset=ISO-8859-1” + crlf
http_response += “Content-Length” + str(len(response)) + crlf
http_response += “Access-Control-Allow-Origin: *” + crlf
http_response += crlf
#add the response to the nonsense above
if response != ‘okay’:
http_response += str(response + crlf)
# send it out the door to Scratch
self.wfile.write(http_response)
What error are you receiving back?
Last edited by itchy20 (May 19, 2014 21:44:34)
- SCRAXE
-
New Scratcher
72 posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
http_response += “Content-Length” + str(len(response)) + crlf
Where's your colon-space
:-)
- jakahila
-
New Scratcher
2 posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
yes
- Metrology1234
-
New Scratcher
7 posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
I am not familiar with the C#.net server, but if you have the source, look at the HTTP send_response message that it is constructing and ultimately sending to Scratch. If the message format is different from the one constructed below, then you will most likely need to override the existing send_response message method, which is exactly what I had to do for s2a_fm. Scratch did not like the “standard” response that the python library constructed as a response.I am getting a 400 Bad request sent back by the c# HTTP listener . So I agree the HTTP from Scratch is wrong in some way.
Looking at the s2a_fm python method, the send response is constructed concatenating all of the pieces shown below.:
crlf = “\r\n”
# http_response = str(response + crlf)
http_response = “HTTP/1.1 200 OK” + crlf
http_response += “Content-Type: text/html; charset=ISO-8859-1” + crlf
http_response += “Content-Length” + str(len(response)) + crlf
http_response += “Access-Control-Allow-Origin: *” + crlf
http_response += crlf
#add the response to the nonsense above
if response != ‘okay’:
http_response += str(response + crlf)
# send it out the door to Scratch
self.wfile.write(http_response)
What error are you receiving back?
I am trying a simple listening socket and sending back the response above with the “: ” added it seems to be working enough to send back values.
Also I don't get a request for a domain file from Adobe just /poll's from the start. Any idea why?
- itchy20
-
Scratcher
100+ posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
You may wish to check out this file:A4S.java..
The author implemented a simple HTTP server to communicate with Scratch. It is written in Java.
The crossdomain file request only occurs for the on-line version of the editor. If you are using the off-line editor, you will never see the request. The Java file handles the cross-domain request as well, it is probably a good starting point for your code.
The author implemented a simple HTTP server to communicate with Scratch. It is written in Java.
The crossdomain file request only occurs for the on-line version of the editor. If you are using the off-line editor, you will never see the request. The Java file handles the cross-domain request as well, it is probably a good starting point for your code.
Last edited by itchy20 (May 20, 2014 23:26:32)
- Metrology1234
-
New Scratcher
7 posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
Ok Ill not worry about the cross domain file.
Thanks for posting the java. I have something working now , the polls are working but commands take a long time to come through and Sctach crashes after a thousand or so polls.
Thanks for posting the java. I have something working now , the polls are working but commands take a long time to come through and Sctach crashes after a thousand or so polls.
- chrisnolan
-
Scratcher
15 posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
Hi there My Y,
We're playing with s2a_fm again and running through the ardx.org kit trying to reproduce them in Scratch. We're on to circuit #5 which introduces Shift Registers (specifically the 74HC595).
Arduino has a method called shiftOut() which their example code uses, but isn't exposed with the command handlers. I figured before I dug into things too deeply in trying to work it into the library I'd see if you've come across something similar before, and how would you go about implementing it – point us in the right direction and we can probably send a pull request? Looking at things and I'm not clear if firmata itself has a ShiftOut or not, one wiki shows it as a proposal, another forum shows it as about to be implemented and I'm confused on the multiple versions of firmata and their connection to pymata.
Their example code alternatively shows how to manipulate the bits instead but I don't think Scratch can work with bits directly, can it?
Thanks for your time
We're playing with s2a_fm again and running through the ardx.org kit trying to reproduce them in Scratch. We're on to circuit #5 which introduces Shift Registers (specifically the 74HC595).
Arduino has a method called shiftOut() which their example code uses, but isn't exposed with the command handlers. I figured before I dug into things too deeply in trying to work it into the library I'd see if you've come across something similar before, and how would you go about implementing it – point us in the right direction and we can probably send a pull request? Looking at things and I'm not clear if firmata itself has a ShiftOut or not, one wiki shows it as a proposal, another forum shows it as about to be implemented and I'm confused on the multiple versions of firmata and their connection to pymata.
Their example code alternatively shows how to manipulate the bits instead but I don't think Scratch can work with bits directly, can it?
Thanks for your time
Last edited by chrisnolan (May 23, 2014 20:44:38)
- itchy20
-
Scratcher
100+ posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
Firmata can be confusing. There is a SysEx command defined for Shift in the Arduino Firmata library (Firmata.h), but sadly it is not implemented.
Funny you should ask how to implement it for yourself. I am finishing up an article for Instructables that demonstrates how to add a new device to PyMata and to the FirmataPlus Arduino Sketch. I am hoping to complete that in the next week or so, so it would be a good guide for you. FYI, this guide adds a stepper motor. It really is quite easy.
If you want to try on your own, look at the Tone support in pymata.py and pymata_command_handler.py. You can look at FirmataPlus.ino for the Arduino side.
To help clarify, PyMata implements the entire StandardFirmata protocol. I have also extended it to support additional devices that I have interest in. StandardFirmata is an example sketch supplied with Arduino Firmata, FirmataPlus is an extended StandardFirmata sketch that I supply with PyMata.
On the Scratch end, if I understand ShiftOut (and I may very well not), you give it a value and that value can be shifted left or right, bit by bit. So if you are looking to build a custom Scratch block in s2a_fm, you could specify the initial value (I am not sure if a numeric input takes a hex value or not, but if not,the value could be specified as a string and then converted). That block would initialize the shift register, and then another block would be created to perform the shift. BTW, creating new blocks and functionality is much simpler in Snap!, so you might want to prototype things there before trying to implement the Scratch code.
Hopefully, this helped. If you have any other questions, please just ask.
Funny you should ask how to implement it for yourself. I am finishing up an article for Instructables that demonstrates how to add a new device to PyMata and to the FirmataPlus Arduino Sketch. I am hoping to complete that in the next week or so, so it would be a good guide for you. FYI, this guide adds a stepper motor. It really is quite easy.
If you want to try on your own, look at the Tone support in pymata.py and pymata_command_handler.py. You can look at FirmataPlus.ino for the Arduino side.
To help clarify, PyMata implements the entire StandardFirmata protocol. I have also extended it to support additional devices that I have interest in. StandardFirmata is an example sketch supplied with Arduino Firmata, FirmataPlus is an extended StandardFirmata sketch that I supply with PyMata.
On the Scratch end, if I understand ShiftOut (and I may very well not), you give it a value and that value can be shifted left or right, bit by bit. So if you are looking to build a custom Scratch block in s2a_fm, you could specify the initial value (I am not sure if a numeric input takes a hex value or not, but if not,the value could be specified as a string and then converted). That block would initialize the shift register, and then another block would be created to perform the shift. BTW, creating new blocks and functionality is much simpler in Snap!, so you might want to prototype things there before trying to implement the Scratch code.
Hopefully, this helped. If you have any other questions, please just ask.
- chrisnolan
-
Scratcher
15 posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
Sounds great about the Instructable, we'll wait for that to be released and give it a go in adding support for the shift registers.
We moved on to the next circuit, in this case using using a piezo speaker to make some music. After a bit of head butting, I remembered that we needed to use a different Firmata for the tone support. We found the FirmataPlus.ino in the Pymata examples but we're getting errors compiling it. There are our errors:
Looking into the source, it seems a bunch of the lines with errors were added in the March 22nd commit and it seems related to ‘ENCODER’, where the commit message says it was just a minor fix re the frequency/duration. The main issue seems to stem from a constant that isn't declared. I'm wondering if something work in progress got committed by mistake?
Of course I could be missing something else too. My arduino is 1.5.6-r2 and I did the stuff re NewPing as described in the readme.
I just tried the version of FirmataPlus.ino from before that commit, but I get errors too so I think I'm doing something wrong… :-( I thought it'd be like the standard firmata, just open it in arduino, and upload it to the board.
We moved on to the next circuit, in this case using using a piezo speaker to make some music. After a bit of head butting, I remembered that we needed to use a different Firmata for the tone support. We found the FirmataPlus.ino in the Pymata examples but we're getting errors compiling it. There are our errors:
FirmataPlus:123: error: ‘IGNORE’ was not declared in this scope
FirmataPlus:124: error: ‘IGNORE’ was not declared in this scope
FirmataPlus:125: error: ‘IGNORE’ was not declared in this scope
FirmataPlus:126: error: ‘IGNORE’ was not declared in this scope
FirmataPlus.ino: In function ‘void setPinModeCallback(byte, int)’:
FirmataPlus:295: error: ‘TONE’ was not declared in this scope
FirmataPlus:298: error: ‘SONAR’ was not declared in this scope
FirmataPlus:301: error: ‘ENCODER’ was not declared in this scope
FirmataPlus.ino: In function ‘void sysexCallback(byte, byte, byte*)’:
FirmataPlus:583: error: ‘ENCODER_CONFIG’ was not declared in this scope
FirmataPlus:595: error: ‘IGNORE’ was not declared in this scope
FirmataPlus:618: error: ‘IGNORE’ was not declared in this scope
FirmataPlus:623: error: ‘IGNORE’ was not declared in this scope
FirmataPlus:645: error: ‘TONE_DATA’ was not declared in this scope
FirmataPlus:668: error: ‘SONAR_CONFIG’ was not declared in this scope
FirmataPlus:690: error: ‘SONAR’ was not declared in this scope
FirmataPlus.ino: In function ‘void systemResetCallback()’:
FirmataPlus:751: error: ‘IS_PIN_TONE’ was not declared in this scope
FirmataPlus:762: error: ‘IGNORE’ was not declared in this scope
FirmataPlus:770: error: ‘IGNORE’ was not declared in this scope
FirmataPlus:772: error: ‘IGNORE’ was not declared in this scope
FirmataPlus:775: error: ‘IGNORE’ was not declared in this scope
FirmataPlus.ino: In function ‘void setup()’:
FirmataPlus:815: error: ‘IGNORE’ was not declared in this scope
FirmataPlus.ino: In function ‘void loop()’:
FirmataPlus:871: error: ‘SONAR_DATA’ was not declared in this scope
FirmataPlus:878: error: ‘IGNORE’ was not declared in this scope
FirmataPlus:886: error: ‘ENCODER_DATA’ was not declared in this scope
Looking into the source, it seems a bunch of the lines with errors were added in the March 22nd commit and it seems related to ‘ENCODER’, where the commit message says it was just a minor fix re the frequency/duration. The main issue seems to stem from a constant that isn't declared. I'm wondering if something work in progress got committed by mistake?
Of course I could be missing something else too. My arduino is 1.5.6-r2 and I did the stuff re NewPing as described in the readme.
I just tried the version of FirmataPlus.ino from before that commit, but I get errors too so I think I'm doing something wrong… :-( I thought it'd be like the standard firmata, just open it in arduino, and upload it to the board.
FirmataPlus.ino: In function ‘void setPinModeCallback(byte, int)’:
FirmataPlus:282: error: ‘TONE’ was not declared in this scope
FirmataPlus:285: error: ‘SONAR’ was not declared in this scope
FirmataPlus.ino: In function ‘void sysexCallback(byte, byte, byte*)’:
FirmataPlus:569: error: ‘ENCODER_CONFIG’ was not declared in this scope
FirmataPlus:573: error: ‘TONE_DATA’ was not declared in this scope
FirmataPlus:596: error: ‘SONAR_CONFIG’ was not declared in this scope
FirmataPlus:618: error: ‘SONAR’ was not declared in this scope
FirmataPlus.ino: In function ‘void systemResetCallback()’:
FirmataPlus:680: error: ‘IS_PIN_TONE’ was not declared in this scope
FirmataPlus:691: error: ‘IGNORE’ was not declared in this scope
FirmataPlus.ino: In function ‘void setup()’:
FirmataPlus:730: error: ‘IGNORE’ was not declared in this scope
FirmataPlus.ino: In function ‘void loop()’:
FirmataPlus:786: error: ‘SONAR_DATA’ was not declared in this scope
- itchy20
-
Scratcher
100+ posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
The easiest way to fix those errors is to download the latest version of PyMata - version 1.57. The problem is that there are include path conflicts with the latest versions of the Arduino IDE. If you remove your current version of FirmataPlus from your Sketchbook, and unzip the latest version of FirmataPlus (there are now install instructions in the documentation directory), it should compile without errors.
The article is done! Here is the Link
The article is done! Here is the Link
Last edited by itchy20 (May 26, 2014 18:09:44)
- Metrology1234
-
New Scratcher
7 posts
Just released s2a_fm - a hardware extension for Arduino/Scratch Integration
You may wish to check out this file:A4S.java..
The author implemented a simple HTTP server to communicate with Scratch. It is written in Java.
The crossdomain file request only occurs for the on-line version of the editor. If you are using the off-line editor, you will never see the request. The Java file handles the cross-domain request as well, it is probably a good starting point for your code.
Hi
I am still having trouble with the offline version does your comment mean that the online version supports HTTP as well as java script.
How do I load the javascript examples http://llk.github.io/scratch-extensions/? I have hosted one of the examples on google drive and tried to load it into the online version but it does nothing. No error message to tell be the problem. Any advice?
- Discussion Forums
- » Connecting to the Physical World
-
» Just released s2a_fm - a hardware extension for Arduino/Scratch Integration




