This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2008-09-03 10:03:10

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Remote Sensor Connections in v1.3

v1.3 includes a cool hidden feature that advanced users may be interested in playing with. With a little programming knowledge, you can communicate with external programs and devices, such as an Arduino board.

When you shift-right click on one of the sensor value blocks, you will see an entry in the pull-down menu that lets you enable remote sensor connections.

Basically, once remote sensor connections have been enabled, an extension within Scratch v.1.3 allows you to connect to it via a TCP socket at port  42001. This provides two-way communication between Scratch and whatever program you  use to create the socket (e.g., Actionscript, Java, Processing, Ruby, Python, Squeak,  or any other language that supports sockets.) In Processing, for example, this is  done by creating a Client using the Processing network library:

Code:

Client ScratchClient; 
ScratchClient = new Client (this, "127.0.0.1", 42001);

Or in Python:

Code:

            
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratchSock.connect(('127.0.0.1', 42001))

Messages from Scratch

When remote sensors are enabled, and a client is connected to port 42001, every  Scratch broadcast is thereafter also sent out through the TCP socket as the string  “broadcast"  followed by the actual broadcast message, usually in double quotes, e.g.  :

broadcast “hai!"

When global variables are changed within Scratch, a message is sent out as the string  “sensor-update"  followed by the variable name and the new value:

sensor-update “Scratch-hitpoints"  12

The "Scratch-" part of the first argument lets you know it is a Scratch variable. Note that if the actual value of the global variable does not change within Scratch,  then no message will be sent out. So, even if you have 50 identical blocks setting  the value of “hitpoints"  to 12, it will only send out a message the first time  (assuming the value is not initially 12). This means that if you save a Scratch  project in a certain state, then you’ll have to change the state of the variables to  cause a message to be sent out.

Note also that no messages are sent out for changes in local variables (the ones seen only be a single sprite) or lists (whether global or not).

Messages to Scratch

Broadcast and sensor-update messages are sent to Scratch from your program in the  same form as shown above. The message up to the first whitespace character (any byte  <= 32) is interpreted as a case-insensitive command used to decide what to do with  the rest of the message.

However, you must first send to Scratch four bytes indicating the message length,  most-significant byte first, i.e.:

<size:4 bytes><msg: size bytes>

So the broadcast “hai!"   message above, which has 15 total characters, must be  preceded by three zero bytes followed by a ‘15’ byte.

In Processing, for example, for strings having a length that can be represented by  one byte, this can be accomplished with a simple function that looks something like:

Code:

void sendtoscratch(String dataOut) {                              
byte [] sizeBytes = { 0, 0, 0, 0 };
  sizeBytes[3] = byte(dataOut.length());
  for (int i=0; i<4; i++) {                  
    ScratchClient.write(sizeBytes[i]);
  }
  ScratchClient.write(dataOut);
}

