what the blog?

{upgrade in progress..}

1

Build up a iPad Plarail Patrol with Arduino + OSC

Introduction

This tutorial give you an idea to build up a iPad controlled Parail. Please read my Arduino OSC step by step howto post if you don’t know how to work with Arduino.

1. Hardware Setup

  • Show you how they connected together.
  • The network setup
  • Attach the servo on Plarail component(Rail, Station)

2. Software Setup

  • The exact Arduino coding
  • Sample TouchOSC layout

1. Hardware Setup

Let’s take a look the setup diagram, see how this connected.

  • A servo connected to Digital Pin 19, 20, 21
  • Arduino ethernet assigned a IP 192.168.0.178
  • iOS will be assigned a IP 192.168.0.12 by DHCP

Hardware Components

Components

You need the following component to construct your setup (Shown by bottom up) 1. Arduino board (Mega 2560, UNO…etc) 2. Sensor Shield (Optional) 3. Wireless Module or Wireless router (Optional if run with wired Arduino network) 3. Low power consumption servo unit, recommend a mini or micro sized servo. 4. Power supply, for example 7.4v recommend for wireless, lan, servos at the same time. 5. Wireless AP/Router, DHCP enabled. (For iPad)

Servo extension cable connected to Digital Pin 19,20,21.

The network cable connecting from the wireless module to lan module. Why? because this module only support wireless serial comm., so it cannot setup to listen network socket. Therefore, I configure the modue as a wireless AP that bridge the LAN interface with my router. you may use any router as you like instead of stacking those module like that. Please read my Arduino OSC step by step howtopost to see how to work with generic router.

Plarail Component Setup

You need to work out the servo position with your plarail component, for me, I will just patch it underneath, you may use any skill to make them work together. http://www.youtube.com/watch?v=Hv9IVDAbao0 http://www.youtube.com/watch?v=spidtmUTUuo

2. Software Setup

Arduino Code

Modify IP AddressesYou may change the IP Address for the Arduino and your iOS/Android to fit your setup. For example: 192.168.0.12 on iOS Then search “myIp” line

byte myIp[]  = { 192, 168, 0, 178 }; // Specify your arduino  IP here

and the “destIp” line.

byte myIp[]  = { 192, 168, 0, 12 }; // your IPad Address here<

 


// Receive from iOSC http://recotana.com/iphone/iosc/en/index.html and apply to arduino pins via OSC .
// Credits to Jerome Dupraz & Retocama
// Colorsound 011
// you can download arduino z-osc library from here https://github.com/djiamnot/Z_OSC
// you can download arduino code and vvvv patch from here: http://vvvv.org/contribution/arduinoosc

/*

This is a sample code for controlling servo via TouchOSC on

*/

#include
#include
#include
#include

byte myMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte myIp[]  = { 192, 168, 0, 178 }; // Specify your arduino IP
int  serverPort  = 8000; // and listening port as well

byte destIp[] =  { 192, 168, 0, 12 }; // Talk back with your iPad, not critical
int  destPort = 9000;

float val;// delcare a variable for reading values

// create servo object to control a servo, remember to attach them in setup()
Servo myservo1;
Servo myservo2;
Servo myservo3;

Z_OSCClient client;
Z_OSCServer server;
Z_OSCMessage *rcvMes;
Z_OSCMessage message;

void setup(){
 myservo1.attach(19); // attaches the servo on pin 19 to the servo object
 myservo2.attach(20); // attaches the servo on pin 20 to the servo object
 myservo3.attach(21); // attaches the servo on pin 21 to the servo object
 Serial.begin(19200);
 Ethernet.begin(myMac ,myIp);
 server.sockOpen(serverPort);
}

void loop(){
 if(server.available()){
  message.setAddress(destIp,destPort);
  rcvMes=server.getMessage();

     // myservo1
     if( !strcmp( rcvMes->getZ_OSCAddress() ,  "/1/toggle1" ) )
     {
      val = rcvMes->getFloat(0);
      myservo1.write(val);
      if ( val > 0 ) {
          message.setZ_OSCMessage("/1/toggle1_msg" ,"s" , "Stopping at Tsuen Wan" );
          client.send(&message);
      } else {
          message.setZ_OSCMessage("/1/toggle1_msg" ,"s" , "Tsuen Wan Station" );
          client.send(&message);
      }

     }

      // myservo2
     if( !strcmp( rcvMes->getZ_OSCAddress() ,  "/1/toggle2" ) )
     {
      val = rcvMes->getFloat(0);
      myservo2.write(val);

      if ( val > 0 ) {
          message.setZ_OSCMessage("/1/toggle2_msg" ,"s" , "Stopping at Lai King" );
          client.send(&message);
      } else {
          message.setZ_OSCMessage("/1/toggle2_msg" ,"s" , "Lai King Station" );
          client.send(&message);
      }

     }

     // myservo3
     if( !strcmp( rcvMes->getZ_OSCAddress() ,  "/1/fader1" ) )
     {
      val = rcvMes->getFloat(0);
      if ( val == 20 ) {
              myservo3.write(0);
              message.setZ_OSCMessage("/1/fader1_msg" ,"s" , "Bypass" );
              client.send(&message);
      } else
      if ( val == 150 ) {
              myservo3.write(150);
              message.setZ_OSCMessage("/1/fader1_msg" ,"s" , "Normal" );
              client.send(&message);
      }

     }
 }
}

TouchOSC

Download and modify this sample Plarail TouchOSC Layout

OSC Objects

This sample template, include 1 fader, 2 toggle button for triggering the servo movement, text label area that receive arduino text message.

Toggle button 1

It consist two important parameters used to communicate with arduino and servo. 1. OSC Name: /1/toggle1 /1/toggle1 is corresponding to getZ_OSCAddress line in Arduino code. 2. Value Range: 0 To 62This value specify two exact degree angle of the servo while the toggle switch trigged, please adjust it as you like.

Toggle button 2

It consist two important parameters used to communicate with arduino and servo. 1. OSC Name: /1/toggle2 /1/toggle2 is corresponding to getZ_OSCAddress line in Arduino code. 2. Value Range: 0 To 62This value specify two exact degree angle of the servo while the toggle switch trigged, please adjust it as you like.

Fader button 1

It consist two important parameters used to communicate with arduino and servo. 1. OSC Name: /1/fader1 /1/faderis corresponding to getZ_OSCAddress line in Arduino code. 2. Value Range: 20 To 150This value specify two exact degree angle of the servo while the toggle switch trigged, please adjust it as you like.

Toggle Msg labels

This label area is receiving text message where send back from Arduino, we leave the “text” field empty first. When the servo triggered, Arduino send back a word “Moved” in the label area. OSC Name: /1/toggle1_msg, /1/toggle2_msg, /1/fader1_msgThe OSC name is corresponding to “setZ_OSCMessage” in Arduino code.

arduinooscPlarail

levin • March 11, 2013


Previous Post

Next Post

Comments

  1. 用Arduino+OSC建立一个iPad铁路王国巡视机

Leave a Reply