First off, sorry if this is in the wrong channel.
My actual question - How often does the Cortex update its battery voltage?
In my experience, it does not update often. Is there something I am missing?
I think it continually refreshes, as the Cortex has to make sure the voltage doesn’t get too low or is too high to damage itself. You can access the last sampled battery voltage in millivolts in ROBOTC with nImmediateBatteryLevel. I get sub-second refreshing on my state robot.
I tried the nImmediateBatteryLevel but it is the same the whole time while i use it.
Post the code.
Do you have it in a loop? Post the code to take a look.
#pragma config(Sensor, in1, Expander, sensorAnalog)
#pragma platform(VEX)
#pragma competitionControl(Competition)
#pragma autonomousDuration(15)
#pragma userControlDuration(105)
#include “Vex_Competition_Includes.c”
float PrimaryBattery;
float SecondaryBattery;
float BackupBattery;
void Batteries()
{
PrimaryBattery = nImmediateBatteryLevel/85;
SecondaryBattery = SensorValue[Expander]/23.8;
BackupBattery = BackupBatteryLevel/90;
}
task usercontrol()
{
while (true)
{
Batteries();
}
}
Why are you dividing by 85?
Beware that’s an integer division (int / int) which will round to the nearest integer even if it’s assigned to a float.
Try
PrimaryBattery = (float) nImmediateBatteryLevel / 85;
SecondaryBattery = (float) SensorValue[Expander] / 23.8;
BackupBattery = (float) BackupBatteryLevel / 90;
OR
PrimaryBattery = nImmediateBatteryLevel / 85.0;
SecondaryBattery = SensorValue[Expander] / 23.8;
BackupBattery = BackupBatteryLevel / 90.0;
This is the part that matters. In the Loop there is other stuff that uses time so it won’t hog the cpu.
It is expressed in Percentage.
I know it works because the Secondary battery is always changing its reading, but the Primary Battery does not.(neither does the Backup battery but I do not care as much what its current battery level)
You are dividing the SecondaryBattery by a float (23.8) but the PrimaryBattery and BackupBattery is being divided by integers (85 & 90).
int / int is integer division (will round down to the nearest integer)
int / float is float division, both numbers are casted to floats and divided
float / int is float division, both numbers are casted to floats and divided
Adding any decimal will turn the literal into a float, otherwise they’re integers.
Again try
PrimaryBattery = (float) nImmediateBatteryLevel / 85;
SecondaryBattery = (float) SensorValue[Expander] / 23.8;
BackupBattery = (float) BackupBatteryLevel / 90;
This deliberately casts the short values (nImmediateBatteryLevel & BackupBatteryLevel) to floats creating float / int division.
OR
PrimaryBattery = nImmediateBatteryLevel / 85.0;
SecondaryBattery = SensorValue[Expander] / 23.8;
BackupBattery = BackupBatteryLevel / 90.0;
Which is always int / float division because all the rvalues are floats.
Thanks
The /85 comes from nImmediateBatteryLevel/1000 to give its volts and i wanted it in percent so I made it the 100 percent 8.5V which is usually what it is after it just finished charging.
So if I divided by 84.9 it would not round it, or is there another way I do not know about.
Just write 85 as 85.0 the .0 part tells the compiler you want a floating point literal.
I don’t know if ROBOTC understands this syntax but you can also write it as 85.f in C++.
Obviously if you want to use 84.9 you can but there’s no need to alter your number if you want 85 just write it as 85.0.
Thank you
No problem, classic misunderstanding. One of the NAR groups ran into it last season.
A couple of other comments on this.
nImmediateBatteryLevel returns the battery level in mV but it is not completely continuous, for example, it will not return 8500, 8499, 8498 etc. but rather there will be discrete steps of approximately 59mV. You would expect to see something like this, 8510, 8451, 8392, 8333 and so on. This is because the raw internal data is actually an 8 bit value that is scaled appropriately to create mV. This will only have a small impact on the display in percentage as 59mV is about 0.7% using the scale you have chosen.