Page 1 of 1

How can I use the battery sensor

Posted: Sun Aug 02, 2020 8:14 am
by Ryan
Hey, I try to use the battery sensor and show the Time left in seconds. I have added the sensor. But when I try to print out the variable of Timeleft, it shows there are infinite time. It is more likely that this battery does not enable. So how can I enable this battery sensors.

Code: Select all

m_pcBattery = GetSensor <CBatteryDefaultSensor >("battery"); const CCI_BatterySensor::SReading & tBatteryReads = m_pcBattery->GetReading(); Real timeleft = tBatteryReads.AvailableCharge; std::cout<<" TimeLeft:" << timeleft <<"\n";

Re: How can I use the battery sensor

Posted: Sun Aug 02, 2020 8:43 pm
by pincy
Can you share a more detailed report of the problem?
  1. Where is this code written?
  2. What XML did you write to activate the sensor?

Re: How can I use the battery sensor

Posted: Mon Aug 03, 2020 1:32 am
by Ryan
In the controllers folder:

Code: Select all

void CFootBot::Init(TConfigurationNode& t_node){ m_pcBattery = GetSensor <CBatteryDefaultSensor >("battery"); } void CFootBot::ControlStep() { const CCI_BatterySensor::SReading & tBatteryReads = m_pcBattery->GetReading(); Real timeleft = tBatteryReads.AvailableCharge; std::cout<<"TimeLeft:" << timeleft <<"\n"; }
In the XML:

Code: Select all

<sensors> <battery implementation="default" /> </sensors>

Re: How can I use the battery sensor

Posted: Mon Aug 03, 2020 2:42 am
by pincy
In the .argos file, you need to configure the battery in two places:

Code: Select all

<controllers> <my_controller ...> <sensors> ... <battery implementation="default" /> </sensors> ... </my_controller> </controllers> <arena> <foot-bot ...> ... <battery id="battery_0" discharge_model="time" delta="0.001" /> </foot-bot> </arena>
The parameter "delta" of the battery is how much of the battery is lost at every time step. So, if you want your battery to last 1000 time steps, then delta is 1 / 1000.

I found a small bug in the way the battery is discharged. Please update ARGoS and recompile it from the sources. If you need assistance with that, let me know.

Re: How can I use the battery sensor

Posted: Fri Aug 07, 2020 5:03 pm
by Ryan
Now, I tried to add the footbot in the loopfunction file. And I tried to set the battery sensor in model of "DischargeModelTimeMotion". But it seems that the value of TimeLeft has no change.

In the loop function file:

Code: Select all

void CLoopFunctions::Init(TConfigurationNode& t_node) { /* * Create the foot-bot and get a reference to its battery */ m_pcFootBot = new CFootBotEntity( "fb", // entity id "fdc" // controller id as set in the XML ); AddEntity(*m_pcFootBot); m_pcBattery = &dynamic_cast<CBatteryEquippedEntity&>(m_pcFootBot->GetBatterySensorEquippedEntity()); // m_pcDischargeModelMotion = new CBatteryDischargeModelMotion(); // m_pcDischargeModelTime = new CBatteryDischargeModelTime(); m_pcDischargeModelTimeMotion = new CBatteryDischargeModelTimeMotion(); m_pcDischargeModelTimeMotion->SetDelta(0.01); m_pcDischargeModelTimeMotion->SetPosFactor(0.3); m_pcDischargeModelTimeMotion->SetOrientFactor(0.2); m_pcDischargeModelTimeMotion->SetBattery(m_pcBattery); m_pcDischargeModelTimeMotion->operator()(); m_pcBattery->SetAvailableCharge(0.01); }

In the controller file:

Code: Select all

void Init(TConfigurationNode& t_node){ m_pcBattery = GetSensor <CBatteryDefaultSensor >("battery"); m_pcBattery->Update(); } void ControlStep() { const CCI_BatterySensor::SReading & tBatteryReads = m_pcBattery->GetReading(); Real timeleft = tBatteryReads.AvailableCharge; std::cout<<" TimeLeft:" << timeleft <<"\n"; }

Re: How can I use the battery sensor

Posted: Sat Aug 08, 2020 1:27 pm
by Ryan
Now, I tried to add the footbot in the loopfunction file. And I tried to set the battery sensor in the model of "DischargeModelTimeMotion". But it seems that the value of TimeLeft has no change. How can I fix it?

In the loop function file:

Code: Select all

void CLoopFunctions::Init(TConfigurationNode& t_node) { /* * Create the foot-bot and get a reference to its battery */ m_pcFootBot = new CFootBotEntity( "fb", // entity id "fdc" // controller id as set in the XML ); AddEntity(*m_pcFootBot); m_pcBattery = &dynamic_cast<CBatteryEquippedEntity&>(m_pcFootBot->GetBatterySensorEquippedEntity()); // m_pcDischargeModelMotion = new CBatteryDischargeModelMotion(); // m_pcDischargeModelTime = new CBatteryDischargeModelTime(); m_pcDischargeModelTimeMotion = new CBatteryDischargeModelTimeMotion(); m_pcDischargeModelTimeMotion->SetDelta(0.01); m_pcDischargeModelTimeMotion->SetPosFactor(0.3); m_pcDischargeModelTimeMotion->SetOrientFactor(0.2); m_pcDischargeModelTimeMotion->SetBattery(m_pcBattery); m_pcDischargeModelTimeMotion->operator()(); m_pcBattery->SetAvailableCharge(0.01); }

In the controller file:

Code: Select all

void Init(TConfigurationNode& t_node){ m_pcBattery = GetSensor <CBatteryDefaultSensor >("battery"); m_pcBattery->Update(); } void ControlStep() { const CCI_BatterySensor::SReading & tBatteryReads = m_pcBattery->GetReading(); Real timeleft = tBatteryReads.AvailableCharge; std::cout<<" TimeLeft:" << timeleft <<"\n"; }