Commit 8406521f authored by MikhailChaikovskii's avatar MikhailChaikovskii
Browse files

default behavior for events

Showing with 48 additions and 35 deletions
+48 -35
......@@ -74,15 +74,14 @@ class EventContactTimeOut(SimulationSingleEvent):
"""
if self.contact:
return EventCommands.CONTINUE
else:
self.contact = env_data.get_amount_contacts()[0] > 0
if self.contact:
return EventCommands.CONTINUE
else:
if current_time > self.reference_time:
self.state = True
self.step_n = step_n
return EventCommands.STOP
self.contact = env_data.get_amount_contacts()[0] > 0
if self.contact and current_time > self.reference_time:
self.state = True
self.step_n = step_n
return EventCommands.STOP
return EventCommands.CONTINUE
class EventFlyingApart(SimulationSingleEvent):
......@@ -112,6 +111,8 @@ class EventFlyingApart(SimulationSingleEvent):
self.step_n = step_n
return EventCommands.STOP
return EventCommands.CONTINUE
class EventSlipOut(SimulationSingleEvent):
"""The event that stops simulation if the body slips out from the grasp after the contact.
......@@ -140,16 +141,14 @@ class EventSlipOut(SimulationSingleEvent):
if contact:
self.time_last_contact = current_time
return EventCommands.CONTINUE
else:
if self.time_last_contact is None:
return EventCommands.CONTINUE
else:
if current_time - self.time_last_contact > self.reference_time:
if (not self.time_last_contact is None) :
if current_time - self.time_last_contact > self.reference_time:
self.step_n = step_n
self.state = True
return EventCommands.STOP
else:
return EventCommands.CONTINUE
return EventCommands.CONTINUE
class EventGrasp(SimulationSingleEvent):
......@@ -187,14 +186,14 @@ class EventGrasp(SimulationSingleEvent):
Returns:
EventCommands: return a command for simulation
"""
if not self.state and current_time > self.grasp_limit_time:
return EventCommands.STOP
if self.state:
if current_time > self.force_test_time + self.grasp_time:
return EventCommands.STOP
else:
return EventCommands.CONTINUE
elif current_time > self.grasp_limit_time:
return EventCommands.STOP
if not self.contact:
self.contact = env_data.get_amount_contacts()[0] > 0
......@@ -213,3 +212,5 @@ class EventGrasp(SimulationSingleEvent):
print('Grasp event!', current_time)
return EventCommands.ACTIVATE
return EventCommands.CONTINUE
......@@ -13,6 +13,8 @@ from rostok.virtual_experiment.robot_new import BuiltGraphChrono, RobotChrono
from rostok.virtual_experiment.sensors import DataStorage, Sensor
from rostok.criterion.simulation_flags import SimulationSingleEvent, EventCommands
import time
class SystemPreviewChrono:
"""A simulation of the motionless environment and design"""
......@@ -107,7 +109,7 @@ class SimulationResult:
key_storage = storage[key]
for key_2 in key_storage:
value = key_storage[key_2]
new_value = value[:step_n+2:]
new_value = value[:step_n + 2:]
key_storage[key_2] = new_value
if self.environment_final_ds:
......@@ -116,7 +118,7 @@ class SimulationResult:
key_storage = storage[key]
for key_2 in key_storage:
value = key_storage[key_2]
new_value = value[:step_n+2:]
new_value = value[:step_n + 2:]
key_storage[key_2] = new_value
def reduce_nan(self):
......@@ -281,7 +283,7 @@ class RobotSimulationChrono():
number_of_steps: int,
step_length: float,
frame_update: int,
event_container: List[SimulationSingleEvent]=None,
event_container: List[SimulationSingleEvent] = None,
visualize=False,
):
"""Execute a simulation.
......@@ -309,7 +311,8 @@ class RobotSimulationChrono():
for i in range(number_of_steps):
current_time = self.chrono_system.GetChTime()
self.simulate_step(step_length, current_time, i)
self.result.time_vector.append(self.chrono_system.GetChTime()) # TODO: timevector can be constructed from number_of_steps and time_step
self.result.time_vector.append(self.chrono_system.GetChTime(
)) # TODO: timevector can be constructed from number_of_steps and time_step
if vis:
vis.Run()
if i % frame_update == 0:
......@@ -318,7 +321,8 @@ class RobotSimulationChrono():
vis.EndScene()
if event_container:
for event in event_container:
event_command = event.event_check(current_time, self.robot.sensor, self.data_storage.sensor)
event_command = event.event_check(current_time, self.robot.sensor,
self.data_storage.sensor)
if event_command == EventCommands.STOP:
stop_flag = True
break
......@@ -336,15 +340,31 @@ class RobotSimulationChrono():
self.result.reduce_nan()
return self.result
class RobotSimulationWithForceTest(RobotSimulationChrono):
def __init__(self, delay = False, object_list: List[Tuple[ChronoEasyShapeObject, bool]] = []):
def __init__(self, delay=False, object_list: List[Tuple[ChronoEasyShapeObject, bool]] = []):
super().__init__(object_list)
self.delay_flag = delay
def activate(self, code:int, current_time, step_n):
def activate(self, code: int, current_time, step_n):
if code == 0:
self.force_torque_container.controller_list[0].start_time = current_time
def handle_events(self, event_container, current_time, step_n):
if event_container == None:
return False
for event in event_container:
event_command = event.event_check(current_time, step_n, self.robot.sensor,
self.data_storage.sensor)
if event_command == EventCommands.STOP:
return True
elif event_command == EventCommands.ACTIVATE:
self.activate(event.activation_code, current_time, step_n)
return False
def simulate(
self,
number_of_steps: int,
......@@ -385,19 +405,11 @@ class RobotSimulationWithForceTest(RobotSimulationChrono):
vis.BeginScene(True, True, chrono.ChColor(0.1, 0.1, 0.1))
vis.Render()
vis.EndScene()
stop_flag = self.handle_events(event_container, current_time, i)
if event_container:
for event in event_container:
event_command = event.event_check(current_time, i, self.robot.sensor, self.data_storage.sensor)
if event_command == EventCommands.STOP:
stop_flag = True
break
elif event_command == EventCommands.ACTIVATE:
self.activate(event.activation_code, current_time, i)
if stop_flag:
break
# just to slow down the simulation
if self.delay_flag:
time.sleep(0.0001)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment