Commit edbc2fdd authored by Sergi Hernandez's avatar Sergi Hernandez
Browse files

Integrated the buzzer into the alarm system of the charger and also for user use.

parent a0b4929e
......@@ -157,91 +157,164 @@ bool CBattery_Charger::is_charger_enabled(void)
/* fuel gauge functions */
double CBattery_Charger::get_battery_temperature(void)
{
unsigned short int value;
this->dyn_device->read_word_register(BATTERY_TEMPERATURE_L,&value);
return value*0.1-273.15;
}
double CBattery_Charger::get_battery_voltage(void)
{
short int value;
this->dyn_device->read_word_register(BATTERY_VOLTAGE_L,(unsigned short int *)&value);
return value/1000.0;
}
double CBattery_Charger::get_battery_current(void)
{
short int value;
this->dyn_device->read_word_register(BATTERY_CURRENT_L,(unsigned short int *)&value);
return value/1000.0;
}
double CBattery_Charger::get_battery_avg_current(void)
{
short int value;
this->dyn_device->read_word_register(BATTERY_AVG_CURRENT_L,(unsigned short int *)&value);
return value/1000.0;
}
unsigned char CBattery_Charger::get_battery_relative_soc(void)
{
unsigned char value;
this->dyn_device->read_byte_register(BATTERY_RELATIVE_SOC,&value);
return value;
}
unsigned char CBattery_Charger::get_battery_absolute_soc(void)
{
unsigned char value;
this->dyn_device->read_byte_register(BATTERY_RELATIVE_SOC,&value);
return value;
}
double CBattery_Charger::get_battery_remaining_capacity(void)
{
unsigned short int value;
this->dyn_device->read_word_register(BATTERY_REMAINING_CAP_L,&value);
return value/1000.0;
}
double CBattery_Charger::get_battery_full_charge_capacity(void)
{
unsigned short int value;
this->dyn_device->read_word_register(BATTERY_FULL_CHARGE_CAP_L,&value);
return value/1000.0;
}
unsigned int CBattery_Charger::get_battery_run_time_to_empty(void)
{
unsigned short int value;
this->dyn_device->read_word_register(BATTERY_RUN_TIME_EMPTY_L,&value);
return value;
}
unsigned int CBattery_Charger::get_battery_avg_time_to_empty(void)
{
unsigned short int value;
this->dyn_device->read_word_register(BATTERY_AVG_TIME_EMPTY_L,&value);
return value;
}
unsigned int CBattery_Charger::get_battery_avg_time_to_full(void)
{
unsigned short int value;
this->dyn_device->read_word_register(BATTERY_AVG_TIME_FULL_L,&value);
return value;
}
unsigned short int CBattery_Charger::get_battery_status(void)
{
unsigned short int value;
this->dyn_device->read_word_register(BATTERY_STATUS_L,&value);
return value;
}
double CBattery_Charger::get_battery_design_capacity(void)
{
unsigned short int value;
this->dyn_device->read_word_register(BATTERY_DESIGN_CAP_L,&value);
return value/1000.0;
}
double CBattery_Charger::get_battery_design_voltage(void)
{
unsigned short int value;
this->dyn_device->read_word_register(BATTERY_DESIGN_VOLTAGE_L,&value);
return value/1000.0;
}
double CBattery_Charger::get_battery_cell1_voltage(void)
{
unsigned short int value;
this->dyn_device->read_word_register(BATTERY_CELL1_VOLTAGE_L,&value);
return value/1000.0;
}
double CBattery_Charger::get_battery_cell2_voltage(void)
{
unsigned short int value;
this->dyn_device->read_word_register(BATTERY_CELL2_VOLTAGE_L,&value);
return value/1000.0;
}
double CBattery_Charger::get_battery_cell3_voltage(void)
{
unsigned short int value;
this->dyn_device->read_word_register(BATTERY_CELL3_VOLTAGE_L,&value);
return value/1000.0;
}
double CBattery_Charger::get_battery_cell4_voltage(void)
{
unsigned short int value;
this->dyn_device->read_word_register(BATTERY_CELL4_VOLTAGE_L,&value);
return value/1000.0;
}
......@@ -272,6 +345,137 @@ void CBattery_Charger::get_battery_all_data(TBatteryInfo *info)
info->cell4_voltage=(data[32]+(data[33]<<8))/1000.0;
}
/* buzzer functions */
void CBattery_Charger::start_buzzer(TBuzzer *buzzer)
{
unsigned char status;
/* check if the buzzer is active */
this->dyn_device->read_byte_register(BATTERY_BUZZER_STATUS,&status);
if(status&0x01)
this->stop_buzzer();
/* set the new parameters */
this->dyn_device->write_word_register(BATTERY_BUZZER_FREQ_L,buzzer->freq_hz);
this->dyn_device->write_word_register(BATTERY_BUZZER_TIME_ON_L,buzzer->time_on_s);
this->dyn_device->write_word_register(BATTERY_BUZZER_TIME_OFF_L,buzzer->time_off_s);
/* start the buzzer */
this->dyn_device->read_byte_register(BATTERY_BUZZER_CONTROL,&status);
status|=0x02;
this->dyn_device->write_byte_register(BATTERY_BUZZER_CONTROL,status);
}
void CBattery_Charger::get_buzzer_config(TBuzzer *buzzer)
{
unsigned char data[6];
this->dyn_device->read_registers(BATTERY_BUZZER_FREQ_L,data,6);
buzzer->freq_hz=data[0]+(data[1]<<8);
buzzer->time_on_s=data[2]+(data[3]<<8);
buzzer->time_off_s=data[4]+(data[5]<<8);
}
void CBattery_Charger::stop_buzzer(void)
{
unsigned char status;
this->dyn_device->read_byte_register(BATTERY_BUZZER_CONTROL,&status);
status&=0xFD;
this->dyn_device->write_byte_register(BATTERY_BUZZER_CONTROL,status);
}
void CBattery_Charger::enable_buzzer_alarm(void)
{
unsigned char status;
this->dyn_device->read_byte_register(BATTERY_BUZZER_CONTROL,&status);
status|=0x01;
this->dyn_device->write_byte_register(BATTERY_BUZZER_CONTROL,status);
}
void CBattery_Charger::disable_buzzer_alarm(void)
{
unsigned char status;
this->dyn_device->read_byte_register(BATTERY_BUZZER_CONTROL,&status);
status&=0xFE;
this->dyn_device->write_byte_register(BATTERY_BUZZER_CONTROL,status);
}
bool CBattery_Charger::is_buzzer_alarm_enabled(void)
{
unsigned char status;
this->dyn_device->read_byte_register(BATTERY_BUZZER_CONTROL,&status);
if(status&0x01)
return true;
else
return false;
}
void CBattery_Charger::set_warning_alarm(unsigned char time_s,unsigned char time_hist_s,TBuzzer *buzzer)
{
unsigned char error_time;
/* get the error time */
this->dyn_device->read_byte_register(BATTERY_ERROR_TIME,&error_time);
if(time_s<error_time)
throw CBatteryChargerException(_HERE_,"The warning time to empty can not be smaller than the error time");
this->dyn_device->write_byte_register(BATTERY_WARNING_TIME,time_s);
this->dyn_device->write_byte_register(BATTERY_WARNING_TIME_HIST,time_hist_s);
this->dyn_device->write_byte_register(BATTERY_WARNING_FREQ_L,buzzer->freq_hz&0x00FF);
this->dyn_device->write_byte_register(BATTERY_WARNING_FREQ_H,(buzzer->freq_hz>>8)&0x00FF);
this->dyn_device->write_byte_register(BATTERY_WARNING_TIME_ON_L,buzzer->time_on_s&0x00FF);
this->dyn_device->write_byte_register(BATTERY_WARNING_TIME_ON_H,(buzzer->time_on_s>>8)&0x00FF);
this->dyn_device->write_byte_register(BATTERY_WARNING_TIME_OFF_L,buzzer->time_off_s);
this->dyn_device->write_byte_register(BATTERY_WARNING_TIME_OFF_H,(buzzer->time_off_s>>8)&0x00FF);
}
void CBattery_Charger::get_warning_alarm(unsigned char *time_s,unsigned char *time_hist_s,TBuzzer *buzzer)
{
unsigned char data[8];
this->dyn_device->read_registers(BATTERY_WARNING_TIME,data,8);
*time_s=data[0];
*time_hist_s=data[1];
buzzer->freq_hz=data[2]+(data[3]<<8);
buzzer->time_on_s=data[4]+(data[5]<<8);
buzzer->time_off_s=data[6]+(data[7]<<8);
}
void CBattery_Charger::set_error_alarm(unsigned char time_s,unsigned char time_hist_s,TBuzzer *buzzer)
{
unsigned char warning_time;
/* get the error time */
this->dyn_device->read_byte_register(BATTERY_WARNING_TIME,&warning_time);
if(time_s>warning_time)
throw CBatteryChargerException(_HERE_,"The error time to empty can not be greater than the warning time");
this->dyn_device->write_byte_register(BATTERY_ERROR_TIME,time_s);
this->dyn_device->write_byte_register(BATTERY_ERROR_TIME_HIST,time_hist_s);
this->dyn_device->write_byte_register(BATTERY_ERROR_FREQ_L,buzzer->freq_hz&0x00FF);
this->dyn_device->write_byte_register(BATTERY_ERROR_FREQ_H,(buzzer->freq_hz>>8)&0x00FF);
this->dyn_device->write_byte_register(BATTERY_ERROR_TIME_ON_L,buzzer->time_on_s);
this->dyn_device->write_byte_register(BATTERY_ERROR_TIME_ON_H,(buzzer->time_on_s>>8)&0x00FF);
this->dyn_device->write_byte_register(BATTERY_ERROR_TIME_OFF_L,buzzer->time_off_s);
this->dyn_device->write_byte_register(BATTERY_ERROR_TIME_OFF_H,(buzzer->time_off_s>>8)&0x00FF);
this->dyn_device->write_byte_register(BATTERY_ERROR_TIME_OFF_L,buzzer->time_off_s);
}
void CBattery_Charger::get_error_alarm(unsigned char *time_s,unsigned char *time_hist_s,TBuzzer *buzzer)
{
unsigned char data[8];
this->dyn_device->read_registers(BATTERY_ERROR_TIME,data,8);
*time_s=data[0];
*time_hist_s=data[1];
buzzer->freq_hz=data[2]+(data[3]<<8);
buzzer->time_on_s=data[4]+(data[5]<<8);
buzzer->time_off_s=data[6]+(data[7]<<8);
}
CBattery_Charger::~CBattery_Charger()
{
if(this->dyn_device!=NULL)
......
......@@ -27,6 +27,13 @@ typedef struct{
double cell4_voltage;
}TBatteryInfo;
typedef struct
{
unsigned short int freq_hz;
unsigned short int time_on_s;
unsigned short int time_off_s;
}TBuzzer;
class CBattery_Charger
{
private:
......@@ -68,6 +75,17 @@ class CBattery_Charger
double get_battery_cell3_voltage(void);
double get_battery_cell4_voltage(void);
void get_battery_all_data(TBatteryInfo *info);
/* buzzer functions */
void start_buzzer(TBuzzer *buzzer);
void get_buzzer_config(TBuzzer *buzzer);
void stop_buzzer(void);
void enable_buzzer_alarm(void);
void disable_buzzer_alarm(void);
bool is_buzzer_alarm_enabled(void);
void set_warning_alarm(unsigned char time_s,unsigned char time_hist_s,TBuzzer *buzzer);
void get_warning_alarm(unsigned char *time_s,unsigned char *time_hist_s,TBuzzer *buzzer);
void set_error_alarm(unsigned char time_s,unsigned char time_hist_s,TBuzzer *buzzer);
void get_error_alarm(unsigned char *time_s,unsigned char *time_hist_s,TBuzzer *buzzer);
~CBattery_Charger();
};
......
......@@ -7,12 +7,42 @@ std::string battery_serial="A401293W";
int main(int argc, char *argv[])
{
unsigned char time,time_hist;
double voltage,current;
TBatteryInfo info;
TBuzzer buzzer;
int i=0;
try{
CBattery_Charger battery(battery_name,battery_serial,57600,0xC0);
battery.get_buzzer_config(&buzzer);
std::cout << "Buzzer frequency: " << buzzer.freq_hz << " Hz" << std::endl;
std::cout << "Buzzer time on: " << buzzer.time_on_s << " s" << std::endl;
std::cout << "Buzzer time off: " << buzzer.time_off_s << " s" << std::endl;
buzzer.freq_hz=260;
battery.start_buzzer(&buzzer);
battery.get_buzzer_config(&buzzer);
std::cout << "Buzzer frequency: " << buzzer.freq_hz << " Hz" << std::endl;
std::cout << "Buzzer time on: " << buzzer.time_on_s << " s" << std::endl;
std::cout << "Buzzer time off: " << buzzer.time_off_s << " s" << std::endl;
sleep(4);
battery.stop_buzzer();
battery.get_warning_alarm(&time,&time_hist,&buzzer);
std::cout << "Warning alarm time: " << (int)time << " min" << std::endl;
std::cout << "Warning alarm time hysteresis: " << (int)time_hist << " min" << std::endl;
std::cout << "Warning alarm frequency: " << buzzer.freq_hz << " Hz" << std::endl;
std::cout << "Warning alarm time on: " << buzzer.time_on_s << " s" << std::endl;
std::cout << "Warning alarm time off: " << buzzer.time_off_s << " s" << std::endl;
time=180;
battery.set_warning_alarm(time,time_hist,&buzzer);
battery.get_error_alarm(&time,&time_hist,&buzzer);
std::cout << "Error alarm time: " << (int)time << " min" << std::endl;
std::cout << "Error alarm time hysteresis: " << (int)time_hist << " min" << std::endl;
std::cout << "Error alarm frequency: " << buzzer.freq_hz << " Hz" << std::endl;
std::cout << "Error alarm time on: " << buzzer.time_on_s << " s" << std::endl;
std::cout << "Error alarm time off: " << buzzer.time_off_s << " s" << std::endl;
time=170;
battery.set_error_alarm(time,time_hist,&buzzer);
for(i=0;i<100;i++)
{
battery.get_no_battery_output(&voltage,&current);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment