add slower rate for braking

This commit is contained in:
interfisch 2019-12-17 23:12:58 +01:00
parent 4c153f28e8
commit 901c46af34
2 changed files with 34 additions and 5 deletions

View File

@ -57,6 +57,7 @@ long loopmillis=0; //only use one millis reading each loop
long last_looptime=0; //for looptiming long last_looptime=0; //for looptiming
#define LOOPTIME 10 //how often the loop(s) should run #define LOOPTIME 10 //how often the loop(s) should run
long millis_lastchange=0; //for poweroff after some time with no movement long millis_lastchange=0; //for poweroff after some time with no movement
#define MAXBRAKERATE 7 //maximum rate for braking (loop timing)
String errormessage=""; //store some error message to print String errormessage=""; //store some error message to print
@ -66,7 +67,7 @@ long state_modechange_time=0;
long millis_lastadc=0; long millis_lastadc=0;
#define ADC_READTIME 10 //time interval to read adc (for filtering) #define ADC_READTIME 10 //time interval to read adc (for filtering)
#define ADC_THROTTLE_FILTER 0.05 //low value = slower change #define ADC_THROTTLE_FILTER 0.2 //low value = slower change
int adc_throttle_raw=0; //raw throttle value from adc int adc_throttle_raw=0; //raw throttle value from adc
float adc_throttle=0; //filtered value float adc_throttle=0; //filtered value
@ -449,6 +450,11 @@ void loop_idle() {
void loop_on() { void loop_on() {
int16_t speedvalue=constrain( map(adc_throttle, ADC_CALIB_THROTTLE_MIN, ADC_CALIB_THROTTLE_MAX, 0, 1000 ) ,0, 1000); int16_t speedvalue=constrain( map(adc_throttle, ADC_CALIB_THROTTLE_MIN, ADC_CALIB_THROTTLE_MAX, 0, 1000 ) ,0, 1000);
int16_t lastSpeed=(out_speedFL+out_speedFR+out_speedRL+out_speedRR)/4;
if (speedvalue<=lastSpeed) { //braking
speedvalue = (lastSpeed-speedvalue)>MAXBRAKERATE ? (lastSpeed-MAXBRAKERATE):speedvalue;
}
out_speedFL=speedvalue; out_speedFL=speedvalue;
out_speedFR=speedvalue; out_speedFR=speedvalue;

View File

@ -12,12 +12,17 @@ long lastReadADC=0;
long lastSend=0; long lastSend=0;
long lastHBLoop=0; long lastHBLoop=0;
int LOOPTIME_CONTROLLER=10; //controller looptime
long lastLoopController=0;
int ADC_CALIB_THROTTLE_MIN=2000; //controller int ADC_CALIB_THROTTLE_MIN=2000; //controller
int ADC_CALIB_THROTTLE_MAX=3120; //controller int ADC_CALIB_THROTTLE_MAX=3120; //controller
int ADC_READTIME=10; //controller. in ms int ADC_READTIME=10; //controller. in ms
int adc_throttle_raw=ADC_CALIB_THROTTLE_MIN; //controller. raw value read from adc (here slider) int adc_throttle_raw=ADC_CALIB_THROTTLE_MIN; //controller. raw value read from adc (here slider)
float adc_throttle=adc_throttle_raw; //controller. internally filtered value float adc_throttle=adc_throttle_raw; //controller. internally filtered value
float ADC_THROTTLE_FILTER=0.05; //controller float ADC_THROTTLE_FILTER=0.2; //controller
int speedvalue=0;
int MAXBRAKERATE=7; //Controller
int SENDPERIOD=50; //controller. in ms int SENDPERIOD=50; //controller. in ms
int sendSpeed=0; //transmittet speed value over usart int sendSpeed=0; //transmittet speed value over usart
@ -30,6 +35,7 @@ int outSpeed=0; //hoverboard. speedvalue for pwm. filtered speedRate
Slider SinputValue; Slider SinputValue;
Slider SfilteredInputValue; Slider SfilteredInputValue;
Slider SspeedValue;
Slider SspeedRate; Slider SspeedRate;
Slider SoutSpeed; Slider SoutSpeed;
@ -62,14 +68,20 @@ void setup() {
.setRange(ADC_CALIB_THROTTLE_MIN,ADC_CALIB_THROTTLE_MAX) .setRange(ADC_CALIB_THROTTLE_MIN,ADC_CALIB_THROTTLE_MAX)
; ;
SspeedRate=cp5.addSlider("speedRate") SspeedValue=cp5.addSlider("speedValue") //after brake slowing
.setPosition(10,50+30+10+20+10) .setPosition(10,50+30+10+20+10)
.setSize(500,20) .setSize(500,20)
.setRange(0,1000) .setRange(0,1000)
; ;
SspeedRate=cp5.addSlider("speedRate")
.setPosition(10,50+30+10+20+10+20+10)
.setSize(500,20)
.setRange(0,1000)
;
SoutSpeed=cp5.addSlider("outSpeed") SoutSpeed=cp5.addSlider("outSpeed")
.setPosition(1,50+30+10+20+10+20+10) .setPosition(1,50+30+10+20+10+20+10+20+10)
.setSize(500,30) .setSize(500,30)
.setRange(0,1000) .setRange(0,1000)
; ;
@ -152,9 +164,20 @@ void draw() {
adc_throttle = adc_throttle*(1-ADC_THROTTLE_FILTER) + adc_throttle_raw*ADC_THROTTLE_FILTER; //adc_throttle = adc_throttle*(1-ADC_THROTTLE_FILTER) + adc_throttle_raw*ADC_THROTTLE_FILTER; adc_throttle = adc_throttle*(1-ADC_THROTTLE_FILTER) + adc_throttle_raw*ADC_THROTTLE_FILTER; //adc_throttle = adc_throttle*(1-ADC_THROTTLE_FILTER) + adc_throttle_raw*ADC_THROTTLE_FILTER;
SfilteredInputValue.setValue(adc_throttle); SfilteredInputValue.setValue(adc_throttle);
} }
if (loopmillis-lastLoopController>LOOPTIME_CONTROLLER){ //frequency of controller LOOPTIME
lastLoopController=millis();
int lastSpeed=speedvalue;
speedvalue=int(constrain( map(adc_throttle, ADC_CALIB_THROTTLE_MIN, ADC_CALIB_THROTTLE_MAX, 0, 1000 ) ,0, 1000));
if (speedvalue<=lastSpeed) { //braking
speedvalue = (lastSpeed-speedvalue)>MAXBRAKERATE ? (lastSpeed-MAXBRAKERATE):speedvalue;
}
SspeedValue.setValue(speedvalue);
}
if (loopmillis-lastSend>SENDPERIOD){ //Frequency of transmitting new values to the hoverboard if (loopmillis-lastSend>SENDPERIOD){ //Frequency of transmitting new values to the hoverboard
lastSend=millis(); lastSend=millis();
sendSpeed=int(constrain( map(adc_throttle, ADC_CALIB_THROTTLE_MIN, ADC_CALIB_THROTTLE_MAX, 0, 1000 ) ,0, 1000)); sendSpeed=speedvalue;
} }
if (loopmillis-lastHBLoop>LOOPTIME){ //frequency of hoverboard mainboard (looptime) if (loopmillis-lastHBLoop>LOOPTIME){ //frequency of hoverboard mainboard (looptime)