pedal_lut.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2015-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_MKZ_CAN_PEDAL_LUT_H
36 #define _DBW_MKZ_CAN_PEDAL_LUT_H
37 #include <math.h>
38 
39 namespace dbw_mkz_can
40 {
41 
42 static const struct {float pedal; float torque;} BRAKE_TABLE[] = {
43 // Duty, Nm
44  {0.150, 0},
45  {0.175, 0},
46  {0.184, 4},
47  {0.208, 108},
48  {0.211, 519},
49  {0.234, 521},
50  {0.246, 816},
51  {0.283, 1832},
52  {0.305, 2612},
53  {0.323, 3316},
54  {0.326, 3412},
55  {0.330, 3412},
56 };
57 static const struct {float pedal; float percent;} THROTTLE_TABLE[] = {
58 // Duty, %
59  {0.150, 0.000},
60  {0.165, 0.001},
61  {0.166, 0.020},
62  {0.800, 1.000},
63 };
64 static inline float brakeTorqueFromPedal(float pedal) {
65  const unsigned int size = sizeof(BRAKE_TABLE) / sizeof(BRAKE_TABLE[0]);
66  if (pedal <= BRAKE_TABLE[0].pedal) {
67  return BRAKE_TABLE[0].torque;
68  } else if (pedal >= BRAKE_TABLE[size - 1].pedal) {
69  return BRAKE_TABLE[size - 1].torque;
70  } else {
71  for (unsigned int i = 1; i < size; i++) {
72  if (pedal < BRAKE_TABLE[i].pedal) {
73  float start = BRAKE_TABLE[i - 1].torque;
74  float dinput = pedal - BRAKE_TABLE[i - 1].pedal;
75  float dtorque = BRAKE_TABLE[i].torque - BRAKE_TABLE[i - 1].torque;
76  float dpedal = BRAKE_TABLE[i].pedal - BRAKE_TABLE[i - 1].pedal;
77  if (fabsf(dpedal) > (float)1e-6) {
78  return start + (dinput * dtorque / dpedal);
79  } else {
80  return start + (dtorque / 2);
81  }
82  }
83  }
84  }
85  return 0.0;
86 }
87 static inline float brakePedalFromTorque(float torque) {
88  const unsigned int size = sizeof(BRAKE_TABLE) / sizeof(BRAKE_TABLE[0]);
89  if (torque <= BRAKE_TABLE[0].torque) {
90  return BRAKE_TABLE[0].pedal;
91  } else if (torque >= BRAKE_TABLE[size - 1].torque) {
92  return BRAKE_TABLE[size - 1].pedal;
93  } else {
94  for (unsigned int i = 1; i < size; i++) {
95  if (torque < BRAKE_TABLE[i].torque) {
96  float start = BRAKE_TABLE[i - 1].pedal;
97  float dinput = torque - BRAKE_TABLE[i - 1].torque;
98  float dpedal = BRAKE_TABLE[i].pedal - BRAKE_TABLE[i - 1].pedal;
99  float dtorque = BRAKE_TABLE[i].torque - BRAKE_TABLE[i - 1].torque;
100  if (fabsf(dtorque) > (float)1e-6) {
101  return start + (dinput * dpedal / dtorque);
102  } else {
103  return start + (dpedal / 2);
104  }
105  }
106  }
107  }
108  return 0.0;
109 }
110 static inline float brakePedalFromPercent(float percent) {
111  return brakePedalFromTorque(percent * BRAKE_TABLE[sizeof(BRAKE_TABLE) / sizeof(BRAKE_TABLE[0]) - 1].torque);
112 }
113 static inline float throttlePedalFromPercent(float percent) {
114  const unsigned int size = sizeof(THROTTLE_TABLE) / sizeof(THROTTLE_TABLE[0]);
115  if (percent <= THROTTLE_TABLE[0].percent) {
116  return THROTTLE_TABLE[0].pedal;
117  } else if (percent >= THROTTLE_TABLE[size - 1].percent) {
118  return THROTTLE_TABLE[size - 1].pedal;
119  } else {
120  for (unsigned int i = 1; i < size; i++) {
121  if (percent < THROTTLE_TABLE[i].percent) {
122  float start = THROTTLE_TABLE[i - 1].pedal;
123  float dinput = percent - THROTTLE_TABLE[i - 1].percent;
124  float dpedal = THROTTLE_TABLE[i].pedal - THROTTLE_TABLE[i - 1].pedal;
125  float dpercent = THROTTLE_TABLE[i].percent - THROTTLE_TABLE[i - 1].percent;
126  if (fabsf(dpercent) > (float)1e-6) {
127  return start + (dinput * dpedal / dpercent);
128  } else {
129  return start + (dpedal / 2);
130  }
131  }
132  }
133  }
134  return 0.0;
135 }
136 static inline float throttlePercentFromPedal(float pedal) {
137  const unsigned int size = sizeof(THROTTLE_TABLE) / sizeof(THROTTLE_TABLE[0]);
138  if (pedal <= THROTTLE_TABLE[0].pedal) {
139  return THROTTLE_TABLE[0].percent;
140  } else if (pedal >= THROTTLE_TABLE[size - 1].pedal) {
141  return THROTTLE_TABLE[size - 1].percent;
142  } else {
143  for (unsigned int i = 1; i < size; i++) {
144  if (pedal < THROTTLE_TABLE[i].pedal) {
145  float start = THROTTLE_TABLE[i - 1].percent;
146  float dinput = pedal - THROTTLE_TABLE[i - 1].pedal;
147  float dpercent = THROTTLE_TABLE[i].percent - THROTTLE_TABLE[i - 1].percent;
148  float dpedal = THROTTLE_TABLE[i].pedal - THROTTLE_TABLE[i - 1].pedal;
149  if (fabsf(dpedal) > (float) 1e-6) {
150  return start + (dinput * dpercent / dpedal);
151  } else {
152  return start + (dpercent / 2);
153  }
154  }
155  }
156  }
157  return 0.0;
158 }
159 
160 } // namespace dbw_mkz_can
161 
162 #endif // _DBW_MKZ_CAN_PEDAL_LUT_H
163 
static float brakeTorqueFromPedal(float pedal)
Definition: pedal_lut.h:64
static float brakePedalFromPercent(float percent)
Definition: pedal_lut.h:110
float percent
Definition: pedal_lut.h:57
float torque
Definition: pedal_lut.h:42
static float throttlePercentFromPedal(float pedal)
Definition: pedal_lut.h:136
static const struct dbw_mkz_can::@16 BRAKE_TABLE[]
float pedal
Definition: pedal_lut.h:42
static float throttlePedalFromPercent(float percent)
Definition: pedal_lut.h:113
static float brakePedalFromTorque(float torque)
Definition: pedal_lut.h:87
static const struct dbw_mkz_can::@17 THROTTLE_TABLE[]


dbw_mkz_can
Author(s): Kevin Hallenbeck
autogenerated on Fri May 14 2021 02:47:08