I am trying to take my Python script that reads two joysticks on a USB PlayStation 3 controller and then send it wirelessly to a router connected to my arduino via a ethernet shield.
Currently, I can read my controller and send its data over serial to my Arduino. From there, the Arduino can process the information in order to drive two motor controllers (Jaguar speed controllers). I am trying to get the data sent wirelessly. I would like to be able to hook my TP-LINK router to my Ethernet shield and send data to it. I am not sure if this will change my code completely. I have attached what I currently programmed in both Python and the Arduino language.
My goal is to create a robot that I can controller with just my PlayStation controller plugged into my computer. It will read the data and send it to the router attached to the Arduino wirelessly. From there, the Arduino will be able to read the incoming data and process it into motor controls (for a tank drive robot, each joystick for both sides of the robot). I have it working properly when all connected. I just need to make things wireless. Hopefully this is not too difficult of a task. Help would be very appreciated. Thanks a lot!
Python code: (Reading Controller Attempt)
#!/usr/bin/env python
import pygame, serial, os, sys, time #, msvcrt
print "Opening Serial port..."
from pygame import locals
arduino = serial.Serial(2, 9600, timeout = 1)
time.sleep(2)
print "Initialize Complete"
pygame.init()
pygame.joystick.init() # main joystick device system
try:
j = pygame.joystick.Joystick(0) # create a joystick instance
j.init() # init instance
print 'Enabled joystick: ' + j.get_name()
print "55,55;"
arduino.write("55,55;")
except pygame.error:
print 'No joystick found.'
while 1:
for e in pygame.event.get(): # iterate over event stack
if e.type == pygame.locals.JOYAXISMOTION: # Read Analog Joystick Axis
y1 = j.get_axis(1) # Left Stick
y2 = j.get_axis(3) # Right Stick
#y1 = (y1 * 255) + 255
#y2 = (y2 * 255) + 255
y1 = (y1 * 45) + 55
y2 = (y2 * 45) + 55
leftStick = int (y1)
rightStick = int (y2)
leftStick = str (leftStick)
rightStick = str (rightStick)
#leftCommand = 'L%s' % leftStick
#rightCommand = 'R%s' % rightStick
arduinoCommand = leftStick
arduinoCommand += ','
arduinoCommand += rightStick
arduinoCommand += ';'
print arduinoCommand
arduino.write(arduinoCommand)
#time.sleep(.1)
Arduino code: (TankDriveRobot)
//
#include <avr/interrupt.h>
const int leftjagsigpin = 9; //pwm connection on pin 9
const int leftjagpwrpin = 7;//using pin 7 as the pwm power reference
const int leftjaggndpin = 8;//using pin 8 as the pwm ground reference
//const int ledpin = 13;//using the standard LED as a status indicator
//const int buffsize = 9;//determining the digits buffer size
const int rightjagsigpin = 10; //pwm connection on pin 9
const int rightjagpwrpin = 2;//using pin 7 as the pwm power reference
const int rightjaggndpin = 4;
//char mybuffer[buffsize]; //declare an array to take input from the serial
int zeroValue = 47;// decimal 47 corresponds with the pwm midpoint
//int minvalue = 20;// decimal 20 corresponds with the pwm full reverse
//int maxvalue = 74; //decimal 74 corresponds with the pwm full forward
//char inChar;
//byte index = 0;
//int leftRead;
//int rightRead;,
//int leftMotor;
//int rightMotor;
//boolean ledstatus = LOW; //initialize the LED status
const int NUMBER_OF_FIELDS = 2; // how many comma separated fields we expect
int fieldIndex = 0; // the current field being received
int values[NUMBER_OF_FIELDS]; // array holding values for all the fields
int motorState;
int motorVal;
void setup()
{
//set the PWM frequency to be 122Hz
TCCR1B = TCCR1B & 0b11111000 | 0x04;
//setup the pins to be used with the PWM
pinMode(leftjagsigpin, OUTPUT);
pinMode(leftjagpwrpin, OUTPUT);
pinMode(leftjaggndpin, OUTPUT);
pinMode(rightjagsigpin, OUTPUT);
pinMode(rightjagpwrpin, OUTPUT);
pinMode(rightjaggndpin, OUTPUT);
//setup the LED status pin
//pinMode(ledpin, OUTPUT);
//define the pwm reference voltages
digitalWrite(leftjagpwrpin,HIGH);
digitalWrite(leftjaggndpin,LOW);
digitalWrite(rightjagpwrpin,HIGH);
digitalWrite(rightjaggndpin,LOW);
//start the serial interface... 9600 is arbitrary
Serial.begin(9600);
Serial.println("We have connection");
//set the pwm to midscale so the jaguar starts in a friendly state
analogWrite(leftjagsigpin, zeroValue);
//delay(1);
analogWrite(rightjagsigpin, zeroValue);
}
// the loop routine runs over and over again forever:
void loop()
{
if( Serial.available())
{
char ch = Serial.read();
if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 and 9?
{
// yes, accumulate the value
values[fieldIndex] = (values[fieldIndex] * 10) + (ch - '0');
}
else if (ch == ',') // comma is our separator, so move on to the next field
{
if(fieldIndex < NUMBER_OF_FIELDS-1)
fieldIndex++; // increment field index
}
else
{
// any character not a digit or comma ends the acquisition of fields
// in this example it's the newline character sent by the Serial Monitor
//Serial.print( fieldIndex +1);
//Serial.println(" fields received:");
for(int i=0; i <= fieldIndex; i++)
{
int motorState = values[i];
if (motorState == values[0])
{
int motorVal = map(motorState, 99, 10, 35, 59);
//Serial.print("Motor 1...");
//Serial.println(motorVal);
analogWrite(leftjagsigpin, motorVal);
}
else if (motorState == values[1])
{
int motorVal = map(motorState, 99, 10, 35, 59);
//Serial.print("Motor 2...");
//Serial.println(motorVal);
analogWrite(rightjagsigpin, motorVal);
}
values[i] = 0; // set the values to zero, ready for the next message
}
Serial.flush();
fieldIndex = 0; // ready to start over
}
}
}