Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Reading battery voltage in software (http://www.chiefdelphi.com/forums/showthread.php?t=43860)

RbtGal1351 12-02-2006 18:34

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

Mark McLeod 12-02-2006 18:37

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


lkdjm 12-02-2006 19:41

Re: Reading battery voltage in software
 
Quote:

Originally Posted by Mark McLeod
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


cool, thanks

the_short1 13-02-2006 10:19

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!

RbtGal1351 16-02-2006 03:01

Re: Reading battery voltage in software
 
thanks so much!

htwiz2002 28-01-2007 23:14

Re: Reading battery voltage in software
 
: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:
Code:

printf("%d",battery_voltage);
Can anyone help?:ahh:

Jared Russell 28-01-2007 23:38

Re: Reading battery voltage in software
 
Quote:

Originally Posted by htwiz2002 (Post 567287)
: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:
Code:

printf("%d",battery_voltage);
Can anyone help?:ahh:

It's a float.

htwiz2002 28-01-2007 23:47

Re: Reading battery voltage in software
 
Quote:

Originally Posted by Abwehr (Post 567299)
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

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);

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

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
        }


DanDon 29-01-2007 08:10

Re: Reading battery voltage in software
 
Quote:

Originally Posted by bear24rw (Post 567331)
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
    }



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

Re: Reading battery voltage in software
 
Quote:

Originally Posted by dhoizner (Post 567404)
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

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?

htwiz2002 29-01-2007 17:27

Re: Reading battery voltage in software
 
Quote:

Originally Posted by dhoizner (Post 567404)
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

Re: Reading battery voltage in software
 
Quote:

Originally Posted by Joel J. (Post 567621)
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:

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;



All times are GMT -5. The time now is 19:45.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi