High resolution Arduino map function

Posted by

In my last post, I talked about sensing the current of the motors in a robotic arm, which was a necessary step in determining energy consumption for the robot. But in order to calculate the energy being used by each motor, I need not only current data but also angular velocity. To get angular velocity, I just need to know the angle and the time, since angular velocity = (angle now – last angle)/(time now – last time). However, the map() function I used in Arduino to turn the potentiometer signals into degrees only deals in integer math, so I was losing a lot of data and getting 0 as my angular velocity a lot, which was really messing up the graph of shoulder speed (see below for an early screenshot – ack) and therefore messing up the energy consumption graph.

I figured I wasn’t the first person to come across this issue, so I posted on the forum and used the feedback to create a custom map_double function:

[code lang=”arduino”]
float map_double(double x, double in_min, double in_max, double out_min, double out_max)
{
return (x – in_min) * (out_max – out_min) / (in_max – in_min) + out_min;
}
[/code]

then just replaced map with map_float everywhere I had it in my original code. Yeah! More to come on the energy consumption of the entire system…

Leave a Reply

Your email address will not be published. Required fields are marked *