> | | > Testing Multiple Controllers

Testing Multiple Controllers

Posted on Sunday 13 January 2013 | No Comments

In order for the two robots to be controlled effectively, it is necessary to receive and differentiate between messages sent from two controllers.  The TouchOSC app allows for the IP address of the host to be configured, along with the port of the application to send messages to.  The port of received messages on the iPad can also be configured, this is generally left as default.  At the bottom is the iPads IP address, which is required to send messages back to the controller to control LEDs and labels on screen. The configuration screen is shown below.
TouchOSC configuration screen

Sending Messages

Using some of the examples on the OscP5 library's website it was possible to create some prototype code to send OSC messages back to the controller, to update labels and turn on LEDs etc. The processing code below updates the player's score label.

import oscP5.*;
import netP5.*;

OscP5 oscP5;
NetAddress myRemoteLocation;

void setup() {
  size(400,400);
  frameRate(25);
  /* start oscP5, listening for incoming messages at port 8000*/
  oscP5 = new OscP5(this,8000);
  
  /* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
   * an ip address and a port number. myRemoteLocation is used as parameter in
   * oscP5.send() when sending osc packets to another computer, device, 
   * application. 
   */
  myRemoteLocation = new NetAddress("192.168.0.5",9000);
}


void draw() {
  background(0);  
}

void mousePressed() {
/*In this method, a message is sent to the remote iPad*/
  OscMessage myMessage = new OscMessage("/1/PlayerScore");

  myMessage.add("17"); //Changes label to show the number 17.

  /* send the message */
  oscP5.send(myMessage, myRemoteLocation); 
}


/* incoming osc message are forwarded to the oscEvent method. */
void oscEvent(OscMessage theOscMessage) {
  /* print the address pattern and the typetag of the received OscMessage */
  print("### received an osc message.");
  print(" addrpattern: "+theOscMessage.addrPattern());
  println(" typetag: "+theOscMessage.typetag());
}
The processing sketch can therefore have two NetAddresses stored, one for controller 1, and one for controller 2.  It is also possible to bundle messages up, for example all messages required to update the entire screen can be sent together.  This will probably be used to sent a set of messages together to one iPad, and then the other.  For example, changing the mousePressed method above to include bundling would show:
void mousePressed() {
  /* create an osc bundle */
  OscBundle myBundle = new OscBundle();
  
  /* createa new osc message object */
  OscMessage myMessage = new OscMessage("/1/DriveOKLED");
  myMessage.add("green"); //Set LED to green
  
  /* add an osc message to the osc bundle */
  myBundle.add(myMessage);
  
  /* reset and clear the myMessage object for refill. */
  myMessage.clear();
  
  /* refill the osc message object again */
  myMessage.setAddrPattern("/1/DriveOKLED");
  myMessage.add(1); //Turn on LED
  myBundle.add(myMessage);
  
  /* send the osc bundle, containing 2 osc messages, to a remote location. */
  oscP5.send(myBundle, myRemoteLocation);
}
This code switches an LED to the colour green, and turns it on in the one transmission.

Receiving Messages

The only change made to receiving messages is the addition of parsing for the '/1/' or '/2/' tag.  This tag shows which controller the control message has come from.  Both controllers send to the same port on the games master machine, so this is the only way of distinguishing between them.

Leave a Reply

Theme MXS. Powered by Blogger.