Thursday, May 22, 2014

Robotic Remote Controller Protocol Design

http://blog.oscarliang.net/robotic-remote-controller-protocol-design/

Robotic Remote Controller Protocol Design

Remote Controller Protocol

The Communication Protocol

Remote Controller Protocol design is the core part of the DIY Remote Controller Project, which can also be the most difficult part if you are aiming for a sophisticated design. I have had similar design experience in my past project designingcommunication protocol.
The protocol I am talking about is the format of a series of values we send from the remote controller to the client, each value is one byte packet sent using Serial.write(). I also call this series of values “command” and it looks something like this:
   1      122       93       28       19        1       0        1
| type | value1 | value2 | value3 | value4 | value5 | value6 | value7 |
“Type” is the type of protocol. I am going to design various type of protocols, each type uses different control values and precisions. The “Type” number tells the client how many and what kind of value we are expecting, and how to use these values.
This is a demo video using the protocol, controlling a quadruped robot.

A Trick To Encode Button/Toggle States

Since the buttons and toggles states only have values of 0 or 1 (boolean value), so it would not be very wise to transfer each of the input using a full byte. What I have done is to treat each button/toggle as 1 bit, and I have 8 of them so it’s just enough to make 1 byte (8 bits). That way I can save 7 bytes each time I send a command! I call this process of converting Button States “Button States Encoding”. When it reaches the client side, we have to do the reverse process to convert 1 byte value into 8 boolean values, and I call it “Button States Decoding”.

Button States Encoding

This is an example on how it works. Imagine we align the inputs like this
 | toggle1 | toggle2 | toggle3 | toggle4 | button1 | button2 | button3 | button4 |
(1) If Button3 is pressed and not any others, we have 0000 0010 in binary, which is 2 in decimal, and that’s the number we are going to send using this Serial.write(2);
(2) Another example if Toggle2 is on, and button2 is pressed and not others, we have 01000100, which is 2^6 + 2^2 = 64 + 4 = 68.
Notice the maths (+ additions and ^ powers) we have to do when doing the button encodings, this is quite computationally expensive. To improve this, we can manipulate what we call “Bit Shift Operator“. It can help reduce calculation time.
For example (1), we can now do button3 << 1, which is 2
For example (2), toggle2 << 6 + button2 << 2 = 68
1// convert 4 toggle and 4 buttons state into binary, and then convert binary to byte number for transmission
2// expect bit1 - bit8 are zeros or ones
3byte EncodeButton(bool bit1, bool bit2, bool bit3, bool bit4, bool bit5, boolbit6, bool bit7, bool bit8){
4 
5    byte sum = 0;
6    sum += bit1 << 7;
7    sum += bit2 << 6;
8    sum += bit3 << 5;
9    sum += bit4 << 4;
10    sum += bit5 << 3;
11    sum += bit6 << 2;
12    sum += bit7 << 1;
13    sum += bit8 << 0;
14 
15    return sum;
16 
17}

Button States Decoding

To do the reverse at the client side, we need to test each bit of the received byte value to see if they are 0 or 1 and assign it to a variable that represents each button. Or you can also have a switch case statement to look it up, it’s up to you. From above examples:
(1) If we received 2, we need to check each bit start from most significant bit. Assuming Toggle1 is on, we should have received a value 10000000 = 2^7 = 128. But 2/128 < 1 thus Toggle1 = 0. Just like this, we work all the way down to Button3.
Assume Button3 is pressed, we should have 00000010 = 2, 2/2 >= 1, thus button3 = 1 is true!
Assume Button4 is pressed, we should have 00000001 = 1, (2-2)/2 < 1, thus button4 = 0!
So, all variables are 0′s, except button3.
Again, like Button Encoding, we can explode the “Bit Shift Operator” trick. From the above example, we need to test each bit starting from the most significant bit.
We received 2, 2 >> 7 = 0, thus toggle1 = 0; And work your way down.
2 >> 1 >= 1, so button3 = 1, (2-2) >> 0 < 1, thus button4 = 0;
1void DecodeButton(byte byte1, bool *bit1, bool *bit2, bool *bit3, bool *bit4,bool *bit5, bool *bit6, bool *bit7, bool *bit8){
2// Usage: DecodeButton(buttonByte, &toggle1, &toggle2, &toggle3, &toggle4, &button3, &button4, &button5, &button6);
3    byte sum = 0;
4 
5    *bit1 = byte1 >> 7;
6 
7    sum += (*bit1)*128;
8    *bit2 = (byte1-sum) >> 6;
9 
10    sum += (*bit2)*64;
11    *bit3 = byte1-sum >> 5;
12 
13    sum += (*bit3)*32;
14    *bit4 = byte1-sum >> 4;
15 
16    sum += (*bit4)*16;
17    *bit5 = byte1-sum >> 3;
18 
19    sum += (*bit5)*8;
20    *bit6 = byte1-sum >> 2;
21 
22    sum += (*bit6)*4;
23    *bit7 = byte1-sum >> 1;
24 
25    sum += (*bit7)*2;
26    *bit8 = byte1-sum >> 0;
27 
28}

Types of Remote Controller Protocols

As I have discussed in the last post, we are going to have multiple protocols that transmit data in different format and level of accuracy, so users are backed by these choices of different protocols depend on the situation. Sometimes you might want the smallest latency protocol (less accurate but faster transmission), and sometimes you might prefer high resolution commands (high accuracy but slower transmission). At the moment I have implemented these protocols, these examples give you an idea what the commands look loke.

1. All Controls, Short Version

All the values are made up as an example. Each value is a byte which has a max value of 255. Same applies to all four examples.
   0     122     93    28     19     60     55    199   250      50
| type | pot1 | pot2| pot3 | pot4 | js1x | js1y| js2x | js2y | buttons |

2. All Controls, Long Version (full accuracy)

    0         2           93           0           19           3
| type | pot1(high) | pot1(low) | pot2(high) | pot2(low) | pot3(high)
     0           155          1           155           3          0
| pot3(low) | pot4(high) | pot4(low) | js1x(high) | js1x(low) | js1y(high)
      3          51            1           12           25         21
| js1y(low) | js2x(high) | js2x(low) | js2y(high) | js2y(low) | buttons |

3. Selected Controls, Short Version

For this example, I selected a potentiomete, a joystick and all buttons and toggles.
   0     129     93     0        19
| type | pot1 | js1x | js1y | buttons |

4. Selected Controls, Long Version (full accuracy)

For this example, I selected a potentiomete, a joystick and all buttons and toggles.
   
   0      2          93         0        19       3       20         0 
|type| pot1(high)| pot1(low)| js1x(h)| js1x(l)| js1y(h)| js1y(l)| buttons

Channel

The term “channel” is used quite a lot when it comes to commercial RC transmitters. Channel can mean 2 totally different things:
1. the number of “things” you can control, for example for a joystick you need at least 2 channel, one for left right, one for up down (1 Degree of freedom each channel).
2. the number of different transmission frequency you can use to avoid conflicts with other remote controllers. (pretty much like Radio channels)
Remote Controller Protocol
Fortunately none of these would be relevant to my remote controller. For (1), because the way I send data, I can choose to send input data from each control one by one if I want, so no matter how many thing I need to control, it would be do-able (although the more control means more data to transmit thus takes longer). For (2), I can use something called “controller identifier” in the command I send to differentiate different controllers, so the commands will only be picked up at the client side with the pre-defined identifier. This is only an idea but totally do-able.
If you want to discuss or share your ideas, you can post something in our forum here.

How To Choose RC Transmitter For Quadcopter

http://blog.oscarliang.net/choose-rc-transmitter-quadcopter/

How To Choose RC Transmitter For Quadcopter

Remote Controller Protocol
Before building your quadcopter, the RC Transmitter would probably be the first few things you need to look at. It’s a common question for RC beginners how to choose a decent RC transmitter. In this article I will discuss the basics of a RC transmitter and what you should buy.
Unlike other parts there isn’t much room for you to DIY, so it’s common that we would just buy a commercially available transmitter. There are a few things about functionality you should know before discussing the price.

Channels

You might already often hear the term Channel when talking about RC transmitters. Each channel allows one individual thing on the aircraft that can be controlled. For example, one channel for throttle, one channel for turning right and left, one channel for pitching forward and backward, one for rolling left and right. Four channels is a minimum for a quadcopter (pitch, roll, throttle, yaw).
RC-transmitter-channels
With more channels than just four, you can even have switch, or potentiometers to change settings on the quadcopter while flying. Some fly controllers (e.g. Multiwii, Arducopter) recommend using transmitters that has at least 5 channels, the extra channel is to switch between different flying modes.
5-channel-transmitter-diagram

Modes

There are 2 different Modes – mode one and mode two. It’s basically different control configuration.
The mode one configuration has the elevator control on the left joystick and the throttle on the right one.
The mode two is the most common for quadcopter because the stick represents the movement of your quadcopter. It has the elevator control on the right joystick and the motor throttle on the left one. The right joystick self centres in the both axis, whereas the left joystick only self centres in left/right axis and “clicks” in the up/down axis in order to allow the throttle setting.
transmitter-stick-modes

RC Transmitter and Receiver Paring

A receiver usually comes with the transmitter when you buy it. But be aware that some types of transmitter are only compatible to their own receivers (same brand same model). That means when the receiver is broken you will have to get the same one. There are a few exceptions that they can be paired with other receivers (i think universal is the word?). Make sure you check and ask the shop before buying.

What RC transmitter should I get?

The price range is huge, from as cheap as $20 to over $1000. Of course the cheaper, the lower quality it would be, and the fewer channels you are going to get. It would be a good idea to get a cheap 5 or 6 channel one to get a taste of flying a plane, and later one upgrade to a better transmitter when you know more about the subject. It’s always a good idea to have backup transmitters anyway. However if you are serious about quadcopters and someday want to get one with GPS navigation you will need 8 or more channels.
The transmitter is potentially a long term investment. If you are not sure about whether you will be staying in this hobby, you would be safe to get something like a cheap 6 channel. But if you are sure you will stay in the next couple of years you will not regret to get a 8 channel or more! Moreover It’s not just a matter of number of channels. Some RC transmitters support programming and firmware flashing to enhance functionality as well. So do your research before spending good money on it.

Recommedation on 8 Channel RC Controller

If you ask me that, I current favourite is the Turnigy 9X! See my review about this Transmitter.

DIY RC Transmitter

Although it’s possible to hack a game console and make your own RC transmitter, it seems quite difficult.
I actually also built a RC Transmitter myself although I haven’t tested it yet with a quadcopter.

DIY Wireless RC Remote Controller for Robots, Quadcopter

http://blog.oscarliang.net/diy-wireless-rc-remote-controller-for-robots/

DIY Wireless RC Remote Controller for Robots, Quadcopter

remote_controller_feature
Great robots deserve a great remote controller. A proper, well designed controller can speed up project development and in some cases can even improve robot performance. In this post I will describe how I design, make, test and improve a customized RC remote controller.
I call this project “Omote” – Oscar’s Remote controller. (Just to clarify, it has nothing to do with the Japanese word Omote which in Aikido, it could mean the act of throwing your opponent in front of them, thanks to John Matsson pointed that out, haha)

The Omote Goal!

The goal of this project is to create a remote controller that can be alternative to a RC transmitter or similar commercial controllers. The remote controller we build would be able to control, manipulate our robots, flying planes like quadcopter, even can be used for PC gaming like car racing games. There are quite a lot of existing commercialcontrollers like RC transmitters, but they tend to be very expensive and probably not optimized to the project we are doing.
We are going to focus on
  • affordable
  • customization
  • multi-purpose
  • latency
  • accuracy
  • reliability
  • operation distance
  • informative
This is the final product. It’s not perfect, and I learned a lot from this trial version. So I might think about making an new version when I have time.
DIY Customized Remote Controller_front
I will divide this project into these tasks. And hopefully at the end we will have a reliable working remote controller.
This is the LED and Servo Control Demo video:

Remote Controller Hardware And Electronics

I tried to use parts that are as simple and cheap as possible. But what components to pick largely depend on what kind of project or robot you are trying to control. For example for a simple robotic tank, you might want to be able to make it go forward and backward, turn left and right. So four push buttons would be enough to accomplish this. But if you want to have better user experience, a joystick would be better. If there is a canon on the tank, you probably also want a button for shooting. If there is a head light, you will need a toggle switch. You should see where this is going.
DIY Customized Remote Controller_design
For my remote controller, I am not designing it narrowly for some particular projects, but for more general usage. Therefore I used a combination of different types of control components. This is what it looks like on drawing.
Here are the parts I used:
  • Toggle Switch x 4
  • 2-Axis Joystick x 2
  • Potentio Meter x 4
  • Push Button x 6
  • LED x 3
  • LCD x 1
  • Arduino Mega x 1
  • Cables x many
  • Small Breadboard x 2
  • Ciesco XRF Wireless Modules x 2
For the Wireless communication Modules, I chose to use the Ciesco XRF because they are so much cheaper than the XBee. I wrote a post on how to use them, check it out it’s very straight-forward. Xbee would also work.
I picked the Arduino Mega because I know Arduino very well. The Mega provides more enough Analogue and Digital pins, which the UNO failed.
I was thinking of getting a 3 Axis Joystick, but they are shockingly expensive! The cheapest one I found was 30 Euros which is literally just a 2 axis joystick with a potentiometer. So I went for the cheap option of getting these two parts separately and it costs me only 3 pounds.

Inputs of These Components

Toggle Switch, Push Buttons return true when pressed (1) and false when it’s not (0). Potentiometer has a max resistance of 10K which give a value between 0 to 1023. 2-axis joysticks are basically just two potentiometers, which gives you two values between 0 to 1023 for X and Y axis. It might be a good idea to test and make sure you understand how these components work before doing any further. The picture shows the testing setup I had.
DIY Customized Remote Controller_hardware_testing

Soldering and Assembling

Assembling wasn’t particularly difficult as everything went quite smoothly as planned, though I had to turn some parts around to fit the actual dimension. Here are some soldering work I did on the parts:
DIY Customized Remote Controller
DIY Customized Remote Controller_push_buttons
DIY Customized Remote Controller_joystick
DIY Customized Remote Controller_potentiometer
Male to Female cable made by myself, they are very handy to have.
DIY Customized Remote Controller_cable3
DIY Customized Remote Controller_cable2
The Arduino Mega is attached to the base board. And the white front panel finished using Polystyrene.
DIY Customized Remote Controller_arduino mega
DIY Customized Remote Controller_panel
This is what happened when I put every components on the panel, it’s a mess! But when I turned it over, I felt much better, LOL.
DIY Customized Remote Controller_messy cable
DIY Customized Remote Controller_all most
It took me about 2 to 3 hours to finish the final hardware testing after the assembling. And that’s it! All the parts are working and ready for programming!
DIY Customized Remote Controller_side
DIY Customized Remote Controller_done

Some Assembling Advice

Make sure you have all sorts of jumper cables ready: Female to male, male to male and female to female. And make sure they are long enough, generally you want them to be twice as long as the width of your remote controller, so you can have the panel and base laid down naturally side by side  while you are connecting the components. Otherwise you will have a really painful time doing that. For trouble shooting, it’s even worse when short cables are used.
Carry out testing right after you connect a component. It would be a nightmare to have all the parts connected and realize nothing works!

Software Overview

Software for this project consists two parts, one for the remote controller (which I call “Host” later on), and the other for the robot (“Client”).
As for software for the remote controller, the idea is quite simple (see state flow chart). But I can foresee to achieve what is required, the programming can get very sticky and complicated. It is responsible for initializing connection, re-establishing broken connection, encoding commands and provide feedback from client to the user. There will also be a LCD menu system to provide current state information of the controller, allow real time parameter adjustment, calibration and so on.
DIY Customized Remote Controller_host
On the client side (robot side), I will be writing a library for it which will act as an interface between the robot and the controller. It is responsible for accepting connection, decoding commands and communicating back.
DIY Customized Remote Controller_client

Arduino Function For Fundemental Communication

As for sending data, because we are using the serial pins on the Arduino, I will be using Serial.write() for sending data. This function sends one byte of data which means the max value we can transmit is 255 each time we call this function.
You might be wondering what we should do about the inputs from the potentiometers and joysticks, as they have a max value of 1023. We have two options, one is to downgrade resolution to map the value between 0 and 1023 to a new value between 0 and 255, which can be fit in one byte. Second option is to treat the number in term of bits (1024 can be represented with 10 bits), which can be send separately as two packets. When they arrived at the client side, we put them back together as one number.
As you might know, for a single value, sending two bytes would take longer than one byte. Although it’s less accurate, we sometimes don’t need that level of accuracy and prefer smaller latency. So I am planning to adopt both methods into the remote controller communication, so user can select which way to go depends on the situation.
And that takes us into the next section – the protocol. How do we design and put together a command from these raw input values for transmission? It’s going to be a wordy topic, so I will talk about this in the next post.
If you want to discuss or share your ideas, you can post something in our forum here.

Tuesday, April 8, 2014

Electronic Glow-worm

http://electronicsforu.com/electronicsforu/circuitarchives/view_article.asp?sno=744&article_type=1&id=672&tt=unhot&b_type=new