R/C Autonomous Car Part I

Project Info

This is a project I have wanted to take on for some time but have only really been sitting down with recently. The goal is to strip an R/C buggy of it’s radio receiver, and install a new “brain” in its place. If you haven’t guessed yet, that brain is the one and only Arduino. I am going to have the car be able to switch between manual and autonomous mode. Autonomous mode will be controlled via a GPS where the car will be told to head to a certain way point and then return to its start location. I figured for manual control I could simply use the standard controller that came with the car, but that is limited in its functionality, so why not use something else? Enter the dual shock 4 controller, a beautiful piece of hardware for any DIY’er that wants to add some remote control to their project. This will be used with a USB host shield in conjunction with a blue tooth dongle. Now for anyone that has played with Bluetooth before, you know how limited the range can be, especially when the DS4 was designed to be used on a couch a few feet from the console, not a quarter mile away while controlling an entire host of electronics and sensors. I’m not sure if I will stick with Bluetooth or possibly take advantage of some Series 1 xBee’s, but that is a decision for a later time.

As of right now the car can be controlled via the Arduino, but that is about it. You can tell it to accelerate or brake as well as turn in either direction, however this all needs to be hard coded in before the car is unplugged and released outside, so it cannot make any decisions for itself nor can it receive commands from you. Getting this far has been challenging enough. Now moving down the road, trying to implement the GPS and the DS4 controller is going to be an even larger hurdle in its own right.

This project has required a lot of prototyping and the use of terrible language but is coming along very well. I don’t know how many parts this build log will take but I expect there to be 3-4 separate sections before we can see a final product.


Parts

So there a few parts needed to get what I have working up and running in your own mad scientist lab.

First and foremost, you are going to have to have an R/C car, I am using my old RC10B4 Buggy, I am only using the old chassis and motor from this car and have bought a new ESC and battery to use in place of the older stock ESC and long dead battery.

Secondly, you need an Arduino of sorts. I am using an Arduino Micro as it can fit easily into my chassis for testing purposes. This will NOT be the final Arduino variant that I use as it is still too large to accommodate everything that will need to fit by the end of this project. It should also be noted that any kind of Arduino can be used here, for example, the Uno R3 which is a fairly popular starting board.

You should get a buzzer for debugging reasons, my code is all debugged so unless you plan on editing the code at all and want to add your own debug chimes then this is not really needed. This is definitely not necessary, but it is very convenient.

Lastly, your going to have to figure out a way to connect the servo 3-pin headers that will lead into your Arduino. I have a bunch of 0.1″ pin-outs on hand that I soldered to a perf-board along with my buzzer and Arduino Micro. This is basically just a testing board to get my speed controller and steering servo working, the final board will be a bit more involved than just these few parts.

Here is my board setup for reference:

IMG_1196           IMG_1197

There is really nothing fancy listed here, any basic Arduino starter kit will have more than enough parts to get you started on this project as really the only necessary item is the Arduino itself, I just like to take things one step further.


       Goals

We only have a few goals today as this part is mostly learning and understanding. Do not underestimate how valuable it is to learn everything there is to know about our car. It is going to help us tremendously later on when we get to working with the Arduino and interfacing it with our car.

By the end of this post you should know the following:

– What components make up the R/C car and what they do

– What signals those components need to work

– How to find our specific signal ranges

– How to mimic those signals with our Arduino

I am going to walk you through the process I used to accomplish the goals listed above. While not all this information is necessary, I’ve found it helpful for troubleshooting when I come across an error to have a good understanding of the system as a whole.


Car Components

There are only a few parts to every R/C car. However we first need to know what type of R/C car you have. I consider there to be two different categories.

– Enthusiast hobby cars

– R/C toys

We are going to be working with an Enthusiast grade car as they tend to be slightly roomier and project friendly while also having plenty of information surrounding their specifications and requirements. This applies to both the car as a whole and the individual components inside them.

Should you not have the below components in your car, chances are you are looking at an R/C toy. These usually contain a circuit board powered by a few AA batteries. While these are much easier to work with, this project will not be applicable due to hardware differences.

So what may we find in one of these enthusiast cars?

First you are going to have your chassis. This will usually be made of a hard plastic and be broken out into sections through the use of thin walls. Here is what my empty chassis and semi-filled looks like for reference, I have labeled the areas for what they are intended to hold.

Picture 8Picture 8

NOTE: the above is only a representation of what the chassis will contain for this part of the project as there will be more components as the build continues

