Posts Tagged ‘Wireless’

Wireless Arduino/XBee Game Controller

For this project, I keep the same controller as in the previous post but, instead of controlling a servo motor  through a second Arduino, I will use it as a game controller.

The game will be played on my desktop computer. Since I am not very good at writing games, I downloaded the PyGame library. This framework comes with tons of samples so I decided to hack one of these samples and replace the keyboard input with my controller. You move the tank left and right by turning the game controller in the vertical position. You can fire bombs by switching the controller horizontally.

PyGame example game aliens.py

PyGame example game aliens.py

The code and the circuit of the game controller are the exact same setup as in my previous post. What I’ll demonstrate here is how to implement a receiver in this Python game.

You will need to connect an XBee wireless module to your computer using a USB adapter board. This will create a new communication port, in our case COM8.

Arduino/XBee controller and XBee USB adapter

Arduino/XBee controller and XBee USB adapter

The idea is to replace the keyboard with the data coming from the controller through the serial port. To maintain compatibility with the original version, I decided to simply override the keyboard values with the wireless controller commands.

The first step is to load the proper modules and open a connection through the serial port. I will use two global variables to replace the keyboard commands: one for the firing command and the other for the tank direction.

#
# Serial port communication with the Arduino/XBee
#
import serial
import thread
arduinoFiring = 0
arduinoDirection = 0
ser = serial.Serial('COM8', 19200, timeout=0) # Change COM[number] for ttyUSB[number]

Once this is defined, we need to override the keyboard values. To replace the spacebar fire command, we override the value with:

  firing = keystate[K_SPACE]
  firing = arduinoFiring # <-- Override the keyboard with the game controller data

To replace the arrows direction commands, we are add the following lines:

  direction = keystate[K_RIGHT] - keystate[K_LEFT]
  direction = arduinoDirection # <-- Override the keyboard with the game controller value

Finally, we have to feed these two values with the data received from the game controller. The best way to do this is by starting a new thread to listen on the serial port, read incoming data and convert it to game usable values.

def processArduino(buffer):
	global arduinoFiring
	global arduinoDirection

	if len(buffer) == 0:
		return
	if buffer[0] != '<':
 		return
 	if buffer[len(buffer)-1] != '>':
		return

	tokens = buffer[1:len(buffer)-1].split(':')
	if len(tokens) < 4:
 		return
 	for i in range(4):
 		if len(tokens[i]) == 0:
 			return
	#print(buffer)
 	c = int(tokens[0])
 	x = int(tokens[1])
 	y = int(tokens[2])
 	z = int(tokens[3])
 	if y > 550:
		arduinoDirection = 1
	elif y < 450:
 		arduinoDirection = -1
 	else:
 		arduinoDirection = 0
 	if z > 575:
		arduinoFiring = 1
	else:
		arduinoFiring = 0

def readArduino():
	buffer = ''
	while 1:
		b = ser.read()
		while b:
			if b == '<':
 				buffer = ''
 			buffer += b
 			if b == '>':
				processArduino(buffer)
			b = ser.read();

def main(winstyle = 0):
    ...
    thread.start_new_thread(readArduino, ())

We have two functions here. The readArduino() function is the thread entry point that contains the code reading on the serial port. A buffer is built of bytes received from the game controller. When the ‘<’ character is read, the buffer is cleared to receive a new command. When the character ‘>’ is received, the command stored in the buffer is processed.

The data is processed in the  function processArduino(buffer). The data buffer is received as a parameter. The function reads the three axis and the compass values. Then, it determines if the direction is -1, 0 or 1 and if the tank fires a bomb. The values of the axis range from 400 to 600. The constants used in the function to determine the position of the controller can be adjusted to match your game requirements.

 

Wireless Arduino/XBee Remote Control with Compass and Accelerometer

A few weeks ago we played with XBee wireless devices. Last week we played with accelerometers and compass. This week we will put it all together in a custom remote control. Our first project will be to remotely control a servo motor by using two Arduinos and two XBee modules.

The remote control (transmitter) should be straight forward to build using our last project and replacing the USB interface with the XBee module.

The wireless servo controller (receiver) on the other hand needs to be built from scratch. We will connect an XBee shield and a servo motor to an Arduino board. The Arduino receives the commands from the serial port and sets the servo position accordingly.

Remote Control (ransmitter)

Arduino/XBee remote control with accelerometer and compass

Arduino/XBee remote control with accelerometer and compass

Let’s start with the transmitter. Instead of using a simplified Arduino board as in our previous articles, we will be using a standard Arduino board, an XBee shield and a Proto Shield all stacked together. The prototyping board is used to make the circuit for the accelerometer and the compass sensors.

