pedal_lut.h
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2018-2019, Dataspeed Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Dataspeed Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 #ifndef _DBW_FCA_CAN_PEDAL_LUT_H
00036 #define _DBW_FCA_CAN_PEDAL_LUT_H
00037 #include <math.h>
00038 
00039 namespace dbw_fca_can
00040 {
00041 
00042 static const struct {float pedal; float torque;} BRAKE_TABLE[] = {
00043 // Duty,   Nm
00044  {0.150,    0},
00045  {0.166,    0},
00046  {0.168,    4},
00047  {0.200,   56},
00048  {0.225,  194},
00049  {0.250,  456},
00050  {0.300, 1312},
00051  {0.350, 2352},
00052  {0.400, 3716},
00053  {0.434, 4740},
00054  {0.566, 6888},
00055  {0.600, 6888},
00056 };
00057 static const struct {float pedal; float percent;} THROTTLE_TABLE[] = {
00058 // Duty,   %
00059  {0.080, 0.000},
00060  {0.114, 0.001},
00061  {0.497, 0.500},
00062  {0.890, 0.998},
00063  {0.892, 1.000},
00064 };
00065 static inline float brakeTorqueFromPedal(float pedal) {
00066   const unsigned int size = sizeof(BRAKE_TABLE) / sizeof(BRAKE_TABLE[0]);
00067   if (pedal <= BRAKE_TABLE[0].pedal) {
00068     return BRAKE_TABLE[0].torque;
00069   } else if (pedal >= BRAKE_TABLE[size - 1].pedal) {
00070     return BRAKE_TABLE[size - 1].torque;
00071   } else {
00072     for (unsigned int i = 1; i < size; i++) {
00073       if (pedal < BRAKE_TABLE[i].pedal) {
00074         float start = BRAKE_TABLE[i - 1].torque;
00075         float dinput = pedal - BRAKE_TABLE[i - 1].pedal;
00076         float dtorque = BRAKE_TABLE[i].torque - BRAKE_TABLE[i - 1].torque;
00077         float dpedal = BRAKE_TABLE[i].pedal - BRAKE_TABLE[i - 1].pedal;
00078         if (fabsf(dpedal) > (float)1e-6) {
00079           return start + (dinput * dtorque / dpedal);
00080         } else {
00081           return start + (dtorque / 2);
00082         }
00083       }
00084     }
00085   }
00086   return 0.0;
00087 }
00088 static inline float brakePedalFromTorque(float torque) {
00089   const unsigned int size = sizeof(BRAKE_TABLE) / sizeof(BRAKE_TABLE[0]);
00090   if (torque <= BRAKE_TABLE[0].torque) {
00091     return BRAKE_TABLE[0].pedal;
00092   } else if (torque >= BRAKE_TABLE[size - 1].torque) {
00093     return BRAKE_TABLE[size - 1].pedal;
00094   } else {
00095     for (unsigned int i = 1; i < size; i++) {
00096       if (torque < BRAKE_TABLE[i].torque) {
00097         float start = BRAKE_TABLE[i - 1].pedal;
00098         float dinput = torque - BRAKE_TABLE[i - 1].torque;
00099         float dpedal = BRAKE_TABLE[i].pedal - BRAKE_TABLE[i - 1].pedal;
00100         float dtorque = BRAKE_TABLE[i].torque - BRAKE_TABLE[i - 1].torque;
00101         if (fabsf(dtorque) > (float)1e-6) {
00102           return start + (dinput * dpedal / dtorque);
00103         } else {
00104           return start + (dpedal / 2);
00105         }
00106       }
00107     }
00108   }
00109   return 0.0;
00110 }
00111 static inline float brakePedalFromPercent(float percent) {
00112   return brakePedalFromTorque(percent * 5000.0);
00113   return brakePedalFromTorque(percent * BRAKE_TABLE[sizeof(BRAKE_TABLE) / sizeof(BRAKE_TABLE[0]) - 1].torque);
00114 }
00115 static inline float throttlePedalFromPercent(float percent) {
00116   const unsigned int size = sizeof(THROTTLE_TABLE) / sizeof(THROTTLE_TABLE[0]);
00117   if (percent <= THROTTLE_TABLE[0].percent) {
00118     return THROTTLE_TABLE[0].pedal;
00119   } else if (percent >= THROTTLE_TABLE[size - 1].percent) {
00120     return THROTTLE_TABLE[size - 1].pedal;
00121   } else {
00122     for (unsigned int i = 1; i < size; i++) {
00123       if (percent < THROTTLE_TABLE[i].percent) {
00124         float start = THROTTLE_TABLE[i - 1].pedal;
00125         float dinput = percent - THROTTLE_TABLE[i - 1].percent;
00126         float dpedal = THROTTLE_TABLE[i].pedal - THROTTLE_TABLE[i - 1].pedal;
00127         float dpercent = THROTTLE_TABLE[i].percent - THROTTLE_TABLE[i - 1].percent;
00128         if (fabsf(dpercent) > (float)1e-6) {
00129           return start + (dinput * dpedal / dpercent);
00130         } else {
00131           return start + (dpedal / 2);
00132         }
00133       }
00134     }
00135   }
00136   return 0.0;
00137 }
00138 
00139 static inline float throttlePercentFromPedal(float pedal) {
00140   const unsigned int size = sizeof(THROTTLE_TABLE) / sizeof(THROTTLE_TABLE[0]);
00141   if (pedal <= THROTTLE_TABLE[0].pedal) {
00142     return THROTTLE_TABLE[0].percent;
00143   } else if (pedal >= THROTTLE_TABLE[size - 1].pedal) {
00144     return THROTTLE_TABLE[size - 1].percent;
00145   } else {
00146     for (unsigned int i = 1; i < size; i++) {
00147       if (pedal < THROTTLE_TABLE[i].pedal) {
00148         float start = THROTTLE_TABLE[i - 1].percent;
00149         float dinput = pedal - THROTTLE_TABLE[i - 1].pedal;
00150         float dpercent = THROTTLE_TABLE[i].percent - THROTTLE_TABLE[i - 1].percent;
00151         float dpedal = THROTTLE_TABLE[i].pedal - THROTTLE_TABLE[i - 1].pedal;
00152         if (fabsf(dpedal) > (float) 1e-6) {
00153           return start + (dinput * dpercent / dpedal);
00154         } else {
00155           return start + (dpercent / 2);
00156         }
00157       }
00158     }
00159   }
00160   return 0.0;
00161 }
00162 
00163 } // namespace dbw_fca_can
00164 
00165 #endif // _DBW_FCA_CAN_PEDAL_LUT_H
00166 


dbw_fca_can
Author(s): Kevin Hallenbeck
autogenerated on Sat May 4 2019 02:40:31