Fix Bug brake not working at all
BRAKE state was not possible with previous state now IDLE is not forced when duty 0 Also adjust some logging
This commit is contained in:
		
							parent
							
								
									3014808d5a
								
							
						
					
					
						commit
						8c7f05134c
					
				@ -95,8 +95,8 @@ void setLoglevels(void){
 | 
			
		||||
	//--- set loglevel for individual tags ---
 | 
			
		||||
	esp_log_level_set("main", ESP_LOG_INFO);
 | 
			
		||||
	esp_log_level_set("buzzer", ESP_LOG_ERROR);
 | 
			
		||||
	esp_log_level_set("motordriver", ESP_LOG_DEBUG);
 | 
			
		||||
	//esp_log_level_set("motor-control", ESP_LOG_DEBUG);
 | 
			
		||||
	esp_log_level_set("motordriver", ESP_LOG_VERBOSE);
 | 
			
		||||
	esp_log_level_set("motor-control", ESP_LOG_INFO);
 | 
			
		||||
	//esp_log_level_set("evaluatedJoystick", ESP_LOG_DEBUG);
 | 
			
		||||
	//esp_log_level_set("joystickCommands", ESP_LOG_DEBUG);
 | 
			
		||||
	esp_log_level_set("button", ESP_LOG_INFO);
 | 
			
		||||
 | 
			
		||||
@ -5,6 +5,7 @@
 | 
			
		||||
//tag for logging
 | 
			
		||||
static const char * TAG = "motor-control";
 | 
			
		||||
 | 
			
		||||
#define TIMEOUT_IDLE_WHEN_NO_COMMAND 8000
 | 
			
		||||
 | 
			
		||||
//=============================
 | 
			
		||||
//======== constructor ========
 | 
			
		||||
@ -79,7 +80,7 @@ void controlledMotor::handle(){
 | 
			
		||||
    //--- receive commands from queue ---
 | 
			
		||||
    if( xQueueReceive( commandQueue, &commandReceive, ( TickType_t ) 0 ) )
 | 
			
		||||
    {
 | 
			
		||||
        ESP_LOGD(TAG, "Read command from queue: state=%s, duty=%.2f", motorstateStr[(int)commandReceive.state], commandReceive.duty);
 | 
			
		||||
        ESP_LOGI(TAG, "Read command from queue: state=%s, duty=%.2f", motorstateStr[(int)commandReceive.state], commandReceive.duty);
 | 
			
		||||
        state = commandReceive.state;
 | 
			
		||||
        dutyTarget = commandReceive.duty;
 | 
			
		||||
		receiveTimeout = false;
 | 
			
		||||
@ -92,7 +93,8 @@ void controlledMotor::handle(){
 | 
			
		||||
            case motorstate_t::BRAKE:
 | 
			
		||||
                //update state
 | 
			
		||||
                state = motorstate_t::BRAKE;
 | 
			
		||||
                dutyTarget = 0;
 | 
			
		||||
                //dutyTarget = 0;
 | 
			
		||||
                dutyTarget = fabs(commandReceive.duty);
 | 
			
		||||
                break;
 | 
			
		||||
            case motorstate_t::IDLE:
 | 
			
		||||
                dutyTarget = 0;
 | 
			
		||||
@ -108,11 +110,12 @@ void controlledMotor::handle(){
 | 
			
		||||
 | 
			
		||||
	//--- timeout, no data ---
 | 
			
		||||
	//turn motors off if no data received for long time (e.g. no uart data / control offline)
 | 
			
		||||
	if ((esp_log_timestamp() - timestamp_commandReceived) > 3000 && !receiveTimeout){
 | 
			
		||||
	//TODO no timeout when braking?
 | 
			
		||||
	if ((esp_log_timestamp() - timestamp_commandReceived) > TIMEOUT_IDLE_WHEN_NO_COMMAND && !receiveTimeout){
 | 
			
		||||
		receiveTimeout = true;
 | 
			
		||||
		state = motorstate_t::IDLE;
 | 
			
		||||
		dutyTarget = 0;
 | 
			
		||||
		ESP_LOGE(TAG, "TIMEOUT, no target data received for more than 3s -> switch to IDLE");
 | 
			
		||||
		ESP_LOGE(TAG, "TIMEOUT, no target data received for more than %ds -> switch to IDLE", TIMEOUT_IDLE_WHEN_NO_COMMAND/1000);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    //--- calculate increment ---
 | 
			
		||||
@ -131,14 +134,15 @@ void controlledMotor::handle(){
 | 
			
		||||
        dutyIncrementDecel = 100;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    
 | 
			
		||||
    //--- BRAKE ---
 | 
			
		||||
    //brake immediately, update state, duty and exit this cycle of handle function
 | 
			
		||||
    if (state == motorstate_t::BRAKE){
 | 
			
		||||
                motor.set(motorstate_t::BRAKE, 0);
 | 
			
		||||
                dutyNow = 0;
 | 
			
		||||
                return; //no need to run the fade algorithm
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
	//--- BRAKE ---
 | 
			
		||||
	//brake immediately, update state, duty and exit this cycle of handle function
 | 
			
		||||
	if (state == motorstate_t::BRAKE){
 | 
			
		||||
		ESP_LOGD(TAG, "braking - skip fading");
 | 
			
		||||
		motor.set(motorstate_t::BRAKE, dutyTarget);
 | 
			
		||||
		//dutyNow = 0;
 | 
			
		||||
		return; //no need to run the fade algorithm
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    //--- calculate difference ---
 | 
			
		||||
 | 
			
		||||
@ -86,7 +86,7 @@ void single100a::set(motorstate_t state_f, float duty_f){
 | 
			
		||||
    uint32_t dutyScaled;
 | 
			
		||||
    if (duty_f > 100) { //target duty above 100%
 | 
			
		||||
        dutyScaled = dutyMax;
 | 
			
		||||
    } else if (duty_f <= 0) { //target at or below 0%
 | 
			
		||||
    } else if (duty_f < 0) { //target at or below 0%
 | 
			
		||||
		state_f = motorstate_t::IDLE;
 | 
			
		||||
        dutyScaled = 0;
 | 
			
		||||
    } else { //target duty 0-100%
 | 
			
		||||
@ -94,6 +94,8 @@ void single100a::set(motorstate_t state_f, float duty_f){
 | 
			
		||||
        dutyScaled = duty_f / 100 * dutyMax;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ESP_LOGV(TAG, "target-state=%s, duty=%d/%d, duty_input=%.3f%%", motorstateStr[(int)state_f], dutyScaled, dutyMax, duty_f);
 | 
			
		||||
 | 
			
		||||
	//TODO: only when previous mode was BRAKE?
 | 
			
		||||
	if (state_f != motorstate_t::BRAKE){
 | 
			
		||||
		//reset brake wait state
 | 
			
		||||
@ -119,7 +121,7 @@ void single100a::set(motorstate_t state_f, float duty_f){
 | 
			
		||||
		case motorstate_t::BRAKE:
 | 
			
		||||
			//prevent full short (no brake resistors) due to slow relay, also reduces switching load
 | 
			
		||||
			if (!brakeWaitingForRelay){
 | 
			
		||||
				ESP_LOGW(TAG, "BRAKE: turned on relay, waiting in IDLE for %d ms", BRAKE_RELAY_DELAY_MS);
 | 
			
		||||
				ESP_LOGW(TAG, "BRAKE: turned on relay, waiting in IDLE for %d ms, then apply brake", BRAKE_RELAY_DELAY_MS);
 | 
			
		||||
				//switch driver to IDLE for now
 | 
			
		||||
				gpio_set_level(config.gpio_a, config.aEnabledPinState);
 | 
			
		||||
				gpio_set_level(config.gpio_b, config.bEnabledPinState);
 | 
			
		||||
@ -162,5 +164,4 @@ void single100a::set(motorstate_t state_f, float duty_f){
 | 
			
		||||
            gpio_set_level(config.gpio_b, config.bEnabledPinState);
 | 
			
		||||
            break;
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGV(TAG, "set module to state=%s, duty=%d/%d, duty_input=%.3f%%", motorstateStr[(int)state_f], dutyScaled, dutyMax, duty_f);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user