JMotor
Loading...
Searching...
No Matches
JMotorCompStandard.h
Go to the documentation of this file.
1#ifndef J_MOTOR_COMP_STANDARD_H
2#define J_MOTOR_COMP_STANDARD_H
3#include "JMotorCompensator.h"
5#include <Arduino.h>
37 unsigned int start_boost_time;
38
40 float _motor_stop_voltage, float _motor_stop_speed,
41 float _motor_start_voltage, float _motor_start_speed,
42 float _motor_high_voltage, float _motor_high_speed,
43 unsigned int _start_boost_time)
44 {
45 motor_stop_voltage = max(_motor_stop_voltage, (float)0.0);
46 motor_stop_speed = max(_motor_stop_speed, (float)0.0);
47 motor_start_voltage = max(_motor_start_voltage, (float)0.0);
48 motor_start_speed = max(_motor_start_speed, (float)0.0);
49 motor_high_voltage = max(_motor_high_voltage, (float)0.0);
50 motor_high_speed = max(_motor_high_speed, (float)0.0);
51 start_boost_time = _start_boost_time;
52 }
53};
58private:
59 JVoltageCompensator& voltComp;
61 bool startBoosting;
62 bool startBoostingArmed;
63 unsigned long startBoostingTimeMillis;
64 float multiplier;
65
66public:
73 JMotorCompStandard(JVoltageCompensator& _voltComp, JMotorCompStandardConfig _config, float _multiplier = 1.0)
74 : voltComp(_voltComp)
75 , config(_config)
76 {
77 startBoosting = false;
78 startBoostingArmed = true;
79 startBoostingTimeMillis = 0;
80 multiplier = _multiplier;
81 }
82 float compensate(float val)
83 {
84 float ret = val * multiplier;
85 if (ret == 0) {
86 startBoostingArmed = true;
87 return 0;
88 }
89
90 if (ret > 0) {
91 ret = calcSlope(ret);
92 ret = max(ret, (float)0);
93 } else {
94 ret = calcSlope(-ret);
95 ret = -max(ret, (float)0);
96 }
97
98 if (ret == 0) {
99
100 startBoostingArmed = true;
101 return 0;
102 }
103
104 if (!startBoosting && startBoostingArmed && abs(ret) <= config.motor_start_voltage) {
105 startBoosting = true;
106 startBoostingArmed = false;
107 startBoostingTimeMillis = millis();
108 }
109 if (startBoosting) {
110 if (millis() - startBoostingTimeMillis < config.start_boost_time) {
111 if (ret > 0) {
112 ret = config.motor_start_voltage;
113 } else {
114 ret = -config.motor_start_voltage;
115 }
116 } else {
117 startBoosting = false;
118 }
119 }
120 ret = voltComp.adjust(ret, driverRange);
121 return constrain(ret, -maxDriverRange, maxDriverRange);
122 }
124 {
125 config = _config;
126 }
127
128 float getMaxVel()
129 {
130 if (voltComp.getSupplyVoltage() * maxDriverRange < config.motor_stop_voltage) {
131 return 0;
132 } else if (voltComp.getSupplyVoltage() < config.motor_start_voltage) {
133 return floatMap(voltComp.getSupplyVoltage(), config.motor_stop_voltage, config.motor_start_voltage, config.motor_stop_speed, config.motor_start_speed) / multiplier;
134 } else {
135 return floatMap(voltComp.getSupplyVoltage(), config.motor_start_voltage, config.motor_high_voltage, config.motor_start_speed, config.motor_high_speed) / multiplier;
136 }
137 }
138
139 float getMinVel()
140 {
141 return config.motor_stop_speed / multiplier;
142 }
143
144 void setMultiplier(float _multiplier)
145 {
146 multiplier = _multiplier;
147 }
148
149private:
150 float calcSlope(float ret)
151 {
152 if (ret <= config.motor_stop_speed) {
153 ret = 0; // can't turn the motor this slowly
154 } else if (ret <= config.motor_start_speed) {
155 ret = floatMap(ret, config.motor_stop_speed, config.motor_start_speed, config.motor_stop_voltage, config.motor_start_voltage);
156 } else {
157 ret = floatMap(ret, config.motor_start_speed, config.motor_high_speed, config.motor_start_voltage, config.motor_high_voltage);
158 }
159 return ret;
160 }
161 float floatMap(float x, float in_min, float in_max, float out_min, float out_max)
162 {
163 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
164 }
165};
166#endif
Converts from speed to driver input compensating for motors not being perfect.
Definition JMotorCompStandard.h:57
void setMultiplier(float _multiplier)
Definition JMotorCompStandard.h:144
float compensate(float val)
calculate
Definition JMotorCompStandard.h:82
void setConfig(JMotorCompStandardConfig &_config)
Definition JMotorCompStandard.h:123
float getMaxVel()
How fast of a motor speed setting would get adjusted to full motor power.
Definition JMotorCompStandard.h:128
float getMinVel()
Slowest speed motor can go.
Definition JMotorCompStandard.h:139
JMotorCompStandard(JVoltageCompensator &_voltComp, JMotorCompStandardConfig _config, float _multiplier=1.0)
Converts from speed to driver input compensating for motors not being perfect.
Definition JMotorCompStandard.h:73
This class defines a common interface for converting from speed to driver input. It should compensate...
Definition JMotorCompensator.h:9
float maxDriverRange
Definition JMotorCompensator.h:12
float driverRange
Definition JMotorCompensator.h:11
convert voltage to value needed to set driver at to get that voltage
Definition JVoltageCompensator.h:7
virtual float getSupplyVoltage()
virtual float adjust(float voltage, float driverRange)
do conversion
struct for containing settings for JMotorCompStandard
Definition JMotorCompStandard.h:9
unsigned int start_boost_time
how long (in milliseconds) to pulse motor_start_voltage to get the motor started
Definition JMotorCompStandard.h:37
float motor_start_voltage
voltage at which the motor can start turning
Definition JMotorCompStandard.h:21
JMotorCompStandardConfig(float _motor_stop_voltage, float _motor_stop_speed, float _motor_start_voltage, float _motor_start_speed, float _motor_high_voltage, float _motor_high_speed, unsigned int _start_boost_time)
Definition JMotorCompStandard.h:39
float motor_start_speed
speed the motor turns at motor_start_voltage
Definition JMotorCompStandard.h:25
float motor_high_speed
speed the motor turns at motor_high_voltage
Definition JMotorCompStandard.h:33
float motor_stop_speed
speed the motor turns at motor_stop_voltage
Definition JMotorCompStandard.h:17
float motor_high_voltage
voltage used for calibration point towards fastest speed of motor
Definition JMotorCompStandard.h:29
float motor_stop_voltage
voltage at which the motor stops turning
Definition JMotorCompStandard.h:13