Mindsensors PSP-Nx-V4 - Click to enlargeControlling your robot with a Playstation 2 remote is all well and good but what if you want to use something else?  No worries, with the new Mindsensors Playstation 2 controller interface, you can also get a model that can receive IR signals from a standard TV remote.  Sounds cool?  Well it is.

When I released my Driver Suite yesterday I also included a small program to show the standard commands that the PSP-Nx V4 can receive and act on.  What I didn’t include was a small program to show how you could use the raw value from the sensor to react to custom commands, like the pause button or all the digits.  So I set out to make something simple to demonstrate this feature.  You will note from the picture on the left that there is no controller dongle inserted into the sensor.  All that’s there is the sensor and the little IR receiver on the top.

I programmed it so that I could use the 4 arrows on the remote to control direction and the “OK” button to stop the robot.  I show a small video, so you can see how it looks.

If you’re interested in the source code, check out the program below.  Please note that this will be part of the next Driver Suite release.

#pragma config(Sensor, S1,     PSPNXV4,        sensorI2CCustomFastSkipStates) 
#pragma config(Motor,  motorA,          ARMS,          tmotorNormal, PIDControl, encoder) 
#pragma config(Motor,  motorB,          RIGHT,         tmotorNormal, PIDControl, reversed, encoder) 
#pragma config(Motor,  motorC,          LEFT,          tmotorNormal, PIDControl, reversed, encoder) 
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

/* 
* $Id$ 
*/

/** 
* PSPV4-driver.h provides an API for the Mindsensors PS2 controller sensor 
* with referee signal receiver.  This program demonstrates how to use that API 
* 
* Changelog: 
* - 0.1: Initial release 
* 
* Credits: 
* - Big thanks to Mindsensors for providing me with the hardware necessary to write and test this. 
* 
* License: You may use this code as you wish, provided you give credit where it's due. 
* 
* THIS CODE WILL ONLY WORK WITH ROBOTC VERSION 3.08 AND HIGHER. 
* Xander Soldaat (mightor_at_gmail.com) 
* 02 August 2012 
* version 0.1 
*/

#include "drivers/MSPSPV4-driver.h"

#define RAW_RIGHT   0x45B 
#define RAW_LEFT    0x45A 
#define RAW_FWD     0x458 
#define RAW_REV     0x459 
#define RAW_STOP    0x45C

#define goBotRight()  moveBot(-20, 20) 
#define goBotLeft()   moveBot(20, -20) 
#define goBotFwd()    moveBot(50, 50) 
#define goBotRev()    moveBot(-50, -50) 
#define goBotStop()   moveBot(0, 0)

void moveBot(int speedRight, int speedLeft) 
{ 
  motor[RIGHT] = speedRight; 
  motor[LEFT] = speedLeft; 
}

task main () 
{ 
  ubyte counter = 0; 
  ubyte oldCounter = 0; 
  long rawValue = 0; 
  long oldRawValue = 0;

  nxtDisplayCenteredTextLine(0, "Mindsensors"); 
  nxtDisplayCenteredBigTextLine(1, "PSP-Nx"); 
  nxtDisplayCenteredTextLine(3, "Test 3"); 
  wait1Msec(2000);

  PlaySound(soundBeepBeep); 
  while(bSoundActive) EndTimeSlice();

  eraseDisplay(); 
  while (true) 
  { 
    // Get the current counter value, wraps at 255 
    counter = PSPV4readRefSignalCount(PSPNXV4);

    if (oldCounter != counter) 
    { 
      // Raw value will also "see" commands from the remote 
      // that are not recognised as "play", "stop", "forward" or "rewind". 
      // You could use this to control additional aspects of your robot! 
      rawValue = PSPV4readRawRefTXValue(PSPNXV4);

      if (oldRawValue != rawValue) 
      { 
        PlaySound(soundShortBlip); 
        switch(rawValue) 
        { 
          case RAW_RIGHT:   nxtDisplayCenteredBigTextLine(3, "RIGHT"); 
                            goBotRight(); 
                            break; 
          case RAW_LEFT:    nxtDisplayCenteredBigTextLine(3, "LEFT"); 
                            goBotLeft(); 
                            break; 
          case RAW_FWD:     nxtDisplayCenteredBigTextLine(3, "FWD"); 
                            goBotFwd(); 
                            break; 
          case RAW_REV:     nxtDisplayCenteredBigTextLine(3, "REV"); 
                            goBotRev(); 
                            break; 
          case RAW_STOP:    nxtDisplayCenteredBigTextLine(3, "STOP"); 
                            goBotStop(); 
                            break; 
        } 
      } 
      nxtDisplayTextLine(7, "Raw:  0x%03X", rawValue);

      // Update the counters and signals 
      oldCounter = counter; 
      oldRawValue = rawValue; 
    } 
    wait1Msec(50); 
  } 
}

/* 
* $Id$ 
*/

Tags