Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fast_brake command for HWP rapid spindown #787

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions socs/agents/hwp_supervisor/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,7 @@ class Brake:
freq_tol: float
freq_tol_duration: float
brake_voltage: float
fast: bool = False

@dataclass
class WaitForBrake:
Expand All @@ -656,6 +657,7 @@ class WaitForBrake:
"""
min_freq: float
prev_freq: float = None
fast: bool = False

@dataclass
class PmxOff:
Expand Down Expand Up @@ -1104,45 +1106,56 @@ def check_acu_ok_for_spinup():
self.action.set_state(ControlState.Done(success=True))

elif isinstance(state, ControlState.Brake):
pid_dir = int(query_pid_state()['direction'])
if state.fast:
# set pcu fase brake state
new_pcu_state = 'on_2' if (pid_dir == 1) else 'on_1'
min_freq = 0.2
else:
new_pcu_state = 'off'
min_freq = 0.5
self.run_and_validate(
clients.pcu.send_command,
kwargs={'command': 'off'}, timeout=None
kwargs={'command': new_pcu_state}, timeout=None
)

# Flip PID direciton and tune stop
pid_dir = int(query_pid_state()['direction'])
new_d = '0' if (pid_dir == 1) else '1'
self.run_and_validate(clients.pid.set_direction,
kwargs=dict(direction=new_d))
self.run_and_validate(clients.pid.tune_stop)

if not state.fast:
# Flip PID direciton and tune stop
new_d = '0' if (pid_dir == 1) else '1'
self.run_and_validate(clients.pid.set_direction,
kwargs=dict(direction=new_d))
self.run_and_validate(clients.pid.tune_stop)
self.run_and_validate(clients.pmx.ign_ext)
self.run_and_validate(clients.pmx.set_v, kwargs={'volt': state.brake_voltage})
self.run_and_validate(clients.pmx.set_on)

time.sleep(10)
self.action.set_state(ControlState.WaitForBrake(
min_freq=0.5,
min_freq=min_freq,
prev_freq=hwp_state.enc_freq,
fast=state.fast
))

elif isinstance(state, ControlState.WaitForBrake):
f0 = query_pid_state()['current_freq']
time.sleep(5)
f1 = query_pid_state()['current_freq']
if f0 < 0.5 or (f1 > f0):
if f0 < state.min_freq or (f1 > f0):
self.log.info("Turning off PMX and putting PCU in stop mode")
self.run_and_validate(clients.pmx.set_off)
self.run_and_validate(
clients.pcu.send_command,
kwargs={'command': 'stop'}, timeout=None
)
self.action.set_state(ControlState.WaitForTargetFreq(
target_freq=0,
freq_tol=0.05,
freq_tol_duration=30,
))
return
if state.fast:
time.sleep(5)
return
Comment on lines +1149 to +1151
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not very safe. We should do WaitForTargetFreq.

But I think the motivation of this PR is to change the rotation direction quickly for special calibration, and you may not want to add WaitForTargetFreq. Therefore, it might be better to have a separate function that will change rotation direction, which will be faster than doing fast brake and spin up.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I agree. Just wondering what is the behavior if you PID to -2 Hz by changing the PID direction while sitting at 2 Hz? If we can get something like that to work I agree we should make it a specialized operation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the behavior if you PID to -2 Hz by changing the PID direction while sitting at 2 Hz?

This command will also rapidly change the rotation direction.
Changing PID direction flips the direction of acceleration and the HWP slows down. As the absolute PID frequency stays at 2 Hz, the PMX serves the highest voltage and current to recover 2 Hz, which results in a strong brake. Then the HWP speeds up to opposite direction and reaches -2 Hz.
But the current hwp_supervisor looks it doesn't expect this procedure so this will require some changes anyway.
Which looks better @ykyohei ?

else:
self.action.set_state(ControlState.WaitForTargetFreq(
target_freq=0,
freq_tol=0.05,
freq_tol_duration=30,
))
return

elif isinstance(state, ControlState.EnableDriverBoard):
def set_outlet_state(outlet: int, outlet_state: bool):
Expand Down Expand Up @@ -1549,6 +1562,7 @@ def set_const_voltage(self, session, params):
@ocs_agent.param('freq_tol', type=float, default=0.05)
@ocs_agent.param('freq_tol_duration', type=float, default=10)
@ocs_agent.param('brake_voltage', type=float, default=10.)
@ocs_agent.param('fast', type=bool, default=False)
def brake(self, session, params):
"""brake(freq_thresh=0.05, freq_thresh_duration=10, brake_voltage=10)

Expand Down Expand Up @@ -1582,6 +1596,7 @@ def brake(self, session, params):
freq_tol=params['freq_tol'],
freq_tol_duration=params['freq_tol_duration'],
brake_voltage=params['brake_voltage'],
fast=params['fast']
)
action = self.control_state_machine.request_new_action(state)
action.sleep_until_complete(session=session)
Expand Down
Loading