pwm_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 "pwm_driver/pwm_driver.h"
00040 
00041 PwmDriver::PwmDriver( bosch_hardware_interface* hw, uint32_t frequency, uint8_t pin ): sensor_driver( hw )
00042 {
00043   _frequency = frequency;
00044   _pin = pin;
00045 }
00046 
00047 PwmDriver::~PwmDriver()
00048 {
00049 }
00050 
00051 uint8_t PwmDriver::getDeviceAddress()
00052 {
00053   // the answer to all questions
00054   return 42;
00055 }
00056 
00057 bool PwmDriver::initialize()
00058 {  
00059   // Initialize the hardware interface
00060   if( hardware_->initialize() == false )
00061   {
00062     ROS_ERROR("PwmDriver::initialize(): Could not initialize a hardware interface!");
00063     return false;
00064   }
00065   return true;
00066 }
00067 
00068 bool PwmDriver::set( float value )
00069 {
00070   int *flags = NULL; // not needed for PWM
00071   uint8_t num_bytes = 4;  // only set to make it look nice, not really needed
00072   uint8_t duty_cycle_chopped[4];
00073   uint32_t duty_cycle;
00074   
00075   if( value < 0.0 || value > 1.0 )
00076   {
00077     ROS_ERROR("PWM duty cycle must be between 0 and 1");
00078     return false;
00079   }
00080   
00081   // convert float to uint32
00082   double convertion_helper = value;
00083   duty_cycle = (uint32_t)(convertion_helper * 0xFFFFFFFF);
00084   // write expects an uint_8 array, chopping uint32 to four uint8_t MSB first
00085   uint32_t temp;
00086   temp = (duty_cycle & (0xFF << 24)) >> 24;
00087   duty_cycle_chopped[0] = (uint8_t)temp;
00088   temp = (duty_cycle & (0xFF << 16)) >> 16;
00089   duty_cycle_chopped[1] = (uint8_t)temp;
00090   temp = (duty_cycle & (0xFF << 8)) >> 8;
00091   duty_cycle_chopped[2] = (uint8_t)temp;
00092   temp = (duty_cycle & (0xFF << 0));
00093   duty_cycle_chopped[3] = (uint8_t)temp;
00094   
00095   //std::cout << "duty_cycle: " << duty_cycle << "  chopped: " << duty_cycle_chopped[0] + 0 << " " << duty_cycle_chopped[1] + 0 << " " << duty_cycle_chopped[2] + 0 << " " << duty_cycle_chopped[3] + 0 << " " << std::endl;
00096   
00097   if( hardware_->write( this->getDeviceAddress(), PWM, _frequency, flags, _pin, duty_cycle_chopped, num_bytes ) < 0 )
00098   {
00099     ROS_ERROR("PwmDriver::setPWM(): could not write PWM to serial device.");
00100     return false;
00101   } 
00102   return true;
00103 }


pwm_driver
Author(s): Kai Franke
autogenerated on Sat Dec 28 2013 16:49:44