RC Vehicle Help with Coding

Hey All,

I am still a newbie in Arduino and would very appreciate help please. I need to (for a competition I am in) program my vehicle to run: four motors (one in each wheel), a controller (preferably and Xbox or Playstation controller), and four wheel steer with two servos. I would also like to learn how to read inputs from my dc motor (with the encoder) to implement a basic traction control.

As apart of the competition (Land Rover 4x4 in schools), we have to have two basic functions that need to be coded, they are to have a light sensor that reads the lux level to turn a set of lights on if it gets below a certain light level (determined by the competition technical regulations document) and a tilt sensor to read the angle the vehicle is tilted on, and then set a tone off when it gets above approx. 22 degrees from horizontal. The code I just used for the last competition just gone is below (it is very basic):

int led = 13;
int tiltSensor = A0;
int angle = 0;
int lightSensor = A3;
int lightLevel = 0;
int buzzer = 11;

void setup()
{
  pinMode(led, OUTPUT);
  Serial.begin(9600); 
}

void loop()
{
  tiltCode();
  lightSensorCode();
}

void tiltCode()
{
  angle = analogRead(tiltSensor);
  if(angle > 370 || angle < 280)
  {
    tone(buzzer, 1000);
  }
  else
  {
    noTone(buzzer);
  }
}

void lightSensorCode()
{
  lightLevel = analogRead(lightSensor);
  Serial.println(lightLevel);
  if(lightLevel < 15)
  {
    digitalWrite(led, HIGH);
  }
  else
  {
    digitalWrite(led, LOW);
  }
}

In this competition you do not have to code your steering and motors, but rather you can (if you wish) just install a receiver for an RC controller and plug your servos and motors into this. Although I would like to be able to have coded our Arduino to run the motors, servos, sensors and controller of our choice.

Due to my limited knowledge, when I’ve tried to find ways to connect a controller to the Arduino Uno board, I couldn’t decipher what they were talking about with the USB Host shields in these websites:

I would like to be able to understand what they are talking about in these websites in being able to “access” the libraries of the controllers. Specifically I would like to know about the Xbox One and/or the PS 4 controllers as these would be my team’s ideal choice of controller. And also, is there something you have to put into your sketch in order to be able to “access” these libraries?

Could someone please help me out?

I’ve read that some of the triggers and/or dongles are digital (meaning they read the exact values) whereas some are analog (meaning they only read if it true or false, for the controller: if the button is pushed or if it is not). I would like the throttles and steering to be digital so that the person driving can adjust the speed and/or direction of turning. Would it be right to assume that I can have the controller be able to steer the car with the dongles on the controller and use the right trigger as a throttle for speed and left trigger as a reversing throttle?

Thanks for taking the time to help!

PS if you would like to know bit more about the competition see the youtube link below:

This is a video of the World Finals from last year (2016) that my team ended up winning, but I’ve come back this year with another team and am wanting to do it again. I’ve just won the National Finals for Australia and am heading off to the World Finals in Abu Dhabi in December.
This is the website for the competition:
http://www.4x4inschools.co.uk/home/?video_id=46

Feel free to ask questions about the competition and/or what I’ve talked about wanting for my vehicle if you want :slight_smile: .

Rather ambitious Project for a beginner.

"I couldn't decipher what they were talking about with the USB Host shields". A "shield" for an Arduino is a separate board designed to attach "piggyback" onto the Arduino. The USB Host shield can be purchased from a number of places.

Libraries must be installed into your Arduino development Environment (onto the Computer where the rest of the Arduino Software resides) and then are "included" into a given program using #include at the beginning of a sketch. Almost any sample you look at has a number of These commands at the top of the sketch.

Your Explanation of analog and digital are reversed. Digital reads 1 or 0 (pressed or released) while analog reads "how much", for example the Position of a potentiometer (variable resistor).

Hi,

(determined by the competition technical regulations document)

Can to post a copy of the doco or a link to its site please?

Is this you guys?

http://rea.org.au/4x4inschools/

Thanks.. Tom.. :slight_smile:

lucasstep02:
a controller (preferably and Xbox or Playstation controller),

IMHO it would actually be much easier to make another Arduino as a controller and use cheap (and very effective) nRF24L01+ transceivers for the wireless.

Most of the code you will need for 2-way communication is in the second example in this Simple nRF24L01+ Tutorial

...R

Jaba:

Yes I have found out that it will be quite a large project for myself, but I have over a month to complete the sketch so I thought it was worth the challenge.

Thanks, I've been looking at this Host Shield:

http://shop.tkjelectronics.dk/product_info.php?products_id=43

as this store is local and I can go and pick it up when I want to, is this what you'd recommend or is there other shields that better suit this project?

So the #include in sketches is to include those libraries, I was always confused with what they meant.

Thankyou for the clarification of the two terms of analog and digital!

TomGeorge:

Yes that is the program I am in and have been representing. REA is the Australian Nation wide competition for both the two engineering challenges, 4x4 in schools and F1 in schools.

See the attahced documents for the technical regulations for the World Finals of 2017.

Robin2:

What would you then use to drive the vehicle? Would you need to use a computer? This would not be ideal because we have to hand in our controllers to the judges. This is so that no teams can accidentally have their controllers on at the same time and both have the same signal being used. I would like to use a console controller because then we can hand it in as well as use it comfortably (I've had another suggestion of using a phone but this would then be bad because I don't want to lose my phone during comp and it's not as easy to use as what a controller would be).

lucasstep02:
Robin2:

What would you then use to drive the vehicle? Would you need to use a computer? This would not be ideal because we have to hand in our controllers to the judges.

No computer. My interest is in radio-controlled model trains. I have made a controller box with an Atmega 328 on a piece of veroboard (stripboard), a potentiometer for speed control and a switch for forward and reverse. I guess you would want another potentiometer for steering.

This is one of the controllers I made
ControllerDSCF4230sml.png

ControllerOpen.png

If you don’t like the idea of making a circuit with Veroboard you could, of course, use an Uno in a somewhat bigger enclosure.

…R

Sorry TomGeorge, the files didn't attach as they were too big. You have to sign up a team to receive these documents online, but if you have an email that I could send the docs through to, I will shoot an email through when I get the chance. This goes for anybody else as well that wants the documentation of regulations :slight_smile:

Thanks Robin! That looks like it could work great but my team has asked for me to make the controller easy to use and comfortable to use as we will be having to use it for hours of preparation and the actual competition. In this they have asked for a console (such as Xbox or PlayStation controller). Although would the same principles apply for a Bluetooth connection to a console controller? How would it differ?

Also, just out of interest, how easy is it to quickly change direction (from forwards to backwards) with that controller? It looks like it could cause problems for a car if you have to take your hand off the steering potentiometer to change the direction. Would this be the case or can it be done with one hand?

If you want an easy, intuitive, well designed RC car controller form factor, go with the proven form factor.
An RC car controller.
I am on my iPad so it is a pain to put in a pic, but google it.
Trigger for forward and reverse.
Steering wheel.
Plenty of real estate for additional controls if you need it.

Steering with anything other than a steering wheel is difficult.

lucasstep02:
Also, just out of interest, how easy is it to quickly change direction (from forwards to backwards) with that controller? It looks like it could cause problems for a car if you have to take your hand off the steering potentiometer to change the direction. Would this be the case or can it be done with one hand?

You would need to design the controls on your box to suit your application. People don’t need to change the direction of a train in a hurry. :slight_smile:

I know nothing about an XBox or Playstation. I guess my kids (middle-aged men!) do.

…R

Robin2:
Yeah I guess you don't need to change directions quickly with a train, but thanks anyways for the suggestion!

vinceherman:
I cannot find any controllers under the name of 'proven form factor' as it pops up with perf boards and form factors for computers. What do you mean?

Also what makes using a dongle from a controller hard to code? Please don't hear me being rude here, I'm just curious as to what makes coding these dongles so hard?

lucasstep02:
Yeah I guess you don't need to change directions quickly with a train, but thanks anyways for the suggestion!

Can't you adapt my idea to meet your needs? I don't think it would be very difficult.

...R

Robin I'll explain my thinking if you don't mind. So to change your model into what we desire would be to put in an extra potentiometer for steering, as well as including a brake for the vehicle (so we can stop suddenly or stop on a downhill if needed). All these tools (the steering, accelerator and the brake) would need to be in the driver's hands at all times to be able to control the vehicle effectively. So therefore you will need a mold (or shape) that can be held with both hands whilst having your fingers on the tools to be used. So a console controller would suit these needs, whereas if we were to make a module like yours we will need to create a controller-like shape to be held.

Although we still can't use the potentiometers because we need to be able to grip it with more than one finger, meaning that we have to use one hand per potentiometer (so that takes up both our hands) but then we can't use the brakes or change of direction of motors without taking our hand off one of the potentiometers. This could result in us losing the control of our car on the track and crashing it, making us lose points.

We also need to be moving around with the controller in hand to get the best view point for our vehicle crossing that section of the track. This means we need to be able to hold it whilst driving and if we have both hands on the potentiometers, then we cannot pick the controller up.

Sorry if this seems rude, I'm just trying to do what's best for my team. But does this make sense or is there a way to solve some of the issues with this idea?

Libraries must be installed into your Arduino development Environment (onto the Computer where the rest of the Arduino Software resides) and then are "included" into a given program using #include at the beginning of a sketch.

Jaba, how do you install the library and where are they? I know the ones I need for my project as my team have chosen (for now, unless I can convince them of the other idea presented by Robin) the PlayStation 4 controller but I just don't know where to download it from.

lucasstep02:
vinceherman:
I cannot find any controllers under the name of ‘proven form factor’ as it pops up with perf boards and form factors for computers. What do you mean?

I meant to google “rc car controller”

Do you want precise steering? Then you want a steering wheel.

RCCarController.jpg

lucasstep02:
Sorry if this seems rude, I'm just trying to do what's best for my team. But does this make sense or is there a way to solve some of the issues with this idea?

There is certainly nothing rude about having a different point of view.

I guess my focus was on removing the complexity of interfacing with a console device that probably has a USB output. Especially as you have limited time for developing your project.

I wonder if the use of an analog joystick (i.e. 2 potentiometers controlled by 1 lever) would help deal with your issues.

I do understand that the reaction times for train control are different but the units I made are normally used single handed. I suspect if you had a larger "box" it could be held with one hand and operated with the other. And the thumb of the "holding" hand could probably operate some controls.

The R/C flying guys usually have the controller held on a neck strap which leaves both of their hands free for the joysticks.

The subject of designing a control system is interesting in its own right :slight_smile:

Feel free to ignore all of this.

...R

vinceherman:
Previously my team has been using the Spektrum dx4c controller for competitions but I don't know if this could be programmed with Arduino so I wanted to go for an option I knew people can do and help me with if needed. Although I'm open to other suggestions if they will work better than the PlayStation controller.

This here is a pic of the controller we have been using.

Is there a way to be able to read the joysticks precisely. I was thinking of using something along the lines of that the joystick in its standard position (centralised) is straight, wheels are straight, and that as you move the joytick to left or right this is measured in the serial monitor and then you grab these values in another line of coding to control the servos. Is this possible?

Robin:
I think I'll go with the PS 4 controller as this is what my team have decided, I'm sorry but I think this is best for my team.

I would still really like your help with anything you can offer if that's ok.

lucasstep02:
Robin:
I think I'll go with the PS 4 controller as this is what my team have decided, I'm sorry but I think this is best for my team.

That's fine.

...R

To have a throttle capability with the 4 motors, will I need an ESC and/or a motor shield? This is because I have encoders for the motors which I want to utilise with traction control but don't know what to physically get the 'plug' the motors into.

Also for the encoders (I have these motors with the encoders) what will I need to plug the ports into on the Arduino?

ESCs are used for 3-phase induction motors and H-bridge drivers are used for DC motors.

It all depends on what motors you're using.

I am using DC motors (see link in my previous reply for link to web). So I will need h-bridges? Will I also need a motor shield?

How are h-bridges used, and how could they be integrated into my current sketch (below):

#include <PS4BT.h>
#include <usbhub.h>
#include <Servo.h>

#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h)
#endif

USB Usb;
BTD Btd(&Usb);

PS4BT PS4(&Btd, PAIR);

int servoVal;

Servo servoFront;
Servo servoBack;

bool printAngle, printTouch;
uint8_t oldL2Value, oldR2Value;

void setup()
{
  Serial.begin(115200);
  #if !defined(__MIPSEL__)
  while (!Serial);
  #endif
  if (Usb.Init() == -1)
  {
    Serial.print(F("\r\nOSC did not start"));
    while (1);
  }
  Serial.print(F("\r\nPS4 Bluetooth Library Started"));

  servoFront.attach(3);                                        // attach to servoFront pin (has to be a PVM signal) but cannot be from pins 6-13
  servoBack.attach(5);                                         // attach to servoBack pin (has to be a PVM signal) but cannot be from pins 6-13
}

void loop()
{
  Usb.Task();

  if (PS4.connected())
  {
    if (PS4.getAnalogHat(LeftHatX) > 137 || PS4.getAnalogHat(LeftHatX) < 117 || PS4.getAnalogHat(LeftHatY) > 137 || PS4.getAnalogHat(LeftHatY) < 117 || PS4.getAnalogHat(RightHatX) > 137 || PS4.getAnalogHat(RightHatX) < 117 || PS4.getAnalogHat(RightHatY) > 137 ||PS4.getAnalogHat(RightHatY) < 117)
    {
      Serial.print(F("\r\nLeftHatX: "));
      Serial.print(PS4.getAnalogHat(LeftHatX));
      Serial.print(F("\tLeftHatY: "));
      Serial.print(PS4.getAnalogHat(LeftHatY));
      Serial.print(F("\tRightHatX: "));
      Serial.print(PS4.getAnalogHat(RightHatX));
      Serial.print(F("\tRightHatY: "));
      Serial.print(PS4.getAnalogHat(RightHatY));

      servoVal = (PS4.getAnalogHat(RightHatX));
      servoVal = map(servoVal, 0, 255, 0, 180);
      servoFront.write(servoVal);
      servoBack.write(servoVal);
    }

    if (PS4.getAnalogButton(L2) || PS4.getAnalogButton(R2))
    {
      Serial.print(F("\r\nL2 "));
      Serial.print(PS4.getAnalogButton(L2));
      Serial.print("\tR2: ");
      Serial.print(PS4.getAnalogButton(R2));
    }
    if (PS4.getAnalogButton(L2) != oldL2Value || PS4.getAnalogButton(R2) != oldR2Value)
      PS4.setRumbleOn(PS4.getAnalogButton(L2), PS4.getAnalogButton(R2));
    oldL2Value = PS4.getAnalogButton(L2);
    oldR2Value = PS4.getAnalogButton(R2);

    if(PS4.getButtonClick(PS))
    {
      Serial.print(F("\r\nPS"));
      PS4.disconnect();
    }
    else
    {
      if(PS4.getButtonClick(TRIANGLE))
      {
        Serial.print(F("\r\nTriangle"));
        PS4.setRumbleOn(RumbleLow);
      }
      if(PS4.getButtonClick(CIRCLE))
      {
        Serial.print(F("\r\nCircle"));
        PS4.setRumbleOn(RumbleHigh);
      }
      if(PS4.getButtonClick(CROSS))
      {
        Serial.print(F("\r\nCross"));
        PS4.setLedFlash(10, 10);
      }
      if(PS4.getButtonClick(SQUARE))
      {
        Serial.print(F("\r\nSquare"));
        PS4.setLedFlash(0, 0);
      }

      if(PS4.getButtonClick(UP))
      {
        Serial.print(F("\r\nUp"));
        PS4.setLed(Red);
      }
      if(PS4.getButtonClick(RIGHT))
      {
        Serial.print(F("\r\nRight"));
        PS4.setLed(Blue);
      }
      if(PS4.getButtonClick(DOWN));
      {
        Serial.print(F("\r\nDown"));
        PS4.setLed(Yellow);
      }
      if(PS4.getButtonClick(LEFT))
      {
        Serial.print(F("\r\nLeft"));
        PS4.setLed(Green);
      }

      if(PS4.getButtonClick(L1))
      {
        Serial.print(F("\r\nL1"));
      }
      if(PS4.getButtonClick(R1))
      {
        Serial.print(F("\r\nR1"));
      }
      if(PS4.getButtonClick(L3))
      {
        Serial.print(F("\r\nL3"));
      }
      if(PS4.getButtonClick(R3))
      {
        Serial.print(F("\r\nR3"));
      }
    }
  }
}

I will need to use 4 motors (one for each wheel).

FYI, I am currently planning on using the following button set-up for the PS4 Controller:

TRIANGLE = Change vehicle into 4-Wheel Steer
CROSS = 2-Wheel Steer
RIGHT JOYSTICK = X-axis Steering
L2 = Reverse
R2 = Acceleration
L2 + R2 = Brake
PS Button = ON/OFF

Are you planning on using buttons for accelerate? Why? I would think that the joystick would be better. Variable speed. Like the RC controller uses.