or in Python, something like (johnm's function):

Code:

def sendScratchCommand(cmd):
    n = len(cmd)
    a = array('c')
    a.append(chr((n >> 24) & 0xFF))
    a.append(chr((n >> 16) & 0xFF))
    a.append(chr((n >>  8) & 0xFF))
    a.append(chr(n & 0xFF))
    scratchSock.send(a.tostring() + cmd)

When you send a sensor-update command to Scratch, it does not update the value of a  global variable: it creates an item in the sensor value pull-down menu so that you can monitor it as though it were any other sensor value.

So, what this all means is that you can write a little middleware program that shuttles messages back and forth between Scratch and whatever other program you can interface with. In the case of the Arduino board, for example, you can write a Processing program that interprets broadcasts from Scratch and then sends instructions to the Arduino board via the serial interface (e.g., using the Firmata firmware).

I'm looking forward to seeing someone build a Scratch-controlled autonomous robot!

Last edited by chalkmarrow (2008-09-03 14:08:46)

Offline

 

#2 2008-09-03 18:52:06

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: Remote Sensor Connections in v1.3

Awesome! I'm learning Ruby now-I'll get right to work on that robot  big_smile !


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#3 2008-09-05 16:25:43

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Remote Sensor Connections in v1.3

johnm has provided some compact Python sample programs that test out remote sensor functionality. I've pasted them below.

Code:

# Scratch client test program
# sends 10 "beat" broadcasts to Scratch

from array import array
import socket
import time

HOST = '127.0.0.1'
PORT = 42001

def sendScratchCommand(cmd):
    n = len(cmd)
    a = array('c')
    a.append(chr((n >> 24) & 0xFF))
    a.append(chr((n >> 16) & 0xFF))
    a.append(chr((n >>  8) & 0xFF))
    a.append(chr(n & 0xFF))
    scratchSock.send(a.tostring() + cmd)

print("connecting...")
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratchSock.connect((HOST, PORT))
print("connected")

for i in xrange(10):
    sendScratchCommand('sensor-update note ' + str(60 + (2 * i)) + ' beats 0.4')
    sendScratchCommand('broadcast "beat"')
    print("beat!")
    time.sleep(0.5)

print("closing socket...")
scratchSock.close()
print("done")

Code:

# Scratch client test program
# Receives and prints data sent by Scratch; use control-C to quit

from array import array
import socket
import time

HOST = '127.0.0.1'
PORT = 42001

print("connecting...")
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratchSock.connect((HOST, PORT))
print("connected! waiting for data...")

# print incoming data forever
while 1:
    time.sleep(0.01)
    data = scratchSock.recv(1024)
    if not data: break
    print(data)

Last edited by chalkmarrow (2008-09-05 16:27:17)

Offline

 

#4 2008-09-05 16:29:48

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Remote Sensor Connections in v1.3

The following is the official Scratch Extension Protocol, by johnm:

------------------------------------

Scratch Extension Protocol

  John Maloney
  September 2, 2008

Introduction

Scratch 1.3 includes an experimental extension feature that supports interaction between Scratch and other programs.

Three kinds of interaction are supported:

  a. sharing broadcasts (in both directions)
  b. virtual sensors
  c. Scratch's global variables are made visible


This document describes this mechanism in sufficient detail to allow people to write their own programs that interact with Scratch in any programming language that supports sockets, such as Python, Java, Ruby, ActionScript, or Squeak.

Protocol

The experimental extension feature is enabled using the right-button menu on one of the two sensor blocks. When remote sensors are enabled, Scratch listens for connections on port 42001.

Once a connection is established, messages are sent in both directions over the socket connection.

Each message consists of a four-byte size field, most-significant byte first, followed by the message itself:

    <size: 4 bytes><msg: size bytes>

The four-byte size field is not counted as part of the message size. Thus, an empty message is four zero bytes.

Message Types

The message up to the first whitespace character (any byte <= 32) is a case-insensitive message type string that is used to decide how to handle the message.

Clients should extract and check the message type string from a message before doing further processing. The set of message types will be extended over time, so client code should be written to skip messages with types that it does not understand. Messages may eventually be used to transmit large amounts of binary data in arbitrary formats. Thus, clients must be prepared to handle (and possibly discard) large messages.

Common Message Types

Most message types contain human-readable strings made up of the following elements:

  - unquoted single-word strings (cat, mouse-x)
  - quoted strings ("a four word string", "embedded ""quotation marks"" are doubled")
  - numbers (1, -1, 3.14, -1.2, .1, -.2)
  - booleans (true or false)


Words and strings are encoded in UTF-8. (Note: ASCII is a subset of UTF-8).

Here are the two most useful message types:

    broadcast <string>
    sensor-update <var-name_1> <new-value_1> ...

A sensor update message includes one or more (variable name, value) pairs. Variable names are strings. Values can be either numbers or quoted strings.

Here is example:

    sensor-update "note" 60 "seconds" 0.1
    broadcast "play note"

The first message sets the value of two virtual sensors named "note" and "seconds". The second broadcasts the "play note" message. A Scratch script might respond to this broadcast by playing a note as specified by the sensor values.

Scratch sends these two message types when broadcasts or global variable changes occur. Scratch also responds to these messages. Broadcast messages sent to Scratch cause a broadcast to occur. Sensor-update messages update the values of virtual sensors available in the sensor block drop-down menu.

Last edited by chalkmarrow (2008-09-05 16:32:35)

Offline

 

#5 2008-09-05 16:35:59

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Remote Sensor Connections in v1.3

A Processing program that allows simple communication between Scratch and an Arduino board is available at:

http://scratchconnections.wik.is/User:Chalkmarrow/Catenary

Last edited by chalkmarrow (2008-09-18 18:20:14)

Offline

 

#6 2008-09-05 16:57:13

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Remote Sensor Connections in v1.3

This is amazing!  Finally, the ability to control something outside of Scratch with a Scratch program!  Great work.  Do you have any (or know of any) sample Arduino projects that demonstrate the sort of things that can be done?


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#7 2008-09-05 17:00:02

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Remote Sensor Connections in v1.3

fullmoon wrote:

Awesome! I'm learning Ruby now-I'll get right to work on that robot  big_smile !

Excellent. Ruby looks like an amazing language, from what I've seen of it. I like that "Shoes" GUI package, which seems really easy to use. If you could mix Shoes and Scratch that might be kinda interesting...

Offline

 

#8 2008-09-05 19:17:57

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Remote Sensor Connections in v1.3

Paddle2See wrote:

This is amazing!  Finally, the ability to control something outside of Scratch with a Scratch program!  Great work.  Do you have any (or know of any) sample Arduino projects that demonstrate the sort of things that can be done?

The arduino playground is a good source:

http://www.arduino.cc/playground/Projects/ArduinoUsers

In general, you can control just about anything, as long as you are willing to buy a few 1K resistors, etc. from Radio Shack. I've found the Arduino to be amazingly robust and easy to use, as compared to the basic stamp, PIC, etc.

btw, if you're curious why Processing is used by artists, check out this project:

http://www.vimeo.com/1582196

Offline

 

#9 2008-09-05 21:37:29

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: Remote Sensor Connections in v1.3

The video looks like jellyfish, not eels! I looked up Shoes, very interesting!


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#10 2008-10-18 19:55:50

stretchyboy
Scratcher
Registered: 2008-10-16
Posts: 11

Re: Remote Sensor Connections in v1.3

Really don't think this is the correct place to put this but can't find reference to a bug tracker.

This doesn't seem to work in presentation mode (as in switching from the main view to presentation in 1.3 on windows freezes the values of the sensors) and also the listening state is not saved in the project so if you just run it will fail to listen. If no one can recreate these issues I will knock up a proper test.

P.S. I have mostly written a prototype Java programme to act as the middle ware between a Nintendo WiiMote and Scratch. I just have to iron out a reliability problem then package it up, should be in the next week or so.

Offline

 

#11 2008-10-18 20:07:32

fullmoon
Retired Community Moderator
Registered: 2007-06-04
Posts: 1000+

Re: Remote Sensor Connections in v1.3

stretchyboy wrote:

P.S. I have mostly written a prototype Java programme to act as the middle ware between a Nintendo WiiMote and Scratch. I just have to iron out a reliability problem then package it up, should be in the next week or so.

Awesome! Can't wait...


http://i302.photobucket.com/albums/nn100/fullmoon32/wow.jpg

Offline

 

#12 2008-10-19 04:28:52

Jens
Scratcher
Registered: 2007-06-04
Posts: 1000+

Re: Remote Sensor Connections in v1.3

remote sensors don't work correctly in presentation mode yet. It's just a small, known glitch which I'm sure will be fixed in the next release. Until then I'm afraid you'll just have to run your remote-sensing projects in edit mode.


Jens Mönig

Offline

 

#13 2008-10-19 10:42:03

ericr
Scratch Team
Registered: 2007-03-21
Posts: 23

Re: Remote Sensor Connections in v1.3

stretchyboy wrote:

P.S. I have mostly written a prototype Java programme to act as the middle ware between a Nintendo WiiMote and Scratch. I just have to iron out a reliability problem then package it up, should be in the next week or so.

Sounds great!  I would strongly suggest just posting it on the wiki, even if it's not perfect yet.  Reliability problems could be hard to debug, and I'm sure people would love to try this out even if they have to deal with some glitches.  Also, if you post your source code you can then have people help you debug it (just like a scratch project)!

Offline

 

#14 2008-10-20 01:48:31

stretchyboy
Scratcher
Registered: 2008-10-16
Posts: 11

Re: Remote Sensor Connections in v1.3

Yeah, that was / is the plan, the bigger problem is that its the first time I've finished writting anything in Java and getting it to work outside eclipse took a lot of doing. I will put it up asap. (the instructions to get it to work are a little involved). actually just thought the source is fairly straight forward. I need to sort out one issue then I'll post it.

I'll also set up a Google code project later.

The code is a bit rubbish (Java isn't my language of choice so its been a lot of hacking about to make it go, and its not very objecty) but it knida works.

Last edited by stretchyboy (2008-10-20 01:58:19)

Offline

 

#15 2008-10-20 03:26:07

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Remote Sensor Connections in v1.3

ericr wrote:

stretchyboy wrote:

P.S. I have mostly written a prototype Java programme to act as the middle ware between a Nintendo WiiMote and Scratch. I just have to iron out a reliability problem then package it up, should be in the next week or so.

Sounds great!  I would strongly suggest just posting it on the wiki, even if it's not perfect yet.  Reliability problems could be hard to debug, and I'm sure people would love to try this out even if they have to deal with some glitches.  Also, if you post your source code you can then have people help you debug it (just like a scratch project)!

/agree. I'd be interested in seeing how that works. I think a lot of people would like to see a wiimote working with Scratch...

Offline

 

#16 2008-10-20 09:53:36

stretchyboy
Scratcher
Registered: 2008-10-16
Posts: 11

Re: Remote Sensor Connections in v1.3

As promised code for linking a WiiMote to Scratch, very definitely early stages but works for me, please give any comments you can.

Just a few things.

It's in Java only because I couldn't get a development set up that would work for C. (I am a PHP / web-coder by trade and just bludgeon through in other languages).
The coding style is really odd as because I don';t know the java conventions sorry.

Here is the link to the proof of concept Scratch game.

http://scratch.mit.edu/projects/stretchyboy/297220

And here is the java code, I'm short of time just at the moment so will set up SVN etc. later.

Code:

/**
 * @author Martyn Eggleton
 * Program for connecting WiiMote to Scratch > 1.3
 * It sends sensor values for all events and also does broadcasts for button presses.
 * 
 * 
 * Code will be released under GPL once i get round to putting source code up.
 * For this you will need wiiusej.jar from http://code.google.com/p/wiiusej/
 * this ships with libwiiuse.so, libWiiuseJ.so wiiuse.dll and WiiUseJ.dll 
 * that all need to be placed in the root of your project.
 * 
 * It is built upon the WiiUse C library so see their documentation
 * (http://wiiuse.net/?nav=docs) for list a of compatible Bluetooth stacks 
 * and hardware.
 * 
 * TODO:
 * Add command line switches to switch on and off various events.
 * Add command line switches to set number of Wiimotes (listeners should be ok up to 4)
 * Add code  Numchucks, Classic Controllers etc. should be trivial.
 * Add receiving broadcasts from Scratch for rumbling and lights.
 * Possibly round the pitch and roll etc to 1 decimal place to shorten the strings sent
 * Possibly re-factor so that Wii2Scrath ran on a timing loop and sent all current values at once. 
 */

    import wiiusej.WiiUseApiManager;
    import wiiusej.Wiimote;
    import wiiusej.values.Orientation;
    import wiiusej.values.GForce;
    import wiiusej.values.RawAcceleration;
    import wiiusej.values.IRSource;
    import wiiusej.wiiusejevents.physicalevents.ExpansionEvent;
    import wiiusej.wiiusejevents.physicalevents.IREvent;
    import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
    import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;
    import wiiusej.wiiusejevents.utils.WiimoteListener;
    import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent;
    import wiiusej.wiiusejevents.wiiuseapievents.NunchukInsertedEvent;
    import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent;
    import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerInsertedEvent;
    import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerRemovedEvent;
    import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroInsertedEvent;
    import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroRemovedEvent;

    import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent;

    import java.io.*;
    import java.net.*;
    import java.util.*;

    public class Wii2Scratch implements WiimoteListener{

           
        /**
         * Opens the port for talking to Scratch
         * and sets up the listeners
         * @param args
         */
        public static void main(String[] args) {    
             try {
                 echoSocket = new Socket("127.0.0.1", 42001);
                 echoSocket.setSoTimeout(100);
                 ScratchClient = echoSocket.getOutputStream();
             } catch (UnknownHostException e) {
                 System.err.println("Don't know about Scratch (port 42001).");
                 System.exit(1);
             } catch (IOException e) {
                 System.err.println("Couldn't get I/O for "
                                    + "the connection to Scratch.");
                 System.exit(1);
             }
            
            Wiimote[] wiimotes = WiiUseApiManager.getWiimotes(1, true);
            Wiimote wiimote = wiimotes[0];
            wiimote.activateIRTRacking();
            wiimote.activateMotionSensing();
            wiimote.addWiiMoteEventListeners(new Wii2Scratch());
            
            sendtoscratch("broadcast \"Wii2ScratchStarted\"");
        }
        
        private static Socket echoSocket;
        private static OutputStream ScratchClient;
        
        
        /**
         * Sends dataOut to Scratch using the format described here
         * http://scratch.mit.edu/forums/viewtopic.php?id=9458
         * @param dataOut
         */
        public static void sendtoscratch(String dataOut){
            //System.out.println(dataOut);
            
            try
            {
                byte [] sizeBytes = { 0, 0, 0, 0 };
                int len = dataOut.length();
          
                sizeBytes[0] =(byte)( len >> 24 );
                sizeBytes[1] =(byte)( (len << 8) >> 24 );
                sizeBytes[2] =(byte)( (len << 16) >> 24 );
                sizeBytes[3] =(byte)( (len << 24) >> 24 );
                
                for (int i=0; i<4; i++) {                  
                    ScratchClient.write(sizeBytes[i]);
                }
                ScratchClient.write(dataOut.getBytes());
            }
            catch(IOException e) {
                System.err.println("Couldn't send "+ dataOut
                        + " to Scratch.");
                System.exit(1);
            }
        }    

        
        /* 
         * Listener for buttons when Just pressed sets a Scratch Sensor with a name like
         * Wii1ButtonA to 1 and sends a broadcast of Wii1ButtonAPressed
         * when its just been release would set Wii1ButtonA to 0 and broadcast Wii1ButtonAReleased
         * 
         * When the Wii1 home button is pressed Wii2Scratch exits after sending the data.
         * 
         * (non-Javadoc)
         * @see wiiusej.wiiusejevents.utils.WiimoteListener#onButtonsEvent(wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent)
         */
        public void onButtonsEvent(WiimoteButtonsEvent arg0) {
            //System.out.println(arg0);
            String sMsg = "";
            String sMsg2 = "";
            Short justPressed = arg0.getButtonsJustPressed();
            Short justReleased = arg0.getButtonsJustReleased();
            String sNamePrefix = "Wii" + arg0.getWiimoteId();
            
            Map<Integer, String> buttonMap = new HashMap<Integer, String>();
            buttonMap.put(128, "ButtonHome");
            buttonMap.put(2, "Button1");
            buttonMap.put(1, "Button2");
            buttonMap.put(8, "ButtonA");
            buttonMap.put(4, "ButtonB");
            buttonMap.put(2048, "ButtonUp");
            buttonMap.put(1024, "ButtonDown");
            buttonMap.put(256, "ButtonLeft");
            buttonMap.put(512, "ButtonRight");
            buttonMap.put(4096, "ButtonPlus");
            buttonMap.put(16, "ButtonMinus");
            
            
            for (Map.Entry<Integer, String> e : buttonMap.entrySet())
            {
                if ((justPressed & e.getKey()) > 0)
                {
                    sMsg = sMsg + " \""+sNamePrefix+e.getValue()+"\" 1";
                    sMsg2 = sMsg2 + " \""+sNamePrefix+e.getValue()+"Pressed\"";
                }
                
                if ((justReleased & e.getKey()) > 0)
                {
                    sMsg = sMsg + " \""+sNamePrefix+e.getValue()+"\" 0";
                    sMsg2 = sMsg2 + " \""+sNamePrefix+e.getValue()+"Released\"";
                }
            }
            
            if (sMsg.length() > 0)
            {
                    sMsg = "sensor-update" + sMsg;
                    sMsg2 = "broadcast" + sMsg2;
                    sendtoscratch(sMsg);
                    sendtoscratch(sMsg2);
            }
            
            if(arg0.isButtonHomePressed())
            {
                System.exit(1);
            }
        }
        
        public boolean bIrPointVisible = false;
        
        
        /* Listener for the IR events.
         * It throws a lot of these which can easily swamp Scratch 
         * so it is now rigged to stop sending one events after it 
         * stops being able to see the lights
         * TODO: add broadcast for can see can't see
         *  
         *  
         * (non-Javadoc)
         * @see wiiusej.wiiusejevents.utils.WiimoteListener#onIrEvent(wiiusej.wiiusejevents.physicalevents.IREvent)
         */
        public void onIrEvent(IREvent arg0) {
            //System.out.println(arg0);
        
            String sMsg = "";
            String sNamePrefix = "Wii" + arg0.getWiimoteId();
            
            
            IRSource[] WiiIRSources = arg0.getIRPoints();
            
            if (WiiIRSources.length == 0)
            {
                if(bIrPointVisible)
                {
                    bIrPointVisible = false;
                }
                else
                {
                    return;
                }
            }
            
            sMsg = sMsg + " \""+sNamePrefix+"IrX\" "+arg0.getX();
            sMsg = sMsg + " \""+sNamePrefix+"IrY\" "+arg0.getY();
            sMsg = sMsg + " \""+sNamePrefix+"IrZ\" "+arg0.getZ();
            sMsg = sMsg + " \""+sNamePrefix+"IrDistance\" "+arg0.getDistance();
            
            
            for(int i=0; i < WiiIRSources.length; i++)
            {    
                sMsg = sMsg + " \""+sNamePrefix+"IrPoint"+(i+1)+"X\" "+WiiIRSources[i].getX();
                sMsg = sMsg + " \""+sNamePrefix+"IrPoint"+(i+1)+"Y\" "+WiiIRSources[i].getY();
                sMsg = sMsg + " \""+sNamePrefix+"IrPoint"+(i+1)+"Rx\" "+WiiIRSources[i].getRx();
                sMsg = sMsg + " \""+sNamePrefix+"IrPoint"+(i+1)+"Ry\" "+WiiIRSources[i].getRy();
                sMsg = sMsg + " \""+sNamePrefix+"IrPoint"+(i+1)+"Size\" "+WiiIRSources[i].getSize();
            }
            
            if (sMsg.length() > 0)
            {
                    sMsg = "sensor-update" + sMsg;
                    sendtoscratch(sMsg);
            }
        }
        

        /* Listener for the motions sensing events
         * Send pitch, roll, yaw and raw acceleration numbers, not currently sending the GForce
         * 
         * (non-Javadoc)
         * @see wiiusej.wiiusejevents.utils.WiimoteListener#onMotionSensingEvent(wiiusej.wiiusejevents.physicalevents.MotionSensingEvent)
         */
        public void onMotionSensingEvent(MotionSensingEvent arg0) {
            //System.out.println(arg0);
            
            String sMsg = "";
            String sNamePrefix = "Wii" + arg0.getWiimoteId();
            
            Orientation WiiOr = arg0.getOrientation();
            sMsg = sMsg + " \""+sNamePrefix+"Pitch\" "+WiiOr.getPitch();
            sMsg = sMsg + " \""+sNamePrefix+"Roll\" "+WiiOr.getRoll();
            sMsg = sMsg + " \""+sNamePrefix+"Yaw\" "+WiiOr.getYaw();
            
            /*
            GForce WiiGForce = arg0.getGforce();
            sMsg = sMsg + " \""+sNamePrefix+"GForceX\" "+WiiGForce.getX();
            sMsg = sMsg + " \""+sNamePrefix+"GForceY\" "+WiiGForce.getY();
            sMsg = sMsg + " \""+sNamePrefix+"GForceZ\" "+WiiGForce.getZ();
            */
            
            RawAcceleration WiiRawAcc = arg0.getRawAcceleration();
            sMsg = sMsg + " \""+sNamePrefix+"GRawAccX\" "+WiiRawAcc.getX();
            sMsg = sMsg + " \""+sNamePrefix+"GRawAccY\" "+WiiRawAcc.getY();
            sMsg = sMsg + " \""+sNamePrefix+"GRawAccZ\" "+WiiRawAcc.getZ();
            
            
            if (sMsg.length() > 0)
            {
                    sMsg = "sensor-update" + sMsg;
                    sendtoscratch(sMsg);
            }
            
            
            //ScratchSet("Moving", "1");
        }

        public void onExpansionEvent(ExpansionEvent arg0) {
            System.out.println(arg0);
        }

        public void onStatusEvent(StatusEvent arg0) {
            //System.out.println(arg0);
        }

        public void onDisconnectionEvent(DisconnectionEvent arg0) {
            System.out.println(arg0);
        }

        public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) {
            System.out.println(arg0);
        }

        public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) {
            System.out.println(arg0);
        }

        public void onClassicControllerInsertedEvent(ClassicControllerInsertedEvent arg0) {
            System.out.println(arg0);
        }

        public void onClassicControllerRemovedEvent(ClassicControllerRemovedEvent arg0) {
            System.out.println(arg0);
        }

        public void onGuitarHeroInsertedEvent(GuitarHeroInsertedEvent arg0) {
            System.out.println(arg0);
        }

        public void onGuitarHeroRemovedEvent(GuitarHeroRemovedEvent arg0) {
            System.out.println(arg0);
        }

        
         }

