Add motor velocity / position check
This commit is contained in:
parent
8d6f7db12a
commit
d3ba2c1134
1 changed files with 17 additions and 4 deletions
|
@ -126,17 +126,30 @@ func motor_sim(state_init State, config Config, sim_freq float64){
|
||||||
close(stateChan)
|
close(stateChan)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
//Clamp cmd_vel to max/min limits
|
||||||
|
if cmd_vel > config.Max_vel || cmd_vel < -config.Max_vel {
|
||||||
|
log.Printf("Command velocity out of limits (%f). Clamping to %f",cmd_vel,config.Max_vel)
|
||||||
|
if cmd_vel>=0 {
|
||||||
|
cmd_vel = config.Max_vel
|
||||||
|
} else if cmd_vel < 0 {
|
||||||
|
cmd_vel = -config.Max_vel
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
default: //No command, do nothing
|
default: //No command, do nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
// cmdVelChan
|
// cmdVelChan
|
||||||
//Update state
|
//Update state
|
||||||
|
//TODO: Error recovery ?
|
||||||
|
if state.Error == "" { //If error, stop update
|
||||||
state.Velocity = cmd_vel //Infinite acceleration
|
state.Velocity = cmd_vel //Infinite acceleration
|
||||||
state.Angle += state.Velocity * 1/sim_freq //Neglect computation delay for delta time
|
state.Angle += state.Velocity * 1/sim_freq //Neglect computation delay for delta time
|
||||||
|
}
|
||||||
|
|
||||||
//TODO: Error check
|
if state.Angle > config.Max_pos || state.Angle < config.Min_pos { //Limit angle
|
||||||
|
state.Error = "Position out of range"
|
||||||
|
}
|
||||||
//Send new state to channel (non-blocking)
|
//Send new state to channel (non-blocking)
|
||||||
select{
|
select{
|
||||||
case stateChan <- state:
|
case stateChan <- state:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue