Close

Serial Communication on a Hacky Afternoon

A project log for Roboartist

"Can a robot turn a... canvas into a beautiful masterpiece?" - Will Smith (I, Robot)

niazangelsniazangels 05/06/2014 at 10:040 Comments

This might be one of those things we probably did on a lazy afternoon. Or evening. I don't remember. Coming up with ideas when drowsy... Half asleep, half awake. When we got to our senses, we realised had a bunch of code that did the job well, but didn't exactly measure up to the International Coding Standards to Not Drive Developers Wild. But it worked. And we let it reside. Today, we introduce you to that part of the code that makes the actual drawings. If you haven't read up on how we managed to position our motors at the right places on the drawing sheet, you should probably read that first.

Anyway, what's the easiest and laziest way to draw on paper then? Tell us if you come up with something lazier in the comments but, we sent the angle values of each AX-12A servo for drawing each pixel to the arduino at rapid rates. Seriously. That's it. This resulted in the stylus moving in the transformed direction of the pixel currently being traced. Here's how we sent the signals to the Arduino.

For controlling the first 3 servos we need 10 bits- ( 0-1023 since Dynamixel AX-12A motors provide 300 degrees of rotation over 1023 steps ) and the 4th servo only needs 1 byte to represent up/down. Hence a total of 31 bits ( nearly 4 bytes) must be sent for representing each pixel. But since arduino supports only 8 bit serial data we break down and rearrange the bits as follows:

The first 3 bytes are formed from the lower 8 bits of the servo angle values. The 4th byte is formed from the upper 2 bits of the 3 servo angles, a delay control bit and the bit representing servo 4’s angle as shown above. These 4 bytes together represent a single point of the image to be drawn on the paper. These bytes are then sent to the Arduino in clusters of 32 bytes

Arduino microcontrollers supports standard baud rates: 4800, 9600, 19200, 38400, 57600, 115200. The Arduino Mega has a 64 byte serial register for incoming bytes. MATLAB initially sends 64 bytes worth of data to the Mega. In the consecutive cycles, after the Mega reads 32 bytes of data, it sends a signaling byte to MATLAB requesting the next 32 bytes. During this time the Mega can read the remaining 32 bytes and hence there is no delay by waiting. We just needed to rearrange the bits on the other side and fire it away to the motors. The signaling byte we have chosen ( for no apparent reason ) is 50 ( 0b00110010 ).

Yup. That was hacky enough for one day. We probably spent the rest of that afternoon ringing doorbells of the neighbours and hiding in the bushes.

Discussions