Last edited by stretchyboy (2008-10-20 09:57:59)

Offline

 

#17 2008-10-20 20:28:33

stretchyboy
Scratcher
Registered: 2008-10-16
Posts: 11

Re: Remote Sensor Connections in v1.3

Hi, I have created an installer & svn repo for Wii2Scratch

http://code.google.com/p/wii2scratch/downloads/list

Should make it easier to use.

Have now made it listen to controls from Scratch borrowing from concepts used by "Catenary", thanks chalkmarrow.

Last edited by stretchyboy (2008-10-23 11:48:34)

Offline

 

#18 2008-10-21 11:25:10

ericr
Scratch Team
Registered: 2007-03-21
Posts: 23

Re: Remote Sensor Connections in v1.3

wonderful.  I'm looking forward to trying this out!

Offline

 

#19 2008-11-18 21:35:49

keithbraafladt
Scratcher
Registered: 2007-03-14
Posts: 25

Re: Remote Sensor Connections in v1.3

Hi - we're sitting at a table with two mac laptops, one intel and one PPC and and an Intel PC laptop.

We're using the Catenary with the Scratch blink project with Arduino boards and we've finally managed to get the Mac PPC and the Win PC to talk on the serial ports, but not the intel-Mac laptop and it feels like brute force on the Macs. We have to start at ^arduinoPort 0 and go to ^arduinoPort 1 and on and on until we find the port that works....

