Skip to content
Snippets Groups Projects
Commit 6ee36208 authored by Sergi Hernandez's avatar Sergi Hernandez
Browse files

Added configurable alarms to notify when something is wrong with the battery.

parent 8dea78a1
No related branches found
No related tags found
No related merge requests found
......@@ -4,11 +4,24 @@
std::string battery_dev="A600eByq";
bool condition1(TBatteryInfo *info)
{
if(info->rem_capacity<28600)
return true;
else
return false;
}
int main(int argc, char *argv[])
{
CEventServer *event_server=CEventServer::instance();
std::string cond1_event_id,cond1_name;
CSegway_Battery battery(battery_dev);
bool end=false;
while(1)
cond1_name="condition1";
cond1_event_id=battery.create_alarm(cond1_name,condition1);
while(!end)
{
try{
std::cout << "Battery voltage: " << battery.get_current_voltage() << " V" << std::endl;
......@@ -18,7 +31,10 @@ int main(int argc, char *argv[])
std::cout << "Remaining capacity: " << battery.get_remaining_capacity() << " mAh" << std::endl;
std::cout << "Time to fully charged: " << battery.get_time_to_charged() << " min" << std::endl;
std::cout << "Time to fully discharged: " << battery.get_time_to_discharged() << " min" << std::endl;
sleep(1);
if(event_server->event_is_set(cond1_event_id))
end=true;
else
sleep(1);
}catch(CException &e){
std::cout << e.what() << std::endl;
}
......
......@@ -96,6 +96,7 @@ void *CSegway_Battery::battery_monitor_thread(void *param)
void CSegway_Battery::parse_packet(unsigned char *data, int len)
{
int i=0;
unsigned int j=0;
static int pos=-2;
float vt=0.0,res=0.0,k=0.0;
static unsigned char packet_data[31];
......@@ -154,6 +155,15 @@ void CSegway_Battery::parse_packet(unsigned char *data, int len)
this->info.time_to_charged=packet_data[27]+packet_data[28]*256;
// convert time to discharged
this->info.time_to_discharged=packet_data[29]+packet_data[30]*256;
// check all alarms
for(j=0;j<this->alarms.size();j++)
{
if(this->alarms[j].condition_function(&this->info))
{
if(!this->event_server->event_is_set(this->alarms[j].event_id))
this->event_server->set_event(this->alarms[j].event_id);
}
}
}
}
}
......@@ -233,9 +243,57 @@ bool CSegway_Battery::over_voltage_protection(void)
else
return false;
}
// alarm handling
std::string CSegway_Battery::create_alarm(std::string &name,bool (*function)(TBatteryInfo *info))
{
TAlarm new_alarm;
unsigned int i=0;
for(i=0;i<this->alarms.size();i++)
{
if(this->alarms[i].id==name)
{
// handle exceptions
throw CException(_HERE_,"An other alarm with the same name already exists.");
}
}
if(function==NULL)
{
// handle exceptions
throw CException(_HERE_,"Invalid condition function.");
}
// initialize the new alarm
new_alarm.id=name;
new_alarm.event_id=name+"_event_id";
this->event_server->create_event(new_alarm.event_id);
new_alarm.condition_function=function;
this->alarms.push_back(new_alarm);
return new_alarm.event_id;
}
void CSegway_Battery::delete_alarm(std::string &name)
{
unsigned int i=0;
for(i=0;i<this->alarms.size();i++)
{
if(this->alarms[i].id==name)
{
this->event_server->delete_event(this->alarms[i].event_id);
this->alarms.erase(this->alarms.begin()+i);
return;
}
}
// handle exceptions
throw CException(_HERE_,"No alarm exists with the given name.");
}
CSegway_Battery::~CSegway_Battery()
{
unsigned int i=0;
if(this->finish_thread_event_id!="")
{
// signal the thread to terminate
......@@ -251,6 +309,15 @@ CSegway_Battery::~CSegway_Battery()
this->event_server->delete_event(this->finish_thread_event_id);
this->finish_thread_event_id="";
}
// clear all alarms
for(i=0;i<this->alarms.size();i++)
{
this->event_server->delete_event(this->alarms[i].event_id);
this->alarms[i].event_id="";
this->alarms[i].id="";
this->alarms[i].condition_function=NULL;
}
this->alarms.clear();
// close the serial port
if(this->serial!=NULL)
{
......
......@@ -25,6 +25,12 @@ typedef struct
double time_to_discharged;
}TBatteryInfo;
typedef struct{
std::string id;
std::string event_id;
bool (*condition_function)(TBatteryInfo *info);
}TAlarm;
class CSegway_Battery
{
private:
......@@ -39,11 +45,14 @@ class CSegway_Battery
// battery information
TBatteryInfo info;
CMutex info_access;
// alarms
std::vector<TAlarm> alarms;
protected:
static void *battery_monitor_thread(void *param);
void parse_packet(unsigned char *data, int len);
public:
CSegway_Battery(std::string &serial_dev);
// basic battery info
double get_current_voltage(void);
double get_current_temperature(void);
double get_input_current(void);
......@@ -56,6 +65,9 @@ class CSegway_Battery
bool is_load_enabled(void);
bool under_voltage_protection(void);
bool over_voltage_protection(void);
// alarm handling
std::string create_alarm(std::string &name,bool (*function)(TBatteryInfo *info));
void delete_alarm(std::string &name);
~CSegway_Battery();
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment