Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "adc_driver/adc_driver.h"
00040
00041 AdcDriver::AdcDriver( bosch_hardware_interface* hw, uint8_t adc_pin ): sensor_driver( hw )
00042 {
00043 _pin = adc_pin;
00044 }
00045
00046 AdcDriver::~AdcDriver()
00047 {
00048 }
00049
00050 uint8_t AdcDriver::getDeviceAddress()
00051 {
00052
00053 return 47;
00054 }
00055
00056 bool AdcDriver::initialize()
00057 {
00058
00059 if( hardware_->initialize() == false )
00060 {
00061 ROS_ERROR("AdcDriver::initialize(): Could not initialize a hardware interface!");
00062 return false;
00063 }
00064 return true;
00065 }
00066
00067 uint32_t AdcDriver::read( )
00068 {
00069 int *flags = NULL;
00070 int frequency = 0;
00071 uint8_t num_bytes = 4;
00072 uint8_t data[4];
00073
00074 if( hardware_->read( this->getDeviceAddress(), ADCONVERTER, frequency, flags, _pin, data, num_bytes ) < 0 )
00075 {
00076 ROS_ERROR("AdcDriver::read(): could not read input");
00077 return 0;
00078 }
00079
00080 uint32_t temp[4];
00081 uint32_t adc_uV;
00082 temp[0] = data[0];
00083 temp[1] = data[1];
00084 temp[2] = data[2];
00085 temp[3] = data[3];
00086 adc_uV = (temp[0] << 24) + (temp[1] << 16) + (temp[2] << 8) + temp[3] ;
00087
00088
00089
00090
00091 return adc_uV;
00092 }
00093
00094 bool AdcDriver::setReference( uint32_t voltage )
00095 {
00096 int *flags = NULL;
00097 int frequency = 0;
00098 uint8_t reg = 0;
00099 uint8_t num_bytes = 4;
00100 uint8_t voltage_chopped[4];
00101
00102
00103 uint32_t temp;
00104 temp = (voltage & (0xFF << 24)) >> 24;
00105 voltage_chopped[0] = (uint8_t)temp;
00106 temp = (voltage & (0xFF << 16)) >> 16;
00107 voltage_chopped[1] = (uint8_t)temp;
00108 temp = (voltage & (0xFF << 8)) >> 8;
00109 voltage_chopped[2] = (uint8_t)temp;
00110 temp = (voltage & (0xFF << 0));
00111 voltage_chopped[3] = (uint8_t)temp;
00112
00113 if( hardware_->write( this->getDeviceAddress(), ADCONVERTER, frequency, flags, reg, voltage_chopped, num_bytes ) < 0 )
00114 {
00115 ROS_ERROR("AdcDriver::setReference(): could not write reference voltage");
00116 return false;
00117 }
00118 return true;
00119 }