Search Microcontrollers

Sunday, August 17, 2014

Arduino Robot - Putting it together

The time of putting (almost) all together finally came.
My young student Marco came over to my place with his family for a week, I showed them around a bit and sure enough we spared quite some time to assemble the robot.

We did not complete the task, as it was never the plan, but we reached a point where Marco can take over and finish almost autonomously (with my remote support) as a "deeper" learning experience.

(In the picture Marco installing the stepper motor 
that moves the scanning ultrasound sensor)

At this point the rover can move a specific amount of centimeters in each direction (using feedback from the encoders), we tested the ultrasound sensors and plenty of functionality is already included in the software (3 different turn types are  implemented : on the spot, sharp, shallow, plus motor speed balancing is implemented, coordinate management etc..).
Separately the NRF24L01+ radio system is connected to another arduino pro and an I2C based communication protocol is drafted (and tested) to communicate with the arduino driving the motors+sensors and with other I2C devices.
Also a UART protocol is used to communicate between the PC and the "base" radio station, the PC software is in Java.

Over time I will help Marco discovering the different parts he likes and activate them.

Meanwhile I would like to share with you a few lessons learnt from this exercise :

1) Cables!
Okay, we decided to pack a lot of features in this project, so we had to support them with software... but that was not the hard part.
All the devices must be somehow connected with data and power lines and this quickly generates a mess with the cables.
I wanted this specific project done in a way that things could easily be attached and detached, so I had to use a lot of improvised connectors and "flying"  cables.
As a general rule, maybe for a more "permanent" project, you may want to secure down your cables to the chassis as much as possible.

We used a female connector to provide 5V+ground connections to the devices connected on the bottom of the rover : 3 Ultrasound Sensors, Stepper Motor, Hall effect sensor, 2 Encoders.
Only for these power connections 14 wires are floating around!
We use velcro to keep them from getting in the way of wheels etc.
We took the +5V rail out of the motor driver as it contains a 5V LDO regulator (motors are powered @6V, the logic on the H-Bridge driver runs at 5V, hence the internal regulator).

2) Encoders
Encoders are simple right? what could possibly go wrong with them?
Well... turns out you should consider a few things when using them.
I got ourselves a few optocouplers, I carefully checked they were "slotted" (those that are U shaped, emitter on one side, photo-transistor on the other side) and the spacing of the U was ok with the thickness of my encoder wheels.
At least I got that part right :)
Then I started to test those couplers and with my surprise they did not work as expected.
There are several issues, the main one being that when light hits the phototransistor, it's  base is activated, but the internal resistance does not go down to zero.
Big deal you may think, something you might also expect, no?
Sure, makes sense, but the issue is that that forms a voltage divider with the pull down resistor you have to add and as a result of that the "high"  level is way lower than 5V.
In my case it was around 2V, while the low level was sitting roughly at 1.3-1.7V.

Direct result of this is that you are not likely going to feed this output directly to an Arduino digital input and in fact I have seen in the web people using an analog input instead and configuring via software the analog threshold to properly detect the logic level.
While that can be a solution, it prevents the usage of interrupts (which are available on digital inputs).
For this reason, some small circuits are sold, containing the coupler, a comparator, the needed resistors and even two fancy tiny leds (pwr-on and logic level).
These circuits make things way easier : simply provide +5V, GND and read a 5V clean logic signal back.

(encoder board with comparator, installed on top of the encoder wheel) 

You might avoid them if you are using an MCU that has embedded comparators, else I strongly suggest you buy these little boards as they save you a lot of trouble.

A final issue with photocouplers is that they are sensitive to ambient light.
That turned out to be a no-problem once installed on the rover as in that position they receive a limited amount of external light, however it polluted my tests when I was trying them on the bench.
No big deal, but something you should account for,

Also I found not particularly smart that the boards I received had male connectors soldered on the same side of the coupler, that forced me to cut slots in the rover chassis and hook the device from behind it.
And yes, I checked if it was worth de-soldering them and re-soldering them on the opposite side, unfortunately the copper layer on the printed board is so thin that if you get anywhere near with a solder iron, you better know very well what you are doing.

3) Arduino interrupts
Normal arduino boards (uno, pro etc) allow you to use two hardware interrupts on digital pins 2 and 3.
That seemed good enough for me as we only had two encoders.
I am not sure yet about this and I am planning to run some specific tests, but I had the impression that servicing the two interrupts with two different dedicated ISR and having them possibly triggering at the same time might result in missed interrupts.
At this point I cannot claim that arduino misses interrupts and since I did not check how the ATMega MCU itself manages interrupts I am not aware if this is actually possible... however I noticed some strange behavior, particularly when debugging via serial port at the same time (it might be due to timing).
This is one of the times when I wish I had a NVIC available like in the Stellaris/Tiva series.

4) Cheap motors
There are available in kits and on ebay cheap DC motors, they run at 6V, do not require a lot of current, contain a reduction gear and they are normally provided with wheel and encoder wheel.
To be honest, they are really cool, but their connections are extremely fragile.
It is ok when they are locked in place and you are not shuffling them around, but if occasionally the connection wires you soldered to them get tight... this may easily rip apart the connections.
That happened to us and we had to dismount one of the motors and solder a new wire almost directly on the brush.
Unless you practice Zen frequently, I strongly suggest you try to avoid that.

(Installing the tail wheel)

Overall it was a real fun experience, as it always is when playing with Arduino.
It is amazing how easy is to explain technology to an enthusiastic and smart 10 year old using Arduino, I really believe it is a perfect platform for this task.
I personally prefer to have a bit more control over the hardware, but that comes with some more complexity, which definitely adds to the fun once you can grasp the basics, until then : Arduino all the way :)

Aside from the main robot activity I was able to help Marco learn how to solder, about serial protocols (I2C, SPI...), how to use a drill press, how to use a multimeter and read an oscilloscope.

There is a new geek in town! :)

Monday, June 23, 2014

Arduino Robot - LiPo batteries update

I said (here) I did not know much about LiPo battery output and their maximum discharge rate.
I made a few assumptions and simply said that my 1200mAh 25C should have been more than fine (for this project) since if it could power an RC car/helicopter, then it should have been more than enough for my  two small DC motors and few other digital electronics.

Still, it was a good opportunity to understand some  more and I researched the meaning of that "25C", here the results :

When you have a battery pack that is declared at xxx mAh you actually know the maximum charge that can be stored in it, but that does not tell you how fast you can (safely, possibly) discharge it or in other words, how many Amperes of current you can draw from it.
1200mAh means that if you draw 1200mA continuously, a completely charged battery will be depleted in 1 hour (assuming you can pull all the charge, which is quite unlikely due to voltage drop that goes with it, plus it is advisable to pull only 80% max of the battery charge to prolong its life).
That does not leave any clue on how you can use that charge, is it going to be at 10mA at a time, at 10A or else?

That's  where the "C" rating comes into place for LiPo batteries.
From what I found the C coefficient has to be multiplied by the total charge of your battery (1200mAh in my case) and that gives the maximum (peak) Amps you can draw.
Now, a bit of math tells us that a 1200mAh @25C can output a whopping 30Amps peak.. at least in theory.
Honestly I don't really think that figure is anywhere near a realistic one, 30A is a scary amount of current even at the moderate voltage we are dealing with (average 7.4V, up to 8.something).
We are talking about 220Watts of energy (7.4 * 30)!
Granted at 30A you would drain the fully charged battery in only 2.4 minutes (1.200/30.000*60)... but I still believe that 30A * 7.4V for two continuous minutes are still a serious business.
I initially thought I had it all wrong, but searching on the web it seems these are the correct figures (!!!).
One of the sources here.

However, when I look at those small wires coming out of it, I see something that seems to have pretty good quality, but still I hardly see them carrying 30Amps without transforming suddenly in a smelly ball of smoke.
I would not feel really safe exceeding 5A, which again is about 10x more than I need.

In conclusion : The max current seems more than adequate for my purpose, all hail to the magic of LiPo batteries... yes but...
Right, is this all so cool, no flipside?
Well, it is until you happen to short circuit your battery, in that case I fear the best thing that can happen is a bit of smoke, fire if it was not your lucky  day and a nice "kaboom" if your horoscope that day was not favorable (in that case you probably deserve it, at least for believing in horoscopes!).
With big power comes big responsibility, play it safe!