Posts Tagged ‘compass’
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.
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.
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)
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)
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.
Build a Custom Arduino (Part 3) – Add a Compass
This post is the last part of this series of articles showing how to build a project using a custom Arduino board. To the accelerometer I added a compass. It begins to look more like a forest than an electronic circuit.
The compass uses the IC2 protocol to communicate with the Arduino micro-controller. That make it more complicated to interface with than the accelerometer. This protocol is handled by the library Wire.h that comes standard with the Arduino API.
In this example, you connect the SCL pin of the compass to the analog input #5 (pin #28) and the SDA pin to the analog input #4 (pin #27) of the micro-controller. As with the accelerometer, the compass is powered with the 3.3v output of the micro-controller on pin #20.
#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(100);
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]; // Put the MSB and LSB together
Serial.print("Current heading: ");
Serial.print(int (headingValue / 10)); // The whole number part of the heading
Serial.print(".");
Serial.print(int (headingValue % 10)); // The fractional part of the heading
Serial.println(" degrees");
x = analogRead(0);
y = analogRead(1);
z = analogRead(2);
Serial.print("acceleretations are x, y, z: ");
Serial.print(x, DEC);
Serial.print(" ");
Serial.print(y, DEC);
Serial.print(" ");
Serial.println(z, DEC);
//pin13 = !pin13;
//digitalWrite(13,pin13);
delay(400);
}
The output will refresh twice a seccond and display the compass heading and the accelerometer three axis values.
I had a problem of interference between the compass and the small green LED. When the LED is turned on, the compass reading is off by up to 10 degrees. I can’t explain that one but, when the LED is disables in the code, the compass readings is back to normal and stable. If you have an explanation for that please let me know in the comment section below.