In the center of the chassis, you will likely have your battery. this is placed in the middle of the car for balancing reasons and should not be moved from its factory placement. If as the project goes on, you find your battery is dying much quicker than it should, it may be helpful to upgrade to a higher mA/h battery that will last longer. I am using a 3000 mA/h and have so far had few issues with it.

On one side of the battery or the other there will be the ESC, which is the Electronic Speed Controller, as you can see mine is located above the battery. The  ESC will have four power wires, two for ground, and two for positive. One set will have a plug on the end, this is for your battery. The other remaining positive and ground wire will not be connected to each other and should go to your motor (if they are not plugged in already). In addition to those four, there will also be a three wide ribbon cable. This will carry +5v and ground on the red and black wires respectively. The white wire is your signal wire and is used to send an appropriate signal triggering the ESC, more on that later however.

On the opposite side you will see your receiver. In the pictures above, my receiver has already been removed as it is not needed anymore. Before that however, the receiver lived in the area now occupied by the Arduino. You can remove yours as well, but do not throw it away just yet as we will need it later on! Before removal of your receiver, take note of which way the wires plug in. It will either be White-Red-Black or Black-Red-White.

On the backside of the car is where our motor and gearing lives. I had mine torn down to perform some repairs at the time of the first photo. In addition to this, we are also missing the rear wheels, I would suggest removing these when testing your code with the ESC and motor attached. This allows the motor to spin freely without fear of a runaway vehicle.

Lastly we have our steering servo that lives in the nose of our car. These can be removed and replaced if they are broken but really should not be messed with to much. Keep track of its three wide ribbon cable, just like our ESC, we will need it to send our steering signals to the servo.


ESC Basics

Setting our ESC up is relatively easy assuming you understand the basics of servos. There are a lot of really awesome tutorials on what servos are and how they are controlled like this video here. However it does help to take that knowledge to the next level and understand the basics of the signal pattern that servos use and why you cannot send just any old PWM signal and expect it to work like I did at first!

Servos use what is called a PPM (Pulse Position Modulation) signal to control their position. This remains unknown to many because the Arduino servo library generates PPM signals for us. We write a value between 0 and 180 degrees to the servo through a function from the servo library. That function then writes a PPM signal to whichever pin our servo is attached to according to the angle we specified in the function.

So why do we care about these PPM signals?

The ESC that controls how fast our motor is running uses the very same PPM signals that our steering servo, or any servo for that matter, uses. This makes for not only very easy, but also very precise control of the car since we can use the servo library. While the coding is simple, we do have to worry about what we are sending with that code. There are many different ESCs out there and chances are you will not have the same one as I do. It is important to note that each of these different ESCs will have different PPM signal expectations as well as an arming process in order to activate the ESC on start up. We can find the process of your ESC by searching for its specification page online; this can be accomplished by just googling the name of your ESC. For myself, I found that my ESC needs an unbroken neutral throttle signal before it will allow me to run the car. Others may require you to pull the throttle to full forward and then release to neutral, this is just one example as there could be other procedures.

We can worry about applying the arming process after we understand what a PPM signal is however. We should be used to seeing a PWM (Pulse Width Modulation) signal as it is one of the first things we learn with an Arduino. This is where we have a square wave being pulled to high for a certain percentage of the period and then low for the remaining percentage, the time that that this signal is pulled high is known as the duty cycle. In the picture below we see that the period of the wave does not change, however, as the duty cycle increases from 20% to 60% of the period, the percentage of time the wave stays low decreases accordingly.

PWM Signal

I will be describing an ever so slightly different variation of what an actual PPM Signal is, as the way we are applying it is very controlled. If you are curious to how PPM Signals work outside the scope of our application then I’d suggest looking up another explanation, but for now here is what we care about. PPM Signals work similarly in that we are still using a square wave to control our device, but instead of looking at the percentage of time our signal is high out of the entire period, we only care how long the signal is pulled high. in other words, the timing of the duty cycle is what is important. We measure this time with micro seconds where 1000 microseconds(uS) = 1 millisecond(mS). The low time between the duty cycles of a PPM signal is not quite as relevant in terms of how the servo and our ESC will read the signal. We view this low time as the time before the ESC and servo will be updated, for now we will be using the standard 20 millisecond low time as this is the widely accepted time between duty cycles in the world of R/C receivers and transmitters. Looking at the picture below we can see three different PPM signals that a basic servo might receive.

PPM Signals

Remember what I said earlier, we want to read and write these signals in terms of Microseconds, the picture above shows the duty cycles in terms of Milliseconds. Generally the length of the duty cycles that we will be sending are going to be between 1000 uS and 2000 uS (or 1 mS and 2 mS). However this range can very to as far as 500 uS – 2500 uS or even farther depending on your ESC requirements. For example, my ESC considers Full Throttle to be at 1150 uS, while Neutral is at 1700 uS and Full Reverse at 2000 uS. As I decrease the timing of the wave from neutral at 1700 uS, the speed of the motor will increase until it reaches full throttle at 500 uS. The same is said about reverse but we are then increasing the timing of our wave until we reach full reverse at 2000 uS.


Finding The Necessary Signals

So now we understand that we need some kind of PPM signal to run our motor, but what your ESC considers full throttle might be what my ESC considers neutral, so we need to find out what your ESC needs! There are two equally easy ways to accomplish this this task. The first method involves using the Arduino and one of its digital pins as an input for our signal from the RC receiver. On the other hand we can also use an oscilloscope as I did, I will cover this process in another post that can be found here (post pending).

For now, we will focus on the former method. Follow this link to download the code you will need. It may be helpful to save this to your own computer for future R/C project use. With that downloaded we can look towards plugging everything into the appropriate pins. Your Receiver should look similar to mine here:

Reciever desc

If you have already removed your receiver as I mentioned you should earlier, we will need to reference the note you wrote down describing the order of your pins. If not, feel free to remove it now while taking note of which order your wires go in. When looking at my receiver, the left most column of pins is the signal output (white wire). The middle column is for +5v (red wire) and lastly, the right column is for ground (black wire). It is possible your order is reverse in which case, make sure your pins are connected respectively to what they need. If you are unsure of which pin is which, you can more than likely find the pin outs of your receiver online by googling its exact name.

In addition to three columns we also have three rows, battery, channel 1 and channel 2. Some receivers will not have a separate row for the battery as it is not really needed. As long as one 5v pin and one ground pin is connected to your Aduino’s power supply, everything will operate normally. Your receiver will have the two other rows for your two separate channels, channel 1 goes towards the steering servo while channel 2 goes to our ESC. We are interested only in channel 2 for the moment.

Plug +5v and ground from your Arduino into their respective pins on your receiver. With that done, connect your channel 2 signal pin into pin seven of your Arduino. With all the necessary connections made, upload the code and open your serial monitor. If nothing is showing up after a few seconds, check to make sure your baud rate is set to 9600 at the bottom right of the screen. If everything has worked as planned you should see a bunch of numbers start streaming onto the monitor. These represent the timing of your PPM wave in microseconds. Try wiggling your throttle stick to check and see if those numbers move. If they don’t, then there is a chance your ESC is routed through channel 1 instead of 2. If that’s the case, simply switch the wire going to the Ardunio’s pin 7 to the appropriate channel.

Here is what my serial monitor looks like:

Neutral PPMFull ThrottleFull Reverse

First is a screen shot of my receiver at neutral, followed by full forward throttle and finally full reverse. Note how these numbers correspond to the numbers I mentioned earlier. Write down the three different numbers you see and which throttle position they are as these are what our car will use for references later on.

Once you have those numbers written down, you should do the same with the steering channel. Just switch our digital pin seven wire to the other channel. you will see a new set of numbers beginning to stream, this will be for your neutral steering. Turn the wheels to the left, right and then returning to straight on making sure to also write these three numbers down. While we do not need to do this, it will make steering the vehicle that much easier for us later on.


Code

So now we have all those fancy numbers that took so long to get. On top of that we know what they mean and what changing them will do to our car. Luckily, it is easier to write these values to our ESC than it was to find them in the first place. I am going to continue with the already made assumption that you have basic familiarity with servos and their accompanying servo library. After we include said library, we can call a function to write whatever length PPM wave we want. This function is as follows:

ESC.writeMicroseconds(1700);

We create a servo object called ESC that we then perform the operation writeMicroseconds() on. Inside our ( ), where I have 1700, we put the length in uS that we want to send to our ESC. For example, if you wanted to tell your car to accelerate to max throttle, you would replace 1700 with whatever number represents full throttle for your car. This function can also be used for steering, simply call the function on our steering servo object instead of our ESC servo object. Using the range of numbers we wrote down earlier from our steering channel on the receiver, we can output any length PPM signal between full right and full left to gain very precise control of the cars direction.

I wrote a small function to assist in throttle control so that the car can accelerate and decelerate a bit more softly than slamming to full throttle or any other speed. Here is that function from my program currently in use:

void Throttle(int uS)
{
    if (uS > crnt)
    {
        for (int i = crnt; i <= uS; i+=10) {
            ESC.writeMicroseconds(i);
            Serial.println(i);
            delay(20);
        } 
        crnt = uS;
        Serial.println(crnt);
    }
 
    else if (uS < crnt)     {         for (int i = crnt; i >= uS; i-=10) {
            ESC.writeMicroseconds(i);
            Serial.println(i);
            delay(20);
        }
        crnt = uS;
        Serial.println(crnt);
    }
}

As you can see, the function looks at the current throttle (represented by the variable ‘crnt’) and compares it with the throttle speed we want to set our ESC at (represented by the variable ‘uS’). It will then increase or decrease the timing of our waves duty cycle by a factor of 10 uS at a time with a delay of 20 mS before updating with the next signal. When the desired throttle speed has been reached, crnt is set to the new throttle speed and the function exits. All of this is written to our ESC through the function mentioned earlier, ESC.writeMicroseconds(); .

When we begin the next part of this project to incorporate the Dual Shock 4 controller, this will not be needed as we will be able to control the throttle manually and do not have to worry about aggressive acceleration or deceleration.

Now would be a good time to find out what the arming process of your ESC is. Google the exact name of your ESC to find its product or specification page. Look around until you find setup or arming procedure, it should be fairly obvious as it is important information. If not, try googling the ESC name followed by “arming process”. With that found, take note of the throttle commands that are required. Look in the downloaded code for where my arming process is, I have it located on line 54 directly before the end of the void setup () function. You should replace my arming process with whatever your ESC needs. If, for example, your arming process requires full throttle followed by full reverse and then neutral, your code should look similar to:

ESC.writeMicroseconds(FULL);
delay(500);
ESC.writeMicroseconds(REV);
delay(500);
ESC.writeMicroseconds(TNEUT);

FULL, REV and TNEUT are all defined at the code according to what your ESC signal requirements are. You found these earlier and should replace my values with your own both for your throttle and steering range.

Outside of the the above mentioned function, nothing else in today’s code is to outlandish. You can find the full written code here, this should be downloaded and saved, everything is commented out and explained so that you know what each line does. If anything is still unclear, please get in contact with me and I will be more than happy to explain my thought process!


Final Assembly

It’s been a long journey and you have been absolutely slammed with information. However, we have finally arrived at the good stuff, putting everything together and seeing if it works! To start the final step off right, we need to look at how everything should be hooked up to our Arduino. I have made up some Eagle files to show off my board schematic, I admit they are not amazing as I am still trying to understand the software. You can find a link to the actual file here, but for those who only need a picture:

Schematic Part 1

This schematic was made using a 3.3v Arduino Pro Micro, as a result, if you are using a standard Arduino you will not need the 3.3v regulator and you can feed +5v into the Vcc pin without hesitation. Our ESC supplies power through the ribbon cable that the signal pin is located on, we can use this to power our Arduino. Just make sure all power lines are connected, of which there should be three. One coming from the ESC where the power will flow from, a second that goes to the steering servo and lastly one that leads into Vcc to power the Arduino. With all those tied together, we should do the same with any ground pins. Remember, anything left un-grounded will never work so don’t forget to ground the buzzer as well if you have one.

Our signal pins will be coming from pin five for throttle, pin three for steering and pin nine for the buzzer. It is fine to use different pins than I have listed here, just be careful that you have the right pins connected to the appropriate outputs in order to avoid anomalies.

We have our board set up, the code to loaded onto it and a car to plug everything in to. The only thing left to do is adjust the code to our hearts content. The first thing you should edit is all of the definitions I have towards the top of the code. There are six different definitions tied to the limits of our cars PPM signals. Just change mine to whichever numbers your car wants and you will be ready to go. As of right now the car will spin the motor up to full speed before stopping and going full reverse. That cycle will endlessly repeat until we shut the power off. We can edit the code and make the car accelerate for a certain amount of time and then turn left or right, but I will leave that for you to tinker with in your own time!


Conclusion

You’re probably thinking we have not actually gotten much done in this post since the car cannot actually do anything outside of what has been hard coded in. While that may be a fair assumption, it is imperative to take note of how much information we just learned. The beginning process is boring and not much progress can clearly be seen, but we now have the groundwork that is necessary to push this project forward with the confidence of knowledge at out backs. Go ahead and mess around with the code and see what things you can find out, there is always a fresh and un-edited copy in the link above should you ever need a fresh slate. Next time we will be incorporating the Dual Shock 4 controller into the project so that we can regain manual control without the use of our receiver. Feel free to shoot me an email with any comments or questions!

Posted in Autonomous R/C Car

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Current Project
Autonomous R/C Car
Projects

Join 4 other followers

%d bloggers like this: