JMOT.control
1from JMOT import connect, extra 2from typing import Literal 3import numpy as np 4 5_CONTROL_SIGNAL = { 6 'display' : 1, 7 'local_log' : 2, 8 'flight_log' : 3, 9 'active_stage' : 5, 10 'switch_craft' : 6, 11 'set_target' : 7, 12 'set_ag' : 8, 13 'set_attitude' : 20, 14 'set_throttle' : 21, 15 'set_brake' : 22, 16 'set_pitching' : 23, 17 'set_heading' : 24, 18 'set_slider' : 25, 19 'set_heading_vector' : 26, 20 'set_translate' : 27, 21 'lock_head_mode' : 28, 22 'set_variable' : 41, 23 'set_variable_list' : 42, 24 'set_part_active' : 9, 25 'set_part_focuse' : 10, 26 'set_part_name' : 11, 27 'set_part_explode' : 12, 28 'set_part_transfer' : 13 29} 30 31ATTITUDE = { 32 "pitch":1, 33 "yaw":2, 34 "roll":3 35} 36 37TRANSLATE = { 38 'forward' : 1, 39 'right' : 2, 40 'up' : 3, 41 'mode' : 4 42} 43 44HEADMODE = { 45 'none' : 1, 46 'prograde' : 2, 47 'retrograde' : 3, 48 'target' : 4, 49 'burnmode' : 5, 50 'current' : 6 51} 52 53 54def display(message:str): 55 '''Display a message on the screen.\n''' 56 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['display']}<<{message}") 57 connect._verify(ack) 58 59def local_log(message:str): 60 '''Log a message in the part local log file.\n''' 61 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['local_log']}<<{message}") 62 connect._verify(ack) 63 64def flight_log(message:str, override:bool): 65 '''Log a message in the flight log file.\n 66 override: 67 if override is True, the message will be overridden. \n 68 if override is False, the message will be added to the end of the log file. \n 69 ''' 70 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['flight_log']}<<{message}<<{override}") 71 connect._verify(ack) 72 73def active_stage(): 74 '''Activate the next stage.\n''' 75 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['active_stage']}") 76 connect._verify(ack) 77 78def switch_craft(craft_id:int): 79 '''Switch to a different craft.\n''' 80 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['switch_craft']}<<{craft_id}") 81 connect._verify(ack) 82 83def set_target(target_name:str): 84 '''Set the target of the vessel or planet.\n''' 85 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_target']}<<{target_name}") 86 connect._verify(ack) 87 88def set_ag(active_group:int, status:bool): 89 '''Set the status of an action group.\n''' 90 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_ag']}<<{active_group}<<{status}") 91 connect._verify(ack) 92 93def set_attitude(attitude:Literal["pitch", "yaw", "roll"], value:float): 94 '''Set the value of an attitude.\n 95 attitude: 96 pitch,yaw,roll \n 97 \n''' 98 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_attitude']}<<{ATTITUDE[attitude]}<<{value}") 99 connect._verify(ack) 100 101def set_throttle(throttle:float): 102 '''Set the throttle value.\n''' 103 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_throttle']}<<{throttle}") 104 connect._verify(ack) 105 106def set_brake(brake:float): 107 '''Set the brake value.\n''' 108 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_brake']}<<{brake}") 109 connect._verify(ack) 110 111def set_pitching(pitching:float): 112 '''Set the pitching value.\n''' 113 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_pitching']}<<{pitching}") 114 connect._verify(ack) 115 116def set_heading(heading:float): 117 '''Set the heading value.\n''' 118 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_heading']}<<{heading}") 119 connect._verify(ack) 120 121def set_slider(slider:int, slider_value:float): 122 '''Set the value of a slider.\n''' 123 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_slider']}<<{slider}<<{slider_value}") 124 connect._verify(ack) 125 126def set_heading_vector(heading:np.ndarray): 127 '''Set the heading vector in ECI coordinates.\n''' 128 vec = extra.array2tuple(heading) 129 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_heading_vector']}<<{vec}") 130 connect._verify(ack) 131 132def set_translate(translate:Literal['forward', 'right', 'up', 'mode'], value:float): 133 '''Set the value of a translate.\n 134 translate:\n 135 forward:-1(back)-1(forward) \n 136 'right':-1(left)-1(right) \n 137 'up':-1(down)-1(up) \n 138 'mode':0(attitude mode) or 1(translate mode) \n 139 ''' 140 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_translate']}<<{TRANSLATE[translate]}<<{value}") 141 connect._verify(ack) 142 143def lock_head_mode(mode:Literal['none', 'prograde', 'retrograde', 'target', 'burnmode', 'current']): 144 '''Lock the head mode of the vessel.\n 145 mode:\n 146 none:no head mode\n 147 prograde:lock to prograde\n 148 retrograde:lock to retrograde\n 149 target:lock to target\n 150 burnmode:lock to burn mode\n 151 current:lock to current direction\n 152 ''' 153 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['lock_head_mode']}<<{HEADMODE[mode]}") 154 connect._verify(ack) 155 156def set_variable(part_id:int, variable_name:str, value:all): 157 '''Set the value of a variable in a part:\n 158 part_id:the part ID of the part\n 159 variable_name:the name of the variable in the part\n 160 value:the value of the variable\n 161 ''' 162 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_variable']}<<{part_id}<<{variable_name}<<{value}") 163 connect._verify(ack) 164 165def set_variable_list(part_id: int, list_variable_name: str, value_list: int|float|str|bool): 166 '''Set the value of a list variable in a part.\n 167 part_id:the part ID of the part\n 168 list_variable_name:the name of the list variable in the part\n 169 value_list:the list of values of the variable\n 170 ''' 171 safe_value_list = [str(item) for item in value_list] 172 173 ack = connect._send_message( 174 f"false<<{_CONTROL_SIGNAL['set_variable_list']}<<{part_id}<<{list_variable_name}<<{safe_value_list}" 175 ) 176 connect._verify(ack) 177 178def set_part_active(part_id:int, part_status:bool): 179 '''Set the status of a part.\n''' 180 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_part_active']}<<{part_id}<<{part_status}") 181 connect._verify(ack) 182 183def set_part_focuse(part_id:int, part_focuse:bool): 184 '''Set the camera focus of a part.\n''' 185 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_part_focuse']}<<{part_id}<<{part_focuse}") 186 connect._verify(ack) 187 188def set_part_name(part_id:int, part_name:str): 189 '''Set the name of a part.\n''' 190 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_part_name']}<<{part_id}<<{part_name}") 191 connect._verify(ack) 192 193def set_part_explode(part_id:int, part_explode_power:float): 194 '''Explode a part.\n 195 part_explode_power:the power of the explosion,\n 196 0 means no explosion but drop down, 1 means full explosion.\n 197 ''' 198 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_part_explode']}<<{part_id}<<{part_explode_power}") 199 connect._verify(ack) 200 201def set_part_transfer(part_id:int, part_trans:float): 202 '''Transfer a part.\n 203 part_trans:the transfer value,\n 204 <0 means exhuast\n 205 0 means no transfer\n 206 >0 means fill.\n 207 ''' 208 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_part_transfer']}<<{part_id}<<{part_trans}") 209 connect._verify(ack) 210
55def display(message:str): 56 '''Display a message on the screen.\n''' 57 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['display']}<<{message}") 58 connect._verify(ack)
Display a message on the screen.
60def local_log(message:str): 61 '''Log a message in the part local log file.\n''' 62 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['local_log']}<<{message}") 63 connect._verify(ack)
Log a message in the part local log file.
65def flight_log(message:str, override:bool): 66 '''Log a message in the flight log file.\n 67 override: 68 if override is True, the message will be overridden. \n 69 if override is False, the message will be added to the end of the log file. \n 70 ''' 71 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['flight_log']}<<{message}<<{override}") 72 connect._verify(ack)
Log a message in the flight log file.
override: if override is True, the message will be overridden.
if override is False, the message will be added to the end of the log file.
74def active_stage(): 75 '''Activate the next stage.\n''' 76 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['active_stage']}") 77 connect._verify(ack)
Activate the next stage.
79def switch_craft(craft_id:int): 80 '''Switch to a different craft.\n''' 81 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['switch_craft']}<<{craft_id}") 82 connect._verify(ack)
Switch to a different craft.
84def set_target(target_name:str): 85 '''Set the target of the vessel or planet.\n''' 86 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_target']}<<{target_name}") 87 connect._verify(ack)
Set the target of the vessel or planet.
89def set_ag(active_group:int, status:bool): 90 '''Set the status of an action group.\n''' 91 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_ag']}<<{active_group}<<{status}") 92 connect._verify(ack)
Set the status of an action group.
94def set_attitude(attitude:Literal["pitch", "yaw", "roll"], value:float): 95 '''Set the value of an attitude.\n 96 attitude: 97 pitch,yaw,roll \n 98 \n''' 99 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_attitude']}<<{ATTITUDE[attitude]}<<{value}") 100 connect._verify(ack)
Set the value of an attitude.
attitude: pitch,yaw,roll
102def set_throttle(throttle:float): 103 '''Set the throttle value.\n''' 104 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_throttle']}<<{throttle}") 105 connect._verify(ack)
Set the throttle value.
107def set_brake(brake:float): 108 '''Set the brake value.\n''' 109 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_brake']}<<{brake}") 110 connect._verify(ack)
Set the brake value.
112def set_pitching(pitching:float): 113 '''Set the pitching value.\n''' 114 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_pitching']}<<{pitching}") 115 connect._verify(ack)
Set the pitching value.
117def set_heading(heading:float): 118 '''Set the heading value.\n''' 119 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_heading']}<<{heading}") 120 connect._verify(ack)
Set the heading value.
122def set_slider(slider:int, slider_value:float): 123 '''Set the value of a slider.\n''' 124 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_slider']}<<{slider}<<{slider_value}") 125 connect._verify(ack)
Set the value of a slider.
127def set_heading_vector(heading:np.ndarray): 128 '''Set the heading vector in ECI coordinates.\n''' 129 vec = extra.array2tuple(heading) 130 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_heading_vector']}<<{vec}") 131 connect._verify(ack)
Set the heading vector in ECI coordinates.
133def set_translate(translate:Literal['forward', 'right', 'up', 'mode'], value:float): 134 '''Set the value of a translate.\n 135 translate:\n 136 forward:-1(back)-1(forward) \n 137 'right':-1(left)-1(right) \n 138 'up':-1(down)-1(up) \n 139 'mode':0(attitude mode) or 1(translate mode) \n 140 ''' 141 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_translate']}<<{TRANSLATE[translate]}<<{value}") 142 connect._verify(ack)
Set the value of a translate.
translate:
forward:-1(back)-1(forward)
'right':-1(left)-1(right)
'up':-1(down)-1(up)
'mode':0(attitude mode) or 1(translate mode)
144def lock_head_mode(mode:Literal['none', 'prograde', 'retrograde', 'target', 'burnmode', 'current']): 145 '''Lock the head mode of the vessel.\n 146 mode:\n 147 none:no head mode\n 148 prograde:lock to prograde\n 149 retrograde:lock to retrograde\n 150 target:lock to target\n 151 burnmode:lock to burn mode\n 152 current:lock to current direction\n 153 ''' 154 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['lock_head_mode']}<<{HEADMODE[mode]}") 155 connect._verify(ack)
Lock the head mode of the vessel.
mode:
none:no head mode
prograde:lock to prograde
retrograde:lock to retrograde
target:lock to target
burnmode:lock to burn mode
current:lock to current direction
157def set_variable(part_id:int, variable_name:str, value:all): 158 '''Set the value of a variable in a part:\n 159 part_id:the part ID of the part\n 160 variable_name:the name of the variable in the part\n 161 value:the value of the variable\n 162 ''' 163 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_variable']}<<{part_id}<<{variable_name}<<{value}") 164 connect._verify(ack)
Set the value of a variable in a part:
part_id:the part ID of the part
variable_name:the name of the variable in the part
value:the value of the variable
166def set_variable_list(part_id: int, list_variable_name: str, value_list: int|float|str|bool): 167 '''Set the value of a list variable in a part.\n 168 part_id:the part ID of the part\n 169 list_variable_name:the name of the list variable in the part\n 170 value_list:the list of values of the variable\n 171 ''' 172 safe_value_list = [str(item) for item in value_list] 173 174 ack = connect._send_message( 175 f"false<<{_CONTROL_SIGNAL['set_variable_list']}<<{part_id}<<{list_variable_name}<<{safe_value_list}" 176 ) 177 connect._verify(ack)
Set the value of a list variable in a part.
part_id:the part ID of the part
list_variable_name:the name of the list variable in the part
value_list:the list of values of the variable
179def set_part_active(part_id:int, part_status:bool): 180 '''Set the status of a part.\n''' 181 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_part_active']}<<{part_id}<<{part_status}") 182 connect._verify(ack)
Set the status of a part.
184def set_part_focuse(part_id:int, part_focuse:bool): 185 '''Set the camera focus of a part.\n''' 186 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_part_focuse']}<<{part_id}<<{part_focuse}") 187 connect._verify(ack)
Set the camera focus of a part.
189def set_part_name(part_id:int, part_name:str): 190 '''Set the name of a part.\n''' 191 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_part_name']}<<{part_id}<<{part_name}") 192 connect._verify(ack)
Set the name of a part.
194def set_part_explode(part_id:int, part_explode_power:float): 195 '''Explode a part.\n 196 part_explode_power:the power of the explosion,\n 197 0 means no explosion but drop down, 1 means full explosion.\n 198 ''' 199 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_part_explode']}<<{part_id}<<{part_explode_power}") 200 connect._verify(ack)
Explode a part.
part_explode_power:the power of the explosion,
0 means no explosion but drop down, 1 means full explosion.
202def set_part_transfer(part_id:int, part_trans:float): 203 '''Transfer a part.\n 204 part_trans:the transfer value,\n 205 <0 means exhuast\n 206 0 means no transfer\n 207 >0 means fill.\n 208 ''' 209 ack = connect._send_message(f"false<<{_CONTROL_SIGNAL['set_part_transfer']}<<{part_id}<<{part_trans}") 210 connect._verify(ack)
Transfer a part.
part_trans:the transfer value,
<0 means exhuast
0 means no transfer
>0 means fill.