Making Things + AS3 - Part 3 Servo Mouse Tracking

Be sure to go to my previous post here so you are up to speed.

For this example we will simply get the servo connected to the board and have it read the mouse position of the screen. When you move your mouse to the left, the servo will turn accordingly and vice versus.

At this point it would also me good for me to point that the way in which we are sending data to flosc might not be ideal. I noticed in the making things AS3 example and source that's to be used specifically with mchelper, they have a communication method for bundling data together. Not sure if this is workable with flosc. While I'm on the topic, I'd also suggest you check that code out. You can find their AS3 examples and library here. I plan on experimenting with the mchelper method of using AS3 and the board shortly.

package com.hydrotik.make {
       
        import flash.display.Shape;
        import flash.display.Sprite;
        import flash.events.MouseEvent;
        import flash.utils.Timer;
        import flash.events.Event;
        import flash.events.TimerEvent;
        import flash.net.URLRequest;
        import flash.media.Sound;
        import flash.media.SoundChannel;
        import org.fwiidom.osc.*;
       
        public class MakeServo extends Sprite {
               
                private static const STR_LOCAL_IP:String = "169.254.73.165″;
               
                private static const STR_REMOTE_IP:String = "192.168.0.200″;
               
                private static const NUM_PORT:Number = 10000;
               
                private var oscConn:OSCConnection;
               
                public function MakeServo() {
                        //Initialize connection to the FLOSC server
                        oscConn = new OSCConnection(STR_LOCAL_IP, NUM_PORT);
                        oscConn.addEventListener(OSCConnectionEvent.ON_CONNECT, onConnect);
                        oscConn.addEventListener(OSCConnectionEvent.ON_CONNECT_ERROR, onConnectError);
                        oscConn.addEventListener(OSCConnectionEvent.ON_PACKET_IN, onPacketIn);
                        oscConn.addEventListener(OSCConnectionEvent.ON_PACKET_OUT, onPacketOut);
                        oscConn.addEventListener(OSCConnectionEvent.ON_CLOSE, onClose);
                        oscConn.connect();
                }
               
                private function onConnect(evtEvent:OSCConnectionEvent):void {
                        trace("FLOSC Connection established");
                        this.addEventListener(Event.ENTER_FRAME, trackServo);
                }
       
                private function onConnectError(evtEvent:OSCConnectionEvent):void {
                        trace("FLOSC Connection error");
                }
               
                private function onClose(evtEvent:OSCConnectionEvent):void {
                        trace("FLOSC Connection closed");
                }
               
               
                private function onPacketIn(evtOSC:OSCConnectionEvent):void {
                        trace("\t>> data received: " + evtOSC.data.name + " " + evtOSC.data.data);     
                }
               
                private function onPacketOut(evtOSC:OSCConnectionEvent):void {
                        trace("\t>> data sent: " + evtOSC.data.name + " " + evtOSC.data.data)
                }
               
                private function trackServo(event:Event) {
                        var pos:int = (this.mouseX/200) * 1023;
                        trace(pos);
                        oscConn.sendOSCPacket(
                                new OSCPacket("/servo/0/position", [pos], STR_REMOTE_IP, NUM_PORT)
                        );
                }
               
               
        }
       
}

Making Things Servo Source

Leave a Reply