pedal_lut.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2018-2019, Dataspeed Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Dataspeed Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 #ifndef _DBW_FCA_CAN_PEDAL_LUT_H
36 #define _DBW_FCA_CAN_PEDAL_LUT_H
37 #include <math.h>
38 
39 namespace dbw_fca_can
40 {
41 
42 static const struct {float pedal; float torque;} BRAKE_TABLE[] = {
43 // Duty, Nm
44  {0.150, 0},
45  {0.166, 0},
46  {0.168, 4},
47  {0.200, 56},
48  {0.225, 194},
49  {0.250, 456},
50  {0.300, 1312},
51  {0.350, 2352},
52  {0.400, 3716},
53  {0.434, 4740},
54  {0.566, 6888},
55  {0.600, 6888},
56 };
57 static const struct {float pedal; float percent;} THROTTLE_TABLE[] = {
58 // Duty, %
59  {0.080, 0.000},
60  {0.114, 0.001},
61  {0.497, 0.500},
62  {0.890, 0.998},
63  {0.892, 1.000},
64 };
65 static inline float brakeTorqueFromPedal(float pedal) {
66  const unsigned int size = sizeof(BRAKE_TABLE) / sizeof(BRAKE_TABLE[0]);
67  if (pedal <= BRAKE_TABLE[0].pedal) {
68  return BRAKE_TABLE[0].torque;
69  } else if (pedal >= BRAKE_TABLE[size - 1].pedal) {
70  return BRAKE_TABLE[size - 1].torque;
71  } else {
72  for (unsigned int i = 1; i < size; i++) {
73  if (pedal < BRAKE_TABLE[i].pedal) {
74  float start = BRAKE_TABLE[i - 1].torque;
75  float dinput = pedal - BRAKE_TABLE[i - 1].pedal;
76  float dtorque = BRAKE_TABLE[i].torque - BRAKE_TABLE[i - 1].torque;
77  float dpedal = BRAKE_TABLE[i].pedal - BRAKE_TABLE[i - 1].pedal;
78  if (fabsf(dpedal) > (float)1e-6) {
79  return start + (dinput * dtorque / dpedal);
80  } else {
81  return start + (dtorque / 2);
82  }
83  }
84  }
85  }
86  return 0.0;
87 }
88 static inline float brakePedalFromTorque(float torque) {
89  const unsigned int size = sizeof(BRAKE_TABLE) / sizeof(BRAKE_TABLE[0]);
90  if (torque <= BRAKE_TABLE[0].torque) {
91  return BRAKE_TABLE[0].pedal;
92  } else if (torque >= BRAKE_TABLE[size - 1].torque) {
93  return BRAKE_TABLE[size - 1].pedal;
94  } else {
95  for (unsigned int i = 1; i < size; i++) {
96  if (torque < BRAKE_TABLE[i].torque) {
97  float start = BRAKE_TABLE[i - 1].pedal;
98  float dinput = torque - BRAKE_TABLE[i - 1].torque;
99  float dpedal = BRAKE_TABLE[i].pedal - BRAKE_TABLE[i - 1].pedal;
100  float dtorque = BRAKE_TABLE[i].torque - BRAKE_TABLE[i - 1].torque;
101  if (fabsf(dtorque) > (float)1e-6) {
102  return start + (dinput * dpedal / dtorque);
103  } else {
104  return start + (dpedal / 2);
105  }
106  }
107  }
108  }
109  return 0.0;
110 }
111 static inline float brakePedalFromPercent(float percent) {
112  return brakePedalFromTorque(percent * 5000.0);
113  return brakePedalFromTorque(percent * BRAKE_TABLE[sizeof(BRAKE_TABLE) / sizeof(BRAKE_TABLE[0]) - 1].torque);
114 }
115 static inline float throttlePedalFromPercent(float percent) {
116  const unsigned int size = sizeof(THROTTLE_TABLE) / sizeof(THROTTLE_TABLE[0]);
117  if (percent <= THROTTLE_TABLE[0].percent) {
118  return THROTTLE_TABLE[0].pedal;
119  } else if (percent >= THROTTLE_TABLE[size - 1].percent) {
120  return THROTTLE_TABLE[size - 1].pedal;
121  } else {
122  for (unsigned int i = 1; i < size; i++) {
123  if (percent < THROTTLE_TABLE[i].percent) {
124  float start = THROTTLE_TABLE[i - 1].pedal;
125  float dinput = percent - THROTTLE_TABLE[i - 1].percent;
126  float dpedal = THROTTLE_TABLE[i].pedal - THROTTLE_TABLE[i - 1].pedal;
127  float dpercent = THROTTLE_TABLE[i].percent - THROTTLE_TABLE[i - 1].percent;
128  if (fabsf(dpercent) > (float)1e-6) {
129  return start + (dinput * dpedal / dpercent);
130  } else {
131  return start + (dpedal / 2);
132  }
133  }
134  }
135  }
136  return 0.0;
137 }
138 
139 static inline float throttlePercentFromPedal(float pedal) {
140  const unsigned int size = sizeof(THROTTLE_TABLE) / sizeof(THROTTLE_TABLE[0]);
141  if (pedal <= THROTTLE_TABLE[0].pedal) {
142  return THROTTLE_TABLE[0].percent;
143  } else if (pedal >= THROTTLE_TABLE[size - 1].pedal) {
144  return THROTTLE_TABLE[size - 1].percent;
145  } else {
146  for (unsigned int i = 1; i < size; i++) {
147  if (pedal < THROTTLE_TABLE[i].pedal) {
148  float start = THROTTLE_TABLE[i - 1].percent;
149  float dinput = pedal - THROTTLE_TABLE[i - 1].pedal;
150  float dpercent = THROTTLE_TABLE[i].percent - THROTTLE_TABLE[i - 1].percent;
151  float dpedal = THROTTLE_TABLE[i].pedal - THROTTLE_TABLE[i - 1].pedal;
152  if (fabsf(dpedal) > (float) 1e-6) {
153  return start + (dinput * dpercent / dpedal);
154  } else {
155  return start + (dpercent / 2);
156  }
157  }
158  }
159  }
160  return 0.0;
161 }
162 
163 } // namespace dbw_fca_can
164 
165 #endif // _DBW_FCA_CAN_PEDAL_LUT_H
166 
float torque
Definition: pedal_lut.h:42
static float brakePedalFromPercent(float percent)
Definition: pedal_lut.h:111
static const struct dbw_fca_can::@17 THROTTLE_TABLE[]
static float brakeTorqueFromPedal(float pedal)
Definition: pedal_lut.h:65
float pedal
Definition: pedal_lut.h:42
float percent
Definition: pedal_lut.h:57
static float brakePedalFromTorque(float torque)
Definition: pedal_lut.h:88
static const struct dbw_fca_can::@16 BRAKE_TABLE[]
static float throttlePedalFromPercent(float percent)
Definition: pedal_lut.h:115
static float throttlePercentFromPedal(float pedal)
Definition: pedal_lut.h:139


dbw_fca_can
Author(s): Kevin Hallenbeck
autogenerated on Wed May 12 2021 02:14:05