|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||||
|
|||||
|
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 |
|
#2
|
|||||
|
|||||
|
Re: Reading battery voltage in software
Look in ifi_aliases.h, line 300
Code:
#define battery_voltage rxdata.rc_main_batt*15.64/256 #define backup_voltage rxdata.rc_backup_batt*15.64/256 |
|
#3
|
||||
|
||||
|
Re: Reading battery voltage in software
Quote:
|
|
#4
|
|||||
|
|||||
|
Re: Reading battery voltage in software
thanks! that will be handy, if battery is too slow then maybe limit max speed of motor / turn off conveyor automatically to favour drivign!
|
|
#5
|
|||||
|
|||||
|
Re: Reading battery voltage in software
thanks so much!
|
|
#6
|
||||
|
||||
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: Code:
printf("%d",battery_voltage);
![]() |
|
#7
|
|||||
|
|||||
|
Re: Reading battery voltage in software
It's a float.
|
|
#8
|
||||
|
||||
|
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...
![]() |
|
#9
|
|||||
|
|||||
|
Re: Reading battery voltage in software
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:
Code:
variabledeci = variable - (int)variable;
variabledeci*=1000;
printf("variable = %d.%03d", (int)variable, (int)variabledeci);
|
|
#10
|
||||
|
||||
|
Re: Reading battery voltage in software
Wrote this over the summer when playing around with the edu rc. It prints out a float
Code:
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
}
Last edited by bear24rw : 29-01-2007 at 00:51. |
|
#11
|
|||||
|
|||||
|
Re: Reading battery voltage in software
Quote:
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. |
|
#12
|
||||
|
||||
|
Re: Reading battery voltage in software
lol true, i had 03 in there but i thought it was a mistake so i took it out before i posted. Nice catch
|
|
#13
|
||||
|
||||
|
Re: Reading battery voltage in software
I have an itchy finger that wants to change that alias to remove the float. Is this kosher? Would bad things happen?
|
|
#14
|
||||
|
||||
|
Re: Reading battery voltage in software
Quote:
Thank you very much! |
|
#15
|
|||
|
|||
|
Re: Reading battery voltage in software
Quote:
)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: Code:
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;
|
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| battery voltage compensation | Rickertsen2 | Programming | 5 | 17-10-2005 22:12 |
| RC Circuits | Melissa Nute | Math and Science | 3 | 25-01-2004 05:02 |
| Battery Chargers | Neal Probert | Electrical | 46 | 16-02-2003 22:31 |