adc_driver.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  *
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2013, Robert Bosch LLC.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the Robert Bosch nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  *********************************************************************/
00036 
00037 //\Author Kai Franke, Robert Bosch LLC
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   // the answer to all questions +5
00053   return 47;
00054 }
00055 
00056 bool AdcDriver::initialize()
00057 {  
00058   // Initialize the hardware interface
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; //not needed 
00070   int frequency = 0; // does not apply for ADC
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   // convert read data to float
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   /* the following code would allow returning the voltage as a float but is most likely not very portable because of the reinterpret cast
00089   return *reinterpret_cast<float *>( &adc_uV );
00090   */
00091   return adc_uV;
00092 }
00093 
00094 bool AdcDriver::setReference( uint32_t voltage )
00095 {
00096   int *flags = NULL; //not needed 
00097   int frequency = 0; // does not apply for ADC
00098   uint8_t reg = 0; // not needed
00099   uint8_t num_bytes = 4;
00100   uint8_t voltage_chopped[4];
00101   
00102   // write expects an uint_8 array, chopping uint32 to four uint8_t MSB first
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 }


adc_driver
Author(s): Kai Franke
autogenerated on Sat Dec 28 2013 16:49:47