diff --git a/controller_teensy/src/main.cpp b/controller_teensy/src/main.cpp index 0368d5b..735c756 100644 --- a/controller_teensy/src/main.cpp +++ b/controller_teensy/src/main.cpp @@ -57,6 +57,11 @@ unsigned long brake_ok_time=0; bool error_throttle_outofrange=false; bool error_brake_outofrange=false; +#define NORMAL_MAX_ACCELERATION_RATE 10000 +#define SLOW_MAX_ACCELERATION_RATE 500 +int16_t max_acceleration_rate=NORMAL_MAX_ACCELERATION_RATE; //maximum cmd send increase per second + + float meanSpeedms=0; float trip=0; //trip distance in meters float wheelcircumference=0.5278; //wheel diameter in m. 8.4cm radius -> 0.084m*2*Pi @@ -66,8 +71,8 @@ float currentConsumed=0; //Ah //Driving parameters int16_t minimum_constant_cmd_reduce=1; //reduce cmd every loop by this constant amount when freewheeling/braking -int16_t brake_cmdreduce_proportional=200; //cmd gets reduced by an amount proportional to brake position (ignores freewheeling). cmd_new-=brake_cmdreduce_proportional / second @ full brake. with BREAK_CMDREDUCE_CONSTANT=1000 car would stop with full brake at least after a second (ignoring influence of brake current control/freewheeling) -float startbrakecurrent=2; //Ampere. "targeted brake current @full brake". at what point to start apply brake proportional to brake_pos. for everything above that cmd is reduced by freewheel_break_factor +int16_t brake_cmdreduce_proportional=500; //cmd gets reduced by an amount proportional to brake position (ignores freewheeling). cmd_new-=brake_cmdreduce_proportional / second @ full brake. with BREAK_CMDREDUCE_CONSTANT=1000 car would stop with full brake at least after a second (ignoring influence of brake current control/freewheeling) +float startbrakecurrent=3; //Ampere. "targeted brake current @full brake". at what point to start apply brake proportional to brake_pos. for everything above that cmd is reduced by freewheel_break_factor float startbrakecurrent_offset=0.1; //offset start point for breaking, because of reading fluctuations around 0A. set this slightly above idle current reading @@ -366,7 +371,6 @@ void writeLogHeader(HardwareSerial &SerialRef) { SerialRef.print("rpm_FrontL,rpm_FrontR,rpm_RearL,rpm_RearR,"); SerialRef.print("temp_Front,temp_Rear,vbat_Front,vbat_Rear,"); SerialRef.println("currentAll,throttle,brake,speed,trip,currentConsumed"); - SerialRef.println("Reduced ducking. More phase advance"); } void writeLog(HardwareSerial &SerialRef, unsigned long time, MotorParameter &mpfront, MotorParameter &mprear, SerialFeedback &fbfront, SerialFeedback &fbrear, float currentAll, int16_t throttle, int16_t brake) @@ -429,6 +433,7 @@ void readADC() { if (digitalRead(PIN_MODE_SWITCH)) { //pushed in, also high if cable got disconnected if (speedmode!=SPEEDMODE_SLOW) { speedmode=SPEEDMODE_SLOW; + max_acceleration_rate=SLOW_MAX_ACCELERATION_RATE; if (loopmillis>WRITE_HEADER_TIME) { writeLogComment(Serial1,loopmillis, "Mode switched to SPEEDMODE_SLOW"); } @@ -436,6 +441,7 @@ void readADC() { }else{ //button not pushed in if (speedmode!=SPEEDMODE_NORMAL) { speedmode=SPEEDMODE_NORMAL; + max_acceleration_rate=NORMAL_MAX_ACCELERATION_RATE; if (loopmillis>WRITE_HEADER_TIME) { writeLogComment(Serial1,loopmillis, "Mode switched to SPEEDMODE_NORMAL"); } @@ -527,7 +533,7 @@ void sendCMD() { filtered_currentAll=max(filtered_currentFront,filtered_currentRear); //positive value is current Drawn from battery. negative value is braking current if (throttle_pos>=last_cmd_send) { //accelerating - cmd_send = throttle_pos; //if throttle higher than last applied value, apply throttle directly + cmd_send += constrain(throttle_pos-cmd_send,0,max_acceleration_rate*SENDPERIOD/1000); //if throttle higher than last applied value, apply throttle directly }else{ //freewheeling or braking if (filtered_currentAll>freewheel_current) { //drive current too high cmd_send-= max(0, (filtered_currentAll-freewheel_current)*freewheel_break_factor*(SENDPERIOD/1000.0)); //how much current over freewheel current, multiplied by factor. reduces cmd_send value @@ -544,7 +550,7 @@ void sendCMD() { last_cmd_send=cmd_send; - int16_t cmd_send_toMotor=constrain(cmd_send-(brake_pos*0.2),0,1000); + int16_t cmd_send_toMotor=constrain(cmd_send* (1.0-(brake_pos*0.5/1000.0) ) ,0,1000); //brake "ducking"