Search Microcontrollers

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! 

Monday, June 16, 2014

Arduino Robot - "Zeroing" Stepper motors

Hello, this is yet another post related to the series dedicated to the Arduino Rover project.
These posts, although eventually enjoyable by any kind of maker, are targeted to young and non particularly expert geeks.
Not long ago I was toying around with hall effect sensors, the reason I was is related to today's posts.

The rover will have three ultrasound sensors (yup, 3!!! they are cheap :) ) in the front side.
Two of them will point slightly on the side (one per each side, symmetrically) and the central one normally will point straight ahead.
This one will be mounted on a stepper motor which will allow the rover to scan the area in front of it (and eventually map it, sending back sonar data via the NRF24L01+ radio).
To avoid ultrasonic interference the three modules will be triggered separately (the two side ones will be on the same trigger, sensors have a 15 degree angle sensitivity, they will be pointing in directions that are way more than that angle apart).

Stepper motors, particularly the one I chose (a cheap 28bjy-48, with gearbox reduction), can have a very fine resolution, so they can provide a very precise scanning, way more than the 15deg sensitivity angle of the ultrasound sensor... gotta do something about that, probably via software.

Despite the fact they can rotate an extremely precise amount of degrees, unfortunately they cannot remember their position, it is normally up to the software to keep track of it.
But what happens if you reset the MCU running such piece of software while the motor is in a given position X?
It happens that whatever variable you were using -if not stored in some flash memory or similar solution- it will be re-initialized at boot time after the reset.

Sure, saving this variable in a non volatile storage might help... but still might fall short of other issues, such as a motor "missing" a few steps.
Actually you instruct the motor to move X steps, but you have no feedback if it actually moved that precise number of steps.
It normally does, unless it gets stuck against something.

So a good solution is to detect a zero position and reset your position variable on that at each boot or even from time to time during normal operation.

Then the question becomes :how to?
As we said, a stepper motor provides no feedback, so we need to ADD something that does.
A typical solution is to use a magnetic or optical device, basically something static that can detect a moving part without touching it.

I went for the magnetic solution, using a hall effect sensor and  neodymium magnets.


The one in the picture is just a test setup, as you can see the small hall effect sensor is taped down while the final solution will hopefully be a bit more stable.
The stepper motor is physically on the opposite side, below the wooden surface, and  a 2mm thick piece of plastic is connected to its shaft, coming this side of the chassis.



I am using some tiny round neodymium magnets (got 200 of those, also really cheap on ebay), they are 3mm in diameter and 1 mm thick, so I was able to embed 2 of them in the 2mm thick plastic.
I simply drilled a 2.8mm hole with my dremel, they are slightly forced in, no need for glue or anything else, they just stay there nicely.

I connected motor and sensor to the arduino, using a mega for this test,  a Uno or pro mini would do the same, I just had this one available at the moment and did not want to disconnect the others I am using for other purposes.

Then using the Stepper Library demo code (configured to run with the mega pins)
I managed to have the motor turn clockwise for a few steps and counter-clockwise for the same number of steps, continuously.
The LED is turned on if the digital pin connected to the sensor is LOW (sensor is active LOW).



The key thing is that I move the motor 1 step at a time and at each step I poll the sensor (you could theoretically use interrupts for this, but I will need two interrupts for the speed encoders and arduino does not have any spare int available :( ).

Now that we saw it works, we need to consider that, even if those magnets are really small -and you might find smaller ones, but they become more difficult to handle- they still cover an area that is represented by a finite number of steps.
Arguably this area might change a bit depending on the distance (the radius) at which you place them from the shaft.

The idea is to consider as ZERO the position of the stepper motor that corresponds to the center of the magnet being over the center of the sensor.
We then need to calculate, when the magnet hits the sensor, how many steps it needs to clear it again.

My simple code is here :

#include <Stepper.h>
int input_pin = 52;
int sensor_vcc =50;
int led_pin = 13;
int cnt=0;
int dir =-1;
int lastSensor = LOW;


// initialize the stepper library 
// I am using a MEGA!!
Stepper myStepper(stepsPerRevolution, 30,26,28,24);  

void setup()
{
 Serial.begin(9600);
 pinMode(led_pin,OUTPUT);
 pinMode(input_pin,INPUT_PULLUP);
 pinMode(sensor_vcc,OUTPUT);
 digitalWrite(sensor_vcc,HIGH); 
 digitalWrite(led_pin,LOW); 
 myStepper.setSpeed(40);
}

void loop()
{
   if (digitalRead(input_pin)==LOW)
   {
      //detecting magnet
      if (lastSensor==LOW)
      {
        cnt=0;
        lastSensor=HIGH;
      }
      cnt++;
   } else 
    if (lastSensor==HIGH) 
    {
      Serial.println(cnt);
      lastSensor= LOW;
    }
   myStepper.step(dir);   
}

Turns out that the readings are quite consistent and it usually takes 36 steps with that sensor, placed in that position, with those magnets etc... you need to run your own measurements if you try to replicate this experiment.



You probably noticed that the numbers tend to increase after few passes, that happened because the tape started to warp a bit when heated by the lamp and the sensor raised a bit getting closer to the magnet.
As I pressed it down again it temporarily went back to 37, this will not happen when the sensor is securely placed on the rover.
As a general rule, when you zero your motor at boot or anytime you may need it, you should also check again this number of steps and get half the value as the zero position.