From 8dea78a1695c8e87c36a33fd9c52030453e8ebba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sergi=20Hern=C3=A0ndez=20Juan?= <shernand@iri.upc.edu>
Date: Wed, 4 Jul 2012 08:04:08 +0000
Subject: [PATCH] Chnaged the driver to use the CFTDI class instead of the
 CRS232 class.

---
 src/examples/segway_battery_test.cpp |  3 +-
 src/segway_battery.cpp               | 77 ++++++++++++++++++++++------
 src/segway_battery.h                 |  5 +-
 3 files changed, 65 insertions(+), 20 deletions(-)

diff --git a/src/examples/segway_battery_test.cpp b/src/examples/segway_battery_test.cpp
index db77aef..543eae7 100755
--- a/src/examples/segway_battery_test.cpp
+++ b/src/examples/segway_battery_test.cpp
@@ -2,7 +2,7 @@
 #include "exceptions.h"
 #include <iostream>
 
-std::string battery_dev="/dev/ttyUSB0";
+std::string battery_dev="A600eByq";
 
 int main(int argc, char *argv[])
 {
@@ -12,6 +12,7 @@ int main(int argc, char *argv[])
   {
     try{
       std::cout << "Battery voltage: " << battery.get_current_voltage() << " V" << std::endl;
+      std::cout << "Battery temperature: " << battery.get_current_temperature() << " ºC" << std::endl;
       std::cout << "Input current: " << battery.get_input_current() << " mA" << std::endl;
       std::cout << "Output current: " << battery.get_output_current() << " mA" << std::endl;
       std::cout << "Remaining capacity: " << battery.get_remaining_capacity() << " mAh" << std::endl;
diff --git a/src/segway_battery.cpp b/src/segway_battery.cpp
index e28fb38..f88eb05 100755
--- a/src/segway_battery.cpp
+++ b/src/segway_battery.cpp
@@ -1,21 +1,32 @@
 #include "segway_battery.h"
 #include "exceptions.h"
 #include <iostream>
+#include <math.h>
 
-CSegway_Battery::CSegway_Battery(std::string &serial_dev)
+CSegway_Battery::CSegway_Battery(std::string &serial_num)
 {
-  TRS232_config serial_config;
+  CFTDIServer *ftdi_server=CFTDIServer::instance();
+  TFTDIconfig ftdi_config;
 
   try{
     // create the serial port device to communicate with the battery
-    this->serial=new CRS232(std::string("battery_monitor_dev"));
-    // open the serial device
-    this->serial->open((void *)&serial_dev);
-    serial_config.baud=9600;
-    serial_config.num_bits=8;
-    serial_config.parity=none;
-    serial_config.stop_bits=1;
-    this->serial->config(&serial_config);
+    if(ftdi_server->get_num_devices()>0)
+    {
+      this->serial=ftdi_server->get_device(serial_num);
+      if(this->serial==NULL)
+      {
+        /* handle exceptions */
+        throw CException(_HERE_,"No such device");
+      }
+      ftdi_config.baud_rate = 9600;
+      ftdi_config.word_length = 8;
+      ftdi_config.stop_bits = 1;
+      ftdi_config.parity = 0;
+      ftdi_config.read_timeout = 1000;
+      ftdi_config.write_timeout = 1000;
+      ftdi_config.latency_timer = 16;
+      this->serial->config(&ftdi_config);
+    }
     // initialize the battery information
     this->info.battery_voltage=0.0;
     this->info.temperature1=0.0;
@@ -86,6 +97,7 @@ void CSegway_Battery::parse_packet(unsigned char *data, int len)
 {
   int i=0;
   static int pos=-2;
+  float vt=0.0,res=0.0,k=0.0;
   static unsigned char packet_data[31];
 
   for(i=0;i<len;i++)
@@ -111,6 +123,7 @@ void CSegway_Battery::parse_packet(unsigned char *data, int len)
       if(pos==31)
       {
         pos=-2;
+        // convert voltage
         this->info.battery_voltage=(packet_data[0])+(packet_data[1]<<8);
         this->info.battery_voltage+=(packet_data[2])+(packet_data[3]<<8);
         this->info.battery_voltage+=(packet_data[4])+(packet_data[5]<<8);
@@ -120,10 +133,26 @@ void CSegway_Battery::parse_packet(unsigned char *data, int len)
         this->info.battery_voltage+=(packet_data[12])+(packet_data[13]<<8);
         this->info.battery_voltage+=(packet_data[14])+(packet_data[15]<<8);
         this->info.battery_voltage/=6.5536;
+        // convert temperature
+        vt=(packet_data[16]+packet_data[17]*256)*2500.0/32768.0;
+        res=230000*vt/(3300-vt);
+        k=(1/298.15)-(log(10000.0/res)/3435.0);
+        this->info.temperature1=(1-273.15*k)/k;
+        vt=(packet_data[18]+packet_data[19]*256)*2500.0/32768.0;
+        res=230000*vt/(3300-vt);
+        k=(1/298.15)-(log(10000.0/res)/3435.0);
+        this->info.temperature2=(1-273.15*k)/k;
+        // get the status
+        this->info.status=packet_data[20];
+        // convert input current
         this->info.current_in=packet_data[21]+packet_data[22]*256;
+        // convert output current
         this->info.current_out=packet_data[23]+packet_data[24]*256;
+        // convert remaining capacity
         this->info.rem_capacity=packet_data[25]+packet_data[26]*256;
+        // convert time to charged
         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;
       }
     }
@@ -137,8 +166,7 @@ double CSegway_Battery::get_current_voltage(void)
 
 double CSegway_Battery::get_current_temperature(void)
 {
-  return 0.0;
-
+  return (this->info.temperature1+this->info.temperature2)/2.0;
 }
 
 double CSegway_Battery::get_input_current(void)
@@ -168,27 +196,42 @@ double CSegway_Battery::get_time_to_discharged(void)
 
 bool CSegway_Battery::is_battery_present(void)
 {
-  return false;
+  if(this->info.status&0x01)
+    return true;
+  else
+    return false;
 }
 
 bool CSegway_Battery::is_charger_present(void)
 {
-  return false;
+  if(this->info.status&0x02)
+    return true;
+  else
+    return false;
 }
 
 bool CSegway_Battery::is_load_enabled(void)
 {
-  return false;
+  if(this->info.status&0x04)
+    return true;
+  else
+    return false;
 }
 
 bool CSegway_Battery::under_voltage_protection(void)
 {
-  return false;
+  if(this->info.status&0x08)
+    return true;
+  else
+    return false;
 }
 
 bool CSegway_Battery::over_voltage_protection(void)
 {
-  return false;
+  if(this->info.status&0x10)
+    return true;
+  else
+    return false;
 }
  
 CSegway_Battery::~CSegway_Battery()
diff --git a/src/segway_battery.h b/src/segway_battery.h
index 5d1b246..c82e0bc 100755
--- a/src/segway_battery.h
+++ b/src/segway_battery.h
@@ -1,7 +1,8 @@
 #ifndef _SEGWAY_BATTERY_H
 #define _SEGWAY_BATTERY_H
 
-#include "rs232.h"
+#include "ftdiserver.h"
+#include "ftdimodule.h"
 #include "threadserver.h"
 #include "eventserver.h"
 #include "mutex.h"
@@ -28,7 +29,7 @@ class CSegway_Battery
 {
   private:
     // serial device object
-    CRS232 *serial;
+    CFTDI *serial;
     // thread attributes
     CThreadServer *thread_server;
     std::string battery_monitor_thread_id;
-- 
GitLab