# Reading battery voltage in software

Hi,

Is it possible for the software to read the battery voltage? (The main battery)
I know that the battery voltage is known, because you can read it on the OI. But can I access it in code, and how?

I tried searching ifirobotics and here but couldn’t find anything, just references to the OI.

Thanks,
~Stephanie

Look in ifi_aliases.h, line 300

``````
#define battery_voltage	 rxdata.rc_main_batt*15.64/256
#define backup_voltage	 rxdata.rc_backup_batt*15.64/256

``````

cool, thanks

thanks! that will be handy, if battery is too slow then maybe limit max speed of motor / turn off conveyor automatically to favour drivign!

thanks so much!

:yikes:Wait! What type of variable is it?!?! When I try to read it it outputs a number greater than 16k!
I’m building a terminal interface (for debugging) it outputs a really strange value when I do:

``````printf("%d",battery_voltage);
``````

Can anyone help?:ahh:

It’s a float.

So the printf replacement is “%f” ? (just because I’d rather set it now rather than try to remember it when I work on the code tomorrow…

If I recall correctly the printf functions in our code don’t support the %f sequence. If the decimal place does not matter to you, just cast the variable as an int when printing it out (i.e. (int)variable). If you do want the decimal places, do something like:

``````variabledeci = variable - (int)variable;

variabledeci*=1000;

printf("variable = %d.%03d", (int)variable, (int)variabledeci);

``````

This stores the decimal portion of the variable in another one, then multiplies by 1000 to get the decimals in front of the decimal point, and then prints the whole thing out.

Wrote this over the summer when playing around with the edu rc. It prints out a float

``````
current_voltage = rxdata.rc_main_batt; 		// Store current voltage
average_voltage += current_voltage;		// Add the current voltage to the average stack

voltage_count++;							// Increase the average stack count

if (voltage_count == 39)					      // If the stack is 40
{
average_voltage = (average_voltage / 40) * 0.038 + 0.05;	// Get the average and apply formula
i = (int)average_voltage;							// Truncate decimals
i2 = (int) ((average_voltage - i) * 1000);				// Subtract to get decimals only and then multiply by 1000
printf("Average: %d.%d
",i,i2);						// Print the two together to make it look like a decimal

average_voltage = 0;									// Reset the stack
voltage_count = 0;									// Rest the counter
}

``````

The only problem that I see with that is, if your voltage is let’s say 8.004, I believe that the way you are printing the variables would print 8.4.

If I recall correctly %03d forces three digits to be printed, which using the above example, would display 8.004.

lol true, i had 03 in there but i thought it was a mistake so i took it out before i posted. Nice catch

I have an itchy finger that wants to change that alias to remove the float. Is this kosher? Would bad things happen?

Alright! I’ll try it and see what happens.
Thank you very much!

I don’t see why they would, unless any code depends on it (which you would find out about very quickly :P)

If you want to be safe, you can just take advantage of the global nature of the rxdata struct to get at the raw data:

``````

if( rxdata.rc_main_batt < 127 )
printf("HAMMER TIME!
");

typedef struct  {     /* begin rx_data_record structure */

...
unsigned char rc_main_batt, rc_backup_batt;
...

} rx_data_record;

``````

I’m having the same problem, and I cannot figure it out at all!!! I’ve contacted our main programmer, I tried the code earlier (which gave me a reading of 70), and I’ve looked over and over in the code.

Holy cow! I know it’s possible (it sounds possible), but I never knew there was such a thing in the code. Thanks!

I have, in the past, used the main battery voltage to formulaicly modify the multiplier on the P term in the PID for the wheel motors (rate control). Worked great!

Also have used it to make power limits on non-servo systems vary to have the same power output (mechanical) regardless of lower battery voltage.

I.e., 25% power limits that stay constant regardless of battery voltage.

-q