Skip to content
Snippets Groups Projects
Commit 5496e6ef authored by Fernando Herrero's avatar Fernando Herrero
Browse files

Remove old segway_battery named files

parent 4411e0a8
No related branches found
No related tags found
No related merge requests found
#edit the following line to add the librarie's header files
FIND_PATH(segway_battery_INCLUDE_DIR segway_battery.h /usr/include/iridrivers /usr/local/include/iridrivers)
FIND_LIBRARY(segway_battery_LIBRARY
NAMES segway_battery
PATHS /usr/lib /usr/local/lib /usr/local/lib/iridrivers)
IF (segway_battery_INCLUDE_DIR AND segway_battery_LIBRARY)
SET(segway_battery_FOUND TRUE)
ENDIF (segway_battery_INCLUDE_DIR AND segway_battery_LIBRARY)
IF (segway_battery_FOUND)
IF (NOT segway_battery_FIND_QUIETLY)
MESSAGE(STATUS "Found segway_battery: ${segway_battery_LIBRARY}")
ENDIF (NOT segway_battery_FIND_QUIETLY)
ELSE (segway_battery_FOUND)
IF (segway_battery_FIND_REQUIRED)
MESSAGE(FATAL_ERROR "Could not find segway_battery")
ENDIF (segway_battery_FIND_REQUIRED)
ENDIF (segway_battery_FOUND)
#include "segway_battery.h"
#include "exceptions.h"
#include <iostream>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/kd.h>
#include <signal.h>
std::string battery_dev="A900WSG1";
bool condition1(TBatteryInfo *info)
{
if(info->battery_current<0)// battery is discharging
{
if(info->remaining_capacity<29990)
return true;
else
return false;
}
else
return false;
}
bool condition2(TBatteryInfo *info)
{
if(info->battery_current<0)// battery is discharging
{
if(info->remaining_capacity<29000)
return true;
else
return false;
}
else
return false;
}
int main(int argc, char *argv[])
{
try{
std::string cond1_event_id,cond1_name;
std::string cond2_event_id,cond2_name;
CSegway_Battery battery(battery_dev);
battery.enable_alarm_sound();
cond1_name="condition1";
cond1_event_id=battery.create_alarm(cond1_name,condition1,1);
battery.set_alarm_sound(cond1_name,440,1,10);
cond2_name="condition2";
cond2_event_id=battery.create_alarm(cond2_name,condition2,0);
battery.set_alarm_sound(cond2_name,440,1,1);
while(!battery.finish())
{
try{
std::cout << battery << std::endl;
sleep(1);
}catch(CException &e){
std::cout << e.what() << std::endl;
}
}
battery.disable_alarm_sound();
}catch(CException &e){
std::cout << e.what() << std::endl;
}
}
#include "segway_battery.h"
#include "exceptions.h"
#include <iostream>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/kd.h>
#include <signal.h>
#ifndef CLOCK_TICK_RATE
#define CLOCK_TICK_RATE 1193180
#endif
bool CSegway_Battery::signal_finish=false;
CSegway_Battery::CSegway_Battery(std::string &serial_num,bool handle_signals)
{
this->dyn_server=CDynamixelServerFTDI::instance();
try{
// create the serial port device to communicate with the battery
this->dyn_server->config_bus(serial_num,9600);
this->bat_mon=this->dyn_server->get_device(0x01);
// initialize the battery information
memset(&this->info,0x00,sizeof(TBatteryInfo));
// initialize sound variables
this->sound_enabled=false;
this->console_fd=-1;
// signal handling
if(handle_signals)
if(signal((int)SIGINT,CSegway_Battery::signal_handler)==SIG_ERR)
throw CException(_HERE_,"Impossible to assign a signal handler to the SIGINT signal");
}catch(CException &e){
if(this->bat_mon!=NULL)
{
this->dyn_server->free_device(0x01);
this->bat_mon=NULL;
}
throw;
}
// initialize the events
this->event_server=CEventServer::instance();
this->finish_thread_event_id="battery_monitor_finish_thread_event";
this->event_server->create_event(this->finish_thread_event_id);
this->finish_sound_event_id="battery_monitor_finish_sound_event";
this->event_server->create_event(this->finish_sound_event_id);
// initialize the threads
this->thread_server=CThreadServer::instance();
this->battery_monitor_thread_id="battery_monitor_thread";
this->thread_server->create_thread(this->battery_monitor_thread_id);
this->thread_server->attach_thread(this->battery_monitor_thread_id,this->battery_monitor_thread,this);
this->thread_server->start_thread(this->battery_monitor_thread_id);
this->battery_sound_thread_id="battery_sound_thread";
this->thread_server->create_thread(this->battery_sound_thread_id);
this->thread_server->attach_thread(this->battery_sound_thread_id,this->battery_sound_thread,this);
}
void *CSegway_Battery::battery_monitor_thread(void *param)
{
CSegway_Battery *battery=(CSegway_Battery *)param;
short int data[21];
bool end=false;
while(!end)
{
try{
if(battery->event_server->event_is_set(battery->finish_thread_event_id))
end=true;
else
{
/* get the data from the battery */
battery->bat_mon->read_registers(0x20,(unsigned char *)data,38);
/* convert the data */
battery->info.temperature=data[0]/10.0-273.15;
battery->info.battery_voltage=data[1]/1000.0;
battery->info.battery_current=data[2]/1000.0;
battery->info.battery_avg_current=data[3]/1000.0;
battery->info.relative_state_of_charge=data[4];
battery->info.absolute_state_of_charge=data[5];
battery->info.remaining_capacity=data[6]/1000.0;
battery->info.full_charge_capacity=data[7]/1000.0;
battery->info.run_time_to_empty=data[8];
battery->info.avg_time_to_empty=data[9];
battery->info.avg_time_to_full=data[10];
battery->info.charging_current=data[11]/1000.0;
battery->info.charging_voltage=data[12]/1000.0;
(*((unsigned short int *)&battery->info.status))=data[13];
battery->info.design_capacity=data[14]/1000.0;
battery->info.design_voltage=data[15]/1000.0;
// pack status ignored
battery->info.cell4_voltage=data[17]/1000.0;
battery->info.cell3_voltage=data[18]/1000.0;
battery->info.cell2_voltage=data[19]/1000.0;
battery->info.cell1_voltage=data[20]/1000.0;
}
}catch(CException &e){
std::cout << e.what() << std::endl;
}
}
pthread_exit(NULL);
}
void *CSegway_Battery::battery_sound_thread(void *param)
{
CSegway_Battery *battery=(CSegway_Battery *)param;
bool end=false;
int i;
while(!end)
{
try{
if(battery->event_server->event_is_set(battery->finish_sound_event_id))
end=true;
else
{
if(ioctl(battery->console_fd,KIOCSOUND,(int)(CLOCK_TICK_RATE/battery->current_alarm->frequency_hz))<0)
std::cout << "Error while setting the desired frequency" << std::endl;
for(i=0;i<battery->current_alarm->time_on_s;i++)
{
if(battery->event_server->event_is_set(battery->finish_sound_event_id))
{
end=true;
break;
}
sleep(1);
}
// stop the sound
if(ioctl(battery->console_fd,KIOCSOUND,0)<0)
std::cout << "Error while setting the desired frequency" << std::endl;
for(i=0;i<battery->current_alarm->time_off_s;i++)
{
if(battery->event_server->event_is_set(battery->finish_sound_event_id))
{
end=true;
break;
}
sleep(1);
}
}
}catch(CException &e){
std::cout << e.what() << std::endl;
}
}
pthread_exit(NULL);
}
void CSegway_Battery::signal_handler(int signum)
{
// signal the object to finish the execution
CSegway_Battery::signal_finish=true;
}
void CSegway_Battery::get_battery_info(TBatteryInfo *info)
{
if(info!=NULL)
memcpy(info,&this->info,sizeof(TBatteryInfo));
}
// alarm handling
void CSegway_Battery::enable_alarm_sound(void)
{
if(!this->sound_enabled)
{
if(this->console_fd!=-1)
close(this->console_fd);
if((this->console_fd=open("/dev/tty0",O_WRONLY)) == -1)
throw CException(_HERE_,"Impossible to initialize the sound system");
this->sound_enabled=true;
}
}
void CSegway_Battery::disable_alarm_sound(void)
{
// stop the thread if it is running
if(this->sound_enabled)
{
if(this->thread_server->get_thread_state(this->battery_sound_thread_id)==active)
{
this->event_server->set_event(this->finish_sound_event_id);
this->thread_server->end_thread(this->battery_sound_thread_id);
this->event_server->reset_event(this->finish_sound_event_id);
}
if(this->console_fd!=-1)
close(this->console_fd);
this->console_fd=-1;
this->sound_enabled=false;
}
}
bool CSegway_Battery::is_alarm_sound_enabled(void)
{
return this->sound_enabled;
}
std::string CSegway_Battery::create_alarm(std::string &name,bool (*function)(TBatteryInfo *info),int priority)
{
std::list<TAlarm>::iterator iterator;
TAlarm new_alarm;
for(iterator=this->alarms.begin();iterator!=this->alarms.end();iterator++)
{
if(iterator->id==name)
{
// handle exceptions
throw CException(_HERE_,"An other alarm with the same name already exists.");
}
else if(priority!=-1 && iterator->priority==priority)
{
// handle exceptions
throw CException(_HERE_,"An other alarm with the same priority 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";
new_alarm.priority=priority;
new_alarm.frequency_hz=440;
new_alarm.time_on_s=1;
new_alarm.time_off_s=10;
this->event_server->create_event(new_alarm.event_id);
new_alarm.condition_function=function;
if(this->alarms.size()>0 && priority!=-1)
{
for(iterator=this->alarms.begin();iterator!=this->alarms.end();iterator++)
if(iterator->priority>priority)
{
this->alarms.insert(iterator,new_alarm);
break;
}
if(iterator==this->alarms.end())
this->alarms.push_back(new_alarm);
}
else
this->alarms.push_back(new_alarm);
return new_alarm.event_id;
}
void CSegway_Battery::set_alarm_sound(std::string &name, int freq_hz,int time_on_s, int time_off_s)
{
std::list<TAlarm>::iterator iterator;
for(iterator=this->alarms.begin();iterator!=this->alarms.end();iterator++)
{
if(iterator->id==name)
{
iterator->frequency_hz=freq_hz;
iterator->time_on_s=time_on_s;
iterator->time_off_s=time_off_s;
return;
}
}
throw CException(_HERE_,"Alarm name not found");
}
void CSegway_Battery::delete_alarm(std::string &name)
{
std::list<TAlarm>::iterator iterator;
for(iterator=this->alarms.begin();iterator!=this->alarms.end();iterator++)
{
if(iterator->id==name)
{
this->event_server->delete_event(iterator->event_id);
this->alarms.erase(iterator);
return;
}
}
// handle exceptions
throw CException(_HERE_,"No alarm exists with the given name.");
}
bool CSegway_Battery::finish(void)
{
return this->signal_finish;
}
std::ostream& operator<< (std::ostream& out,CSegway_Battery& bat)
{
out << "Battery information:" << std::endl;
out << " Temperature: " << bat.info.temperature << " ºC" << std::endl;
out << " Voltage: " << bat.info.battery_voltage << " V" << std::endl;
out << " Current: " << bat.info.battery_current << " A" << std::endl;
out << " Average current: " << bat.info.battery_avg_current << " A" << std::endl;
out << " Relative state of charge: " << bat.info.relative_state_of_charge << " %" << std::endl;
out << " Absolute state of charge: " << bat.info.absolute_state_of_charge << " %" << std::endl;
out << " Remaining capacity: " << bat.info.remaining_capacity << " Ah" << std::endl;
out << " Full charge capacity: " << bat.info.full_charge_capacity << " Ah" << std::endl;
out << " Run time to empty: " << bat.info.run_time_to_empty << " min" << std::endl;
out << " Average time to empty: " << bat.info.avg_time_to_empty << " min" << std::endl;
out << " Average time to full: " << bat.info.avg_time_to_full << " min" << std::endl;
out << " Charging current: " << bat.info.charging_current << " A" << std::endl;
out << " Charging voltage: " << bat.info.charging_voltage << " V" << std::endl;
out << " Battery status: " << std::endl;
out << " over charged alarm: " << bat.info.status.over_charged_alarm << std::endl;
out << " terminate_charge alarm: " << bat.info.status.terminate_charge_alarm << std::endl;
out << " over temperature alarm: " << bat.info.status.over_temperature_alarm << std::endl;
out << " terminate discharge alarm: " << bat.info.status.terminate_discharge_alarm << std::endl;
out << " remaining capacity alarm: " << bat.info.status.remaining_capacity_alarm << std::endl;
out << " remaining time alarm: " << bat.info.status.remaining_time_alarm << std::endl;
out << " initialized: " << bat.info.status.initialized << std::endl;
out << " discharging: " << bat.info.status.discharging << std::endl;
out << " fully charged: " << bat.info.status.fully_charged << std::endl;
out << " fully discharged: " << bat.info.status.fully_discharged << std::endl;
out << " error: " << bat.info.status.error << std::endl;
out << " Design capacity: " << bat.info.design_capacity << " Ah" << std::endl;
out << " Design voltage: " << bat.info.design_voltage << " V" << std::endl;
out << " cell 4 voltage: " << bat.info.cell4_voltage << " V" << std::endl;
out << " cell 3 voltage: " << bat.info.cell3_voltage << " V" << std::endl;
out << " cell 2 voltage: " << bat.info.cell2_voltage << " V" << std::endl;
out << " cell 1 voltage: " << bat.info.cell1_voltage << " V" << std::endl;
return out;
}
CSegway_Battery::~CSegway_Battery()
{
std::list<TAlarm>::iterator iterator;
if(this->thread_server->get_thread_state(this->battery_monitor_thread_id)==active)
{
// signal the thread to terminate
this->event_server->set_event(this->finish_thread_event_id);
// wait for the thread to finish
this->thread_server->end_thread(this->battery_monitor_thread_id);
}
this->thread_server->detach_thread(this->battery_monitor_thread_id);
this->thread_server->delete_thread(this->battery_monitor_thread_id);
this->battery_monitor_thread_id="";
this->event_server->delete_event(this->finish_thread_event_id);
this->finish_thread_event_id="";
if(this->thread_server->get_thread_state(this->battery_sound_thread_id)==active)
{
this->event_server->set_event(this->finish_sound_event_id);
this->thread_server->end_thread(this->battery_sound_thread_id);
}
this->thread_server->detach_thread(this->battery_sound_thread_id);
this->thread_server->delete_thread(this->battery_sound_thread_id);
this->battery_sound_thread_id="";
this->event_server->delete_event(this->finish_sound_event_id);
this->finish_sound_event_id="";
// clear all alarms
for(iterator=this->alarms.begin();iterator!=this->alarms.end();iterator++)
{
this->event_server->delete_event(iterator->event_id);
iterator->event_id="";
iterator->id="";
iterator->condition_function=NULL;
}
this->alarms.clear();
// close the serial port
if(this->bat_mon!=NULL)
{
this->dyn_server->free_device(0x01);;
this->bat_mon=NULL;
}
}
#ifndef _SEGWAY_BATTERY_H
#define _SEGWAY_BATTERY_H
#include "threadserver.h"
#include "eventserver.h"
#include "dynamixel.h"
#include "dynamixelserver_ftdi.h"
#include "mutex.h"
typedef struct
{
unsigned short int error:3;
unsigned short int fully_discharged:1;
unsigned short int fully_charged:1;
unsigned short int discharging:1;
unsigned short int initialized:1;
unsigned short int remaining_time_alarm:1;
unsigned short int remaining_capacity_alarm:1;
unsigned short int reserved2:1;
unsigned short int terminate_discharge_alarm:1;
unsigned short int over_temperature_alarm:1;
unsigned short int reserved1:1;
unsigned short int terminate_charge_alarm:1;
unsigned short int over_charged_alarm:1;
}TBatteryStatus;
typedef struct
{
double temperature;
double battery_voltage;
double battery_current;
double battery_avg_current;
double relative_state_of_charge;
double absolute_state_of_charge;
double remaining_capacity;
double full_charge_capacity;
double run_time_to_empty;
double avg_time_to_empty;
double avg_time_to_full;
double charging_current;
double charging_voltage;
TBatteryStatus status;
double design_capacity;
double design_voltage;
unsigned short int pack_status;
double cell4_voltage;
double cell3_voltage;
double cell2_voltage;
double cell1_voltage;
}TBatteryInfo;
typedef struct{
std::string id;
std::string event_id;
int frequency_hz;
int time_on_s;
int time_off_s;
int priority;
bool (*condition_function)(TBatteryInfo *info);
}TAlarm;
class CSegway_Battery
{
private:
// pc speacker sound variables
bool sound_enabled;
int console_fd;
// serial device object
CDynamixel *bat_mon;
CDynamixelServerFTDI *dyn_server;
// thread attributes
CThreadServer *thread_server;
std::string battery_monitor_thread_id;
std::string battery_sound_thread_id;
// event attributes
CEventServer *event_server;
std::string finish_thread_event_id;
std::string finish_sound_event_id;
// battery information
TBatteryInfo info;
CMutex info_access;
// alarms
std::list<TAlarm> alarms;
std::list<TAlarm>::iterator current_alarm;
// signal variables
static bool signal_finish;
protected:
static void *battery_monitor_thread(void *param);
static void *battery_sound_thread(void *param);
static void signal_handler(int signum);
void parse_packet(unsigned char *data, int len);
public:
CSegway_Battery(std::string &serial_dev,bool handle_signals=true);
// basic battery info
void get_battery_info(TBatteryInfo *info);
// alarm handling
void enable_alarm_sound(void);
void disable_alarm_sound(void);
bool is_alarm_sound_enabled(void);
std::string create_alarm(std::string &name,bool (*function)(TBatteryInfo *info),int priority=-1);
void set_alarm_sound(std::string &name,int freq_hz,int time_on_s, int time_off_s);
void delete_alarm(std::string &name);
// signal handling
bool finish(void);
friend std::ostream& operator<< (std::ostream& out,CSegway_Battery& bat);
~CSegway_Battery();
};
#endif
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