JMotor
Loading...
Searching...
No Matches
JDeadzoneRemover.h
Go to the documentation of this file.
1#ifndef J_DEADZONE_REMOVER_H
2#define J_DEADZONE_REMOVER_H
3#include "JTwoDTransform.h"
4#include <Arduino.h>
5namespace JDeadzoneRemover {
6
20inline float calculate(float value, float start, float max, float deadzone = 0)
21{
22 value = constrain(value, -1.0, 1.0);
23 if (abs(value) <= abs(deadzone)) {
24 return 0;
25 }
26 if (value > deadzone) {
27 // return floatMap(value, deadzone, 1.0, start, max);
28 return (value - deadzone) * (max - start) / (1.0 - deadzone) + start;
29 }
30 if (value < -deadzone) {
31 // return floatMap(value, -deadzone, -1.0, -start, -max);
32 return (value + deadzone) * (-max + start) / (-1.0 + deadzone) - start;
33 }
34 return 0;
35}
36
50inline JTwoDTransform calculate(JTwoDTransform value, JTwoDTransform start, JTwoDTransform max, JTwoDTransform deadzone = { 0, 0, 0 })
51{
52 JTwoDTransform output = { 0, 0, 0 };
53 for (int i = 0; i < 3; i++) {
54 float currentValue = constrain(value.getByNumber(i), -1.0, 1.0);
55 float currentDeadzone = deadzone.getByNumber(i);
56 float currentStart = start.getByNumber(i);
57 float currentMax = max.getByNumber(i);
58 float ret = 0;
59 if (abs(currentValue) <= abs(currentDeadzone)) {
60 ret = 0;
61 }
62 if (currentValue > currentDeadzone) {
63 // return floatMap(value, deadzone, 1.0, start, max);
64 ret = (currentValue - currentDeadzone) * (currentMax - currentStart) / (1.0 - currentDeadzone) + currentStart;
65 }
66 if (currentValue < -currentDeadzone) {
67 // return floatMap(value, -deadzone, -1.0, -start, -max);
68 ret = (currentValue + currentDeadzone) * (-currentMax + currentStart) / (-1.0 + currentDeadzone) - currentStart;
69 }
70 output.setByNumber(i, ret);
71 }
72 return output;
73}
74
75}; // end of namespace
76#endif
Definition JDeadzoneRemover.h:5
float calculate(float value, float start, float max, float deadzone=0)
If a motor's minimum speed is high, you can use this function to reduce the deadzone where nothing mo...
Definition JDeadzoneRemover.h:20
struct for storing a 2 dimensional transformation. Used for telling a drivetrain how to move.
Definition JTwoDTransform.h:9
float getByNumber(int8_t index)
access the three values based on a numerical index
Definition JTwoDTransform.h:166
void setByNumber(int8_t index, float value)
sets one of the three values based on a numerical index
Definition JTwoDTransform.h:181