Both sensors are powered with 3.3v from the Arduino. The accelerometer X, Y and Z axis are respectively connected to the Arduino analog pins #0, #1 and #2. The compass SDA and SCL outputs for the IC2 protocol are connected to the Arduino analog pins #4 and #5.

The remote control is powered by a 6V NiMH rechargeable battery pack. The battery is connected to the Vin and Gnd beaders of the Arduino board.

Following is the program of the remote control used to read the sensors and send data to the receiver:

#include <Wire.h>

int x, y, z;
int pin13 = LOW;

int slaveAddress;
byte headingData[2];
int i, headingValue;
int HMC6352Address = 0x42;

void setup()
{
  Wire.begin();
  slaveAddress = HMC6352Address >> 1;  

  pinMode(13, OUTPUT);
  Serial.begin(19200);
}
void loop()
{
  // Send a "A" command to the HMC6352
  Wire.beginTransmission(slaveAddress);
  Wire.send("A");  // The "Get Data" command
  Wire.endTransmission();
  delay(10);

  i = 0;
  Wire.requestFrom(slaveAddress, 2);        // Request the 2 byte heading (MSB comes first)
  while(Wire.available() && i < 2)
  {
    headingData[i] = Wire.receive();
    i++;
  }

  headingValue = headingData[0]*256 + headingData[1];

  Serial.print("<");
  Serial.print(int (headingValue / 10));
  Serial.print(":");      

  x = analogRead(0);
  y = analogRead(1);
  z = analogRead(2);
  Serial.print(x, DEC);
  Serial.print(":");
  Serial.print(y, DEC);
  Serial.print(":");
  Serial.print(z, DEC);
  Serial.print(">");

  delay(190);
}

Wireless Servo Controller (receiver)

Arduino/XBee wireless servo controller

Arduino/XBee wireless servo controller

To build the receiver you will need both an Arduino board and an XBee shield along with a servo motor. Connect the XBShield onto the Arduino board and the servo motor data wire to the Arduino pin #10. The pins #9, #10 and #11 of the Arduino are Pulse With Modulation (PWM) outputs. A pulse can be sent to these outputs to control a servo motor.

I have used a 9V battery to power the receiver but I would strongly recommend a rechargeable battery pack. The 9V battery will not last a long time driving the servo motor.

Following is the code of the receiver:

#include <Servo.h>

#define BUFFER_MAX 24
#define CENTER_MIN 490
#define CENTER_MAX 510
#define MAX_MAX 590
#define MON_MIN 410

Servo servoA;

byte counter = 0;
char buffer[BUFFER_MAX];

void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(19200);
  servoA.attach(10);
}

void loop() {
  if (Serial.available()) {
    char c = (char) Serial.read();
    if(c == '<')
    {
      digitalWrite(13,HIGH);
      counter = 0;
      memset( buffer, 0, BUFFER_MAX);
    }
    else if(c == '>')
    {
      digitalWrite(13,LOW);
      processMessage();
    }
    else
    {
      if(counter == BUFFER_MAX)
        counter = 0;

      buffer[counter++] = c;
    }    

    delay(10);
  }
}

void processMessage()
{
//  Serial.println(buffer);
//  Serial.println(counter, DEC);

  int count = 0;
  int tokenCount = 0;
  char tokenValue[4];
  int tokenValues[4];
  for(int i=0; i<=counter; i++)
  {
    if(buffer[i] == ':' || i == counter)
    {
      tokenValues[tokenCount++] = atoi(tokenValue);
      count = 0;
      memset(tokenValue, 0, 4);
    }
    else
    {
      tokenValue[count++] = buffer[i];
    }
  } 

  if( tokenValues[3] > MAX_MAX )
  {
    int servoValue = ((int)tokenValues[0]) - 180;
    if(servoValue < 0)
      servoValue *= -1;
    servoA.write(servoValue);
  }
  else
  {
    int angle = 90;
    if(tokenValues[2] < CENTER_MIN)
      angle = 180 - (tokenValues[2] - 400);
    if(tokenValues[2] > CENTER_MAX)
      angle = 600 - tokenValues[2];

    if(angle > 180)
      angle = 180;
    if(angle < 0)
      angle = 0;

    servoA.write(angle);
  }

}

XBee Configuration

To configure the XBShield you can refer to the ladyada website. First when using the XBShield, make sure to set the on-board switch to the right position at the right time. Then, when uploading a sketch to the board, place the switch on the DLINE position. And lastly, when using the XBee as serial port, set the switch to UART.