Is there anyway to identify what port in Mac OSX that I can easily find in the Arduino software, called "/dev/tty.usbserial-A4001m5m" as an active port so that I can figure out if its ^arduinoPort 0,1,2,3,4,....

As well is there a place where we can learn how to do inputs as well as outputs in the Scratch/Arduino commands? ( the blink program is all outputs unless we're misunderstanding it.

Offline

 

#20 2008-11-19 11:14:03

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Remote Sensor Connections in v1.3

keithbraafladt: I'm glad you're playing around with this program! I have to say that I'm not a Mac expert, and merely confirmed that Catenary works on my Mac laptop. I believe that I had to use trial-and-error also to figure out which port to use. I'll look into it though.

Regarding inputs, what you need to do, for example, is:

^pinmode 2 input

When a pin is set as an input, a sensor watcher will appear in the sensor blocks having a corresponding name (“Pin2� ). Its value will be an integer that changes as the input changes. For digital inputs (pins 2-13), the value will be 0 or 1. For analog inputs (pins 14-19), the value will be 0-1024 (corresponding to 0-5 V, assuming you are using the on-board power supply).

I'll try to post a helpful Catenary input program for you later today.

Last edited by chalkmarrow (2008-11-19 11:30:16)

Offline

 

#21 2008-11-21 09:39:22

mathguy
Scratcher
Registered: 2008-11-20
Posts: 19

Re: Remote Sensor Connections in v1.3

It's not working for me

Offline

 

#22 2008-11-22 17:16:49

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Remote Sensor Connections in v1.3

Which "it" isn't working? Maybe I can help. Is it the Catenary program, Scratch, Arduino? I admit, there are a lot of things that need to be done in a certain order to play with this feature.

Offline

 

#23 2008-11-24 10:53:58

supermario1
Scratcher
Registered: 2008-11-24
Posts: 2

Re: Remote Sensor Connections in v1.3

Great work. Thanks for sharing, this shall come in handy.

Offline

 

#24 2008-11-24 19:47:05

rauscher
Scratcher
Registered: 2007-05-30
Posts: 16

Re: Remote Sensor Connections in v1.3

Thanks for revealing this wonderful easter egg.  I'm thinking I'll have the kids write some home automation stuff in Scratch now!

Offline

 

#25 2008-11-24 20:51:13

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Remote Sensor Connections in v1.3

rauscher: good idea. there have been some really cool things developed, including a networked version of pong, a clock that is based on the system clock, etc. check out:
http://scratchconnections.wik.is/Projects

i've found that it's easy to teach kids how to set up the control code in scratch for the arduino (via Catenary), but the problem is the hardware side: it's way too easy to "let the smoke out of" microcontrollers when there are no pull-up resistors, buffers, etc. in the picture (as there are with the picoboard).

Offline

 

Board footer