
This PSP-NX sensor (www.mindsensors.com) can receive signals from the Sony Playstation controller and this input can be used to steer the motors or activate other sensors on the brick. In this sense this PSP-NX sensor is not a real sensor, not measuring "physical" data like light or sound. On the other hand it is funny to be able to steer the NXT around with the conveniance of this controler. Really convenient. The PSP is base on I2C so it is not the fasted around. Of course, because activating commands must be sent to the I2C sensor chip, this sensor has to be controlled in a prog from the brick, like the Camera, no bluetooth control of this sensor. We made the JAVA lejos class for this sensor: The only problem was (again, like the clock sensor) the strange handling of the COMMAND address by lejos JAVA: is has to be divided by 2!!! (See also the clock sensor class in this blogg) For the rest the buttons1 and button2 have to be decoded: Button 2 digits: 1 left down 2 right down 4 left up 8 right up triangle 16 circle 32 cross 64 square 128 Button1 digits: 1 select 8 start 16 left circle up 32 left circle left 64 left circle down 128 left circle left then the joysticks, the mode has to be analog, and then you have a nice range of 0 - 255 for both x and y directions
//JAVA class: import lejos.nxt.*; public class playstation extends I2CSensor implements SensorConstants { // Mindsensors.com constants: private final static byte COMMAND = 0x02 / 2;//here the strange lejos divide by 2 to get address private final static byte SENDADDRESS = 0x41 / 2;//to send the activation etc byte[] buf = new byte[2]; SensorPort myPort; public playstation(SensorPort port) { super(port); myPort = port; activate(); pause(100); analog(); pause(100); } public void activate() { buf[0] = 'E'; super.sendDataSENDADDRESS , buf, 1);//energize } public void deactivate() { buf[0] = 'D'; super.sendData(SENDADDRESS , buf, 1);//power off } public void digital() { buf[0] = 'A'; super.sendData(SENDADDRESS , buf, 1);//digital mode } public void analog() { buf[0] = 's'; super.sendData(SENDADDRESS , buf, 1);//analog mode (for working joysticks) } public void ADPAON() { buf[0] = 'N'; super.sendData(SENDADDRESS , buf, 1);//connecting more sensors to one sensorport } public void ADPAOFF() { buf[0] = 'O'; super.sendData(SENDADDRESS , buf, 1);//not connecting more sensors to one sensorport } public int getData(int register, byte [] buf, int len) { int ret = myPort.i2cStart(COMMAND , register, len, buf, len, 0); if (ret != 0) return ret; while (myPort.i2cBusy() != 0) { Thread.yield(); } return 0; } public String getMyProductID() { String version = " "; char [] versionChars = StringUtils.getCharacters(version); byte[] byteBuff = new byte[8]; int ret = getData(0x00, byteBuff, 8); for(int i=0;i<8;i++) { versionChars[i] = (ret == 0 ? (char) byteBuff[i] : ' '); } return version; } public String getMyDeviceID() { String version = " "; char [] versionChars = StringUtils.getCharacters(version); byte[] byteBuff = new byte[8]; int ret = getData(0x10, byteBuff, 8); for(int i=0;i<8;i++) { versionChars[i] = (ret == 0 ? (char) byteBuff[i] : ' '); } return version; } public String getMyVendorID() { String version = " "; char [] versionChars = StringUtils.getCharacters(version); byte[] byteBuff = new byte[8]; int ret = getData(0x08, byteBuff, 8); for(int i=0;i<8;i++) { versionChars[i] = (ret == 0 ? (char) byteBuff[i] : ' '); } return version; } public int Button1() { int ret = getData(0x42, buf, 1); return (ret == 0 ? ( 255 - buf[0] & 0xFF ) : -1); } public int Button2() { int ret = getData(0x43, buf, 1); return (ret == 0 ? ( 255 - buf[0] & 0xFF ) : -1); } public int xLeftJoystick() { int ret = getData(0x44, buf, 1); return (ret == 0 ? (buf[0] & 0xFF ) : -1); } public int yLeftJoystick() { int ret = getData(0x45, buf, 1); return (ret == 0 ? (buf[0] & 0xFF ) : -1); } public int xRightJoystick() { int ret = getData(0x46, buf, 1); return (ret == 0 ? (buf[0] & 0xFF ) : -1); } public int yRightJoystick() { int ret = getData(0x47, buf, 1); return (ret == 0 ? (buf[0] & 0xFF ) : -1); } private static void pause(int time) { try{ Thread.sleep(time); } catch(InterruptedException e){} } } |
0 comments:
Post a Comment