Tuesday, February 5, 2013

RCArduino Libraries FAQ

A central point for questions and help on using the RCArduino Libraries.

The RCArduino Libraries provides servo output, PPM input and individual RC Channel input.

Its been created to reduce servo jitter in projects that need to both read and output RC Channels. If you want smooth focus for camera work, animatronics that don't twitch around or fast smooth control of your RC Vehicle, read on.

1) Why a new servo and RC library ?

The standard Arduino Servo library is very good, its well documented and well supported, it also has some challenges when used in projects that read incoming RC Signals. The RCArduino Servo library addresses these challenges to provide smoother output. This is achieved through faster interrupt service routines which reduce clashes and glitches.

A major improvement is also acheived by not resetting timer1 as the standard servo library can. This allows us to use timer1 for timing incoming RC Signals as well as generating the servo outputs.



An detailed explanation can be found here -

http://rcarduino.blogspot.com/2012/11/how-to-read-rc-channels-rcarduinofastlib.html

2) Can I use the library to read PPM signals and output RC Channels ?

Yes, an example is provided here, but come back here for more documentation if you need it.

http://rcarduino.blogspot.com/2012/11/how-to-read-rc-receiver-ppm-stream.html

3) Can I use the library to read individual RC Channels and output RC Channels ? 

Yes, an example is provided here, but come back here for more documentation and if your planning to use a mega look out for an update with an example using the six Arduino Mega interrupts instead of the pinchangeint library which is used in the UNO version to provide more than the standard two interrupts.

http://rcarduino.blogspot.com/2012/11/how-to-read-rc-channels-rcarduinofastlib.html

Some background on the pinchangeint library if you need more than 2 interrupts on an UNO or more than 6 on a Mega -

http://rcarduino.blogspot.com/2012/03/need-more-interrupts-to-read-more.html
How do we use the library ?

The RCArduino library uses arrays internally to record the channel input and output values. This is slightly more efficient than the linked lists used by the servo library however its also a little less user friendly. Unlike the Servo library which allows us to attach servos at runtime, the RCArduino library requires that we set the number of servos at compile time. Similarly if you plan to use the PPM Input capability you will need to specify the number of input channels at compile time.

To make this as simple as possible, two #define's are used  in the RCArduinoFastLib.h file.

To set the number of input channels if your going to read a PPM Stream change the number here -
// Change to set the number of channels in PPM Input stream
#define RC_CHANNEL_IN_COUNT 3

To set the number of servos in your project change the number here -

// Change to set the number of servos/ESCs
#define RC_CHANNEL_OUT_COUNT 4

Set the RC_CHANNEL_OUT_COUNT to one more servo than you need, the library is able to generate higher refresh rate signals than the standard Servo library which forces a 50Hz refresh rate. To set the refresh rate we create an additional entry in the servo array. We use the 'setFrameSpace' function on this last entry to set the frame space which sets the refresh rate. While this is initially an awkward additional step, it also gives us a lot more flexibility, for example we can use the library to drive high refresh rates upto 500Hz.

To generate a 50Hz refresh rate which is equivalent to the standard servo library we can use the following calculation -

1/50 = 20,000 microseconds
frame space = 20,000 - (2,000 * number of servos)

An Example -
To drive 8 servos we would set RC_CHANNEL_OUT_COUNT to (8 + 1)= 9. Remember the additional entry is for our framespace.

#define RC_CHANNEL_OUT_COUNT 9

we can then calculate

frame space = 20,000 - (2,000 * (RC_CHANNEL_OUT_COUNT - 1) )

Note we take away 1 so that we do not include the framespace in the framespace calculation

frame space = 20,000 - (2,000 * 8)
frame space = 4,000

So to set the frame space to give a refresh rate of 50 Hz for 8 servos we call

CRCArduinoFastServos::setFrameSpaceA(SERVO_FRAME_SPACE,4000);

Why the maths ?
 
While this is more complex than the standard servo library it is also more flexible and provides a quick and easy way to drive the high frequency ESCs and Servos that are becoming more popular.

If your unsure about the frame space calculation or would like to try the RCArduino libraries with 500Hz ESCs and Servos use the comments or contact me Duane B through the Arduino forum.

I am also considering automating the calculation through a convenience function in a future release, stay tuned.

Now that we have that out of the way, its all very easy.

Attaching A Servo
To attach a servo we call the attach function supply the index of the servo we want to attach (they start at 0, remember they are held in an array) and the Arduino pin we want to attach it to.

CRCArduinoFastServos::attach(SERVO_THROTTLE/* Servo index */,THROTTLE_OUT_PIN /* the pin we want it attached to */);

The final thing we need to do inside out set up function is to call the begin function, this initialises timer1 for use by the library and attaches the interrupt routines.

Understanding The Library and Reading and Writing Channels

The key to understanding and using the library is to be aware of the RC_CHANNEL_IN_COUNT and RC_CHANNEL_OUT_COUNT settings and also to understand that the channel values are stored in arrays which are accessed by the index you supply when calling the read and write functions -

CRCArduinoFastServos::writeMicroseconds(SERVO_STEERING /* 0 based index into servo array */,unSteeringIn);
CRCArduinoPPMChannels::getChannel(SERVO_STEERING /* 0 based index into PPM input array */);

You can create your own indexes into the arrays by defining them in your sketch, the number of input channels does not need to match the number of output channels. You can also have a different index for an input channel that you use for the output channel, in fact you will sometimes find that channels within the PPM stream are in a different order than the printed channel numbers on your receiver.

To get started try the examples provided -

http://rcarduino.blogspot.com/2012/11/how-to-read-rc-channels-rcarduinofastlib.html

http://rcarduino.blogspot.com/2012/11/how-to-read-rc-receiver-ppm-stream.html

For any help, use the comments section below

Duane B

No comments:

Post a Comment