View Full Version : [Project] Arduino Question

05-13-2013, 04:28 AM
Hi everyone. I am currently in the middle of a (for me) fairly complicated project with some of my students. We are making an rc car with live video feed and remote control pan tilt for the camera. I am planning to connect a six-channel receiver to the arduino as well as a pan/tilt turret device which I want to control remotely, through the arduino. I have seen many examples of arduino programs (code) for turrets and pan/tilt devices, and also many for receiver devices, but I have not seen any that do both. i.e. I need the arduino, on reception of a transmited signal, to run the pan/tilt program. Can anyone help me out with this? I know some basic arduino. Thanx in advance!

05-13-2013, 12:15 PM
First: A robust Arduino program does not contain any calls to delay(). (Look at "blink without delay" to get started)
That way, the loop() function will read all the inputs (which includes "current time,") figure out what state, if any, should change, and then write any changed output; repeat forever.
To read the input channels, you may be able to use pin change interrupts, and read the time (micros()) within the interrupt handler, but you're likely to get some jitter from the Arduino environment itself.
To communicate between the interrupt handlers and the servo control output, simply use global variables.
The pan/tilt code in loop() would then only need to write the value of those variables to the Servo library, and the pin change interrupt handler would have to do the decoding of the RC channels data.

05-14-2013, 05:06 AM
Thx Jwatte. Working on that...

05-14-2013, 08:55 AM
Some side comments,

As Jwatte mentioned, often with Arduino programs we avoid using delay, but if you ever port the program to something like a Raspberry Pi or BeagleBone... This wil cause the CPU usage to max out in these spinning loops. In these cases some form of yield is a good thing.

As he also mentioned, you may be able to use interrupts to read in the RC information. At one point I did do a version of my Arduino Phoenix code base, that had an RC receiver input class. I don't remember what state it is in. But some issues included:
a) Not all Arduinos support Pin Change interrupts on all pins. So code may depend on which Arduino you are using.
b) Pin change interrupts are not compatible with the stock SoftwareSerial class, as the SoftwareSerial class will define interrupt handlers for all of the Pin Change Interrupts on a processor. One way to work around this is to make your own copy of it and only define and enable the interrupts for the actual pins you would be using for the RX pins.

Servo library and things that muck with interrupts don't mix well. Two different related issues. The Servo library depends on timer interrupts and needs the timing for when an interrupt is triggered to when it is handled to be consistent, otherwise the pulse widths will not be consistent, which will cause the servos to jitter. Depending on much difference there is the jitters may be minor or can be bad.

So for example if you are using pin change interrupts or something like a hardware usart and the processing of one of those interrupts happens at the exact time that the servo code interrupt occurs, the pulse width will be off by the amount of time it takes to enter the interrupt, do whatever it needs to do and exit. Luckily the atmegas are pretty quick and entering and leaving interrupts...

The other issue is with code that disables interrupts. An example of this is the SoftwareSerial library. On transmit it disables interrupts for the entire time it takes to send one character, likewise when it receives an interrupt on the RX pin for the beginning of a start bit, it hangs in the interrupt handler the entire time it takes to receive a character, before it returns which re-enables the interrupts. So for the most part I found this library incompatible with Servos.

Good Luck

05-14-2013, 09:00 PM
I think I bit off more than I can chew for right now with the arduino. I have the old duemilanove and I am pretty sure that will be a real pain. I am learning now I don't have to use arduino to control the servos. I just watched some videos and I see the servos plugged directly into the receiver. Can this work? I am about to try it, but I don't know the battery I need for the servos. They are mg945 13kg servos and I now the voltage should be between 5 and 7 appx. But what about the amps?

05-14-2013, 09:39 PM
Those are strong servos, and need at least one amp each, if not more while moving. A two-cell LiPo is probably going to be too high voltage for that servo, so you either need a 5.0V or 6.0V BEC (probably one per servo!) or a 5-cell NiHM battery pack (nominal 6.0V) to get the right voltage.

Yes, you can control the servos directly from an RC receiver -- that's kind-of what they were initially intended for :-)

05-16-2013, 09:10 AM
Great! I still want to do something with the arduino, but it will have to be later and I will probably just stick with the xbee instead of using r/c...