Log in

View Full Version : Reading battery voltage in software


RbtGal1351
12-02-2006, 18:34
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

Mark McLeod
12-02-2006, 18:37
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

lkdjm
12-02-2006, 19:41
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

the_short1
13-02-2006, 10:19
thanks! that will be handy, if battery is too slow then maybe limit max speed of motor / turn off conveyor automatically to favour drivign!

RbtGal1351
16-02-2006, 03:01
thanks so much!

htwiz2002
28-01-2007, 23:14
: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:

Jared Russell
28-01-2007, 23:38
: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.

htwiz2002
28-01-2007, 23:47
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... :p

DanDon
28-01-2007, 23:59
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.

bear24rw
29-01-2007, 00:49
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 \n",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
}

DanDon
29-01-2007, 08:10
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 \n",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.

bear24rw
29-01-2007, 16:27
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

Joel J
29-01-2007, 16:40
I have an itchy finger that wants to change that alias to remove the float. Is this kosher? Would bad things happen?

htwiz2002
29-01-2007, 17:27
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.
Alright! I'll try it and see what happens.
Thank you very much!

Shinigami2057
29-01-2007, 22:27
I have an itchy finger that wants to change that alias to remove the float. Is this kosher? Would bad things happen?

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!\n");

typedef struct { /* begin rx_data_record structure */


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

} rx_data_record;

RyanN
20-10-2007, 18:32
: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:

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.

Bomberofdoom
21-10-2007, 05:21
Holy cow! I know it's possible (it sounds possible), but I never knew there was such a thing in the code. Thanks!

Qbranch
21-10-2007, 13:07
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