Xbox 360 Wireless Controller for RC Car

Description

The purpose of this project was to create the base needed to control an electric RC car through the use of an Xbox 360 Wireless Controller and mbed. The principles in this project should apply to any electric RC car as long as an ESC is present, however, parts used and tested listed in the Hardware section.

XB360RC

Hardware

In this case, the parts that comprise a Traxxas Rustler XL-1 and XL-5 were used.
XL-5 model here: Traxxas Rustler XL-5

Most important RC parts for basic functionality:

Parts required for mbed functionality

  • mbed LPC1768
  • USB A Socket Breakout
  • LM7085 +5V Voltage Regulator & Two 1μF-10μF Capacitors
  • Stable 7-18V Power Supply

Mbed and the servo will be powered from the regulated 6V output from the XL-5 ESC.

The Xbox 360 Wireless Receiver will need to be powered off of the 7-18V power supply through the use of the LM7085 voltage regulator. It was found that keeping this device on its own power supply was the most stable option as it seems to draw a lot of power. In initial experimentation, an Energizer 9V battery was drained over the course of approximately 5-6 hours. A more stable power supply (preferably rechargeable) is being searched for.

In this state, the RC car has two power switches. One on the ESC itself, and another to send power to the USB port.

Software

The software behind the Xbox 360 Wireless Controller and its communication with mbed for the purposes of this project was derived through the use of Suga koubou's Xbox 360 Wireless Controller library as a base. Most edits occurred within the onXpadEvent method to allow the RC car to be controller by the Xbox 360 Controller's left and right triggers, left thumbstick, and the Xbox home button. Contols listed below:

ControlAction
Left TriggerBrake/Reverse
Right TriggerForward
Left ThumbstickSteering
Xbox Home ButtonKill Switch

Also, since there is an ESC between mbed and the motor, it was discovered that the ESC operates exactly like a servo. So, instead of trying to drive the motor with a standard PwmOut or Motor output, it is better to use the Servo class.

main.cpp

#include "mbed.h"
#include "USBHostXpad.h"
#include "Servo.h"
volatile int poll = 0;

Servo engine(p24);
Servo servo(p23);

float engineF = 0;
float engineR = 0;
float servoLR = 0;

void onXpadEvent (int buttons, int stick_lx, int stick_ly, int stick_rx, int stick_ry, int trigger_l, int trigger_r) {
    engineF = float(trigger_r)/255*15;
    engineR = -float(trigger_l)/255*10;
    servoLR = -float(stick_lx)/32767*45;
    if ((buttons == 1024) || (engineF > 1 && engineR < -1))
        engine.position(0);
    else if (engineR < 0) 
        engine.position(engineR); 
    else
        engine.position(engineF); 
    if (servoLR > 7 || servoLR < -7)
        servo.position(-float(stick_lx)/32767*45);
    else
        servo.position(0);
    poll = 0;
}

void xpad_task(void const *) {
    USBHostXpad xpad;

    while(1) {
        // try to connect a Xbox 360 Wireless Controller
        while(!xpad.connect())
            Thread::wait(500);
    
        // when connected, attach handler called on xpad event
        xpad.attachEvent(onXpadEvent);

        xpad.led(USBHostXpad::LED_ROTATE);
        Thread::wait(500);
        xpad.rumble(0xff, 0);
        Thread::wait(500);
        xpad.rumble(0, 0xff);
        Thread::wait(500);
        xpad.rumble(0, 0);
        Thread::wait(500);
        xpad.led(USBHostXpad::LED1_ON);

        // wait until the mouse is disconnected
        while(xpad.connected()) {
            Thread::wait(500);
            poll ++;
            if (poll > 2) {
                engine.position(0);
                xpad.restart();
                poll = 0;
            }
        }
    }
}


int main() {
    engine.position(0);
    servo.position(0);
    Thread xpadTask(xpad_task, NULL, osPriorityNormal, 1024 * 4);
    while(1) {
        Thread::wait(200);
    }
}


1 comment on Xbox 360 Wireless Controller for RC Car:

28 Oct 2015

I'm having trouble getting the controller to even connect to the mbed let alone control it. Did you face this problem at all? Any ideas on what I could try?

Please log in to post comments.