Skip to content
Snippets Groups Projects
Commit fd1b2206 authored by Martin Killenberg's avatar Martin Killenberg
Browse files

fix: simlated state is not shared between processes

parent cb62a8f3
No related branches found
No related tags found
No related merge requests found
......@@ -29,7 +29,7 @@ class VnaDummy:
self.simulated_magnitude_difference_hum = 0.0
self.simulated_phase_difference_hum = 0.0
self.simulated_state = shared_simulated_state.get_simulated_state()
self.last_simulated_time = self.simulated_state.simulated_time
self.last_simulated_time = self.simulated_state.simulated_time.value
self.tau_mag_temp = 25 # change rate of magnitude in dependence on temperature
self.tau_phase_temp = 35 # change rate of phase in dependence on temperature
self.tau_mag_hum = 250 # change rate of magnitude in dependence on humidity
......@@ -47,19 +47,19 @@ class VnaDummy:
def run_simulation(self):
self.simulate_magnitude()
self.simulate_phase()
self.last_simulated_time = self.simulated_state.simulated_time
self.last_simulated_time = self.simulated_state.simulated_time.value
def simulate_magnitude(self):
target_magnitude_difference_temp = self.magnitude_slope_temp * \
(self.simulated_state.simulated_temperature - self.reference_temp)
(self.simulated_state.simulated_temperature.value - self.reference_temp)
delta_magnitude_temp = target_magnitude_difference_temp - self.simulated_magnitude_difference_temp
target_magnitude_difference_hum = self.magnitude_slope_hum * \
(self.simulated_state.simulated_humidity - self.reference_hum)
(self.simulated_state.simulated_humidity.value - self.reference_hum)
delta_magnitude_hum = target_magnitude_difference_hum - self.simulated_magnitude_difference_hum
delta_time = self.simulated_state.simulated_time - self.last_simulated_time
delta_time = self.simulated_state.simulated_time.value - self.last_simulated_time
self.simulated_magnitude_difference_temp = self.simulated_magnitude_difference_temp + \
delta_magnitude_temp * \
......@@ -71,13 +71,13 @@ class VnaDummy:
def simulate_phase(self):
target_phase_difference_temp = self.phase_slope_temp * \
(self.simulated_state.simulated_temperature - self.reference_temp)
(self.simulated_state.simulated_temperature.value - self.reference_temp)
delta_phase_temp = target_phase_difference_temp - self.simulated_phase_difference_temp
target_phase_difference_hum = self.phase_slope_hum * \
(self.simulated_state.simulated_humidity - self.reference_hum)
(self.simulated_state.simulated_humidity.value - self.reference_hum)
delta_phase_hum = target_phase_difference_hum - self.simulated_phase_difference_hum
delta_time = self.simulated_state.simulated_time - self.last_simulated_time
delta_time = self.simulated_state.simulated_time.value - self.last_simulated_time
self.simulated_phase_difference_temp = self.simulated_phase_difference_temp + \
delta_phase_temp * \
......@@ -138,7 +138,7 @@ class VnaDummy:
for i in range(50):
measurment_string += str(real_val) + ',' + str(imaginary_val) + ','
# doing the measurement and reading out some time
self.simulated_state.simulated_time += self.communication_delay
self.simulated_state.simulated_time.value += self.communication_delay
# removed last comma by adding slicing([:-1])
return measurment_string[:-1]
......
......@@ -22,10 +22,10 @@ class ClimateChamberDummy:
# store the credentials so that methods can access them
self.target_accuracy = target_accuracy
self.simulated_state = shared_simulated_state.get_simulated_state()
self.last_simulation_time = self.simulated_state.simulated_time
self.last_simulation_time = self.simulated_state.simulated_time.value
self.simulated_mode = 'OFF'
self.target_temperature = self.simulated_state.simulated_temperature
self.target_humidity = self.simulated_state.simulated_humidity
self.target_temperature = self.simulated_state.simulated_temperature.value
self.target_humidity = self.simulated_state.simulated_humidity.value
self.temperature_slope = 0.1 # 0.1 degrees/second
self.humidity_slope = 0.5 # degrees/second
self.communication_delay = 0.3
......@@ -33,55 +33,55 @@ class ClimateChamberDummy:
def run_simulation(self):
self.simulate_temp()
self.simulate_hum()
self.last_simulation_time = self.simulated_state.simulated_time
self.last_simulation_time = self.simulated_state.simulated_time.value
def simulate_temp(self):
max_delta_temp = self.target_temperature - self.simulated_state.simulated_temperature
max_delta_temp = self.target_temperature - self.simulated_state.simulated_temperature.value
if max_delta_temp < 0:
direction = -1
else:
direction = 1
new_time = self.simulated_state.simulated_time
new_time = self.simulated_state.simulated_time.value
delta_time = new_time - self.last_simulation_time
max_temp_change_in_delta_time = self.temperature_slope * delta_time
if abs(max_delta_temp) < max_temp_change_in_delta_time:
self.simulated_state.simulated_temperature = self.target_temperature
self.simulated_state.simulated_temperature.value = self.target_temperature
else:
self.simulated_state.simulated_temperature = self.simulated_state.simulated_temperature + direction * \
self.simulated_state.simulated_temperature.value = self.simulated_state.simulated_temperature.value + direction * \
self.temperature_slope * delta_time
def simulate_hum(self):
max_delta_hum = self.target_humidity - self.simulated_state.simulated_humidity
max_delta_hum = self.target_humidity - self.simulated_state.simulated_humidity.value
if max_delta_hum < 0:
direction = -1
else:
direction = 1
new_time = self.simulated_state.simulated_time
new_time = self.simulated_state.simulated_time.value
delta_time = new_time - self.last_simulation_time
max_hum_change_in_delta_time = self.humidity_slope * delta_time
if abs(max_delta_hum) < max_hum_change_in_delta_time:
self.simulated_state.simulated_humidity = self.target_humidity
self.simulated_state.simulated_humidity.value = self.target_humidity
else:
self.simulated_state.simulated_humidity = self.simulated_state.simulated_humidity + direction * \
self.simulated_state.simulated_humidity.value = self.simulated_state.simulated_humidity.value + direction * \
self.humidity_slope * delta_time
# the communication section
def read_temperature(self):
self.simulated_state.simulated_time += self.communication_delay
self.simulated_state.simulated_time.value += self.communication_delay
# data format: [current temp, set temp, upper limit, lower limit]
return [self.simulated_state.simulated_temperature, self.target_temperature, 0., 100.]
return [self.simulated_state.simulated_temperature.value, self.target_temperature, 0., 100.]
def read_humidity(self):
self.simulated_state.simulated_time += self.communication_delay
self.simulated_state.simulated_time.value += self.communication_delay
# data format: [current temp, set temp, upper limit, lower limit]
return [self.simulated_state.simulated_humidity, self.target_humidity, 0., 100.]
return [self.simulated_state.simulated_humidity.value, self.target_humidity, 0., 100.]
def set_temperature(self, temperature):
# simulate everything that has happened until now, before we store the new setpoint
......@@ -89,7 +89,7 @@ class ClimateChamberDummy:
self.target_temperature = temperature
print('ChamberDummy: target temperature now is ' + str(self.target_temperature))
response = 'OK: TEMP, S30\r\n'
self.simulated_state.simulated_time += self.communication_delay
self.simulated_state.simulated_time.value += self.communication_delay
return response
def set_humidity(self, humidity):
......@@ -99,7 +99,7 @@ class ClimateChamberDummy:
self.target_humidity = humidity
print('ChamberDummy: target humidity now is ' + str(self.target_humidity))
response = 'OK: HUMI, S30\r\n'
self.simulated_state.simulated_time += self.communication_delay
self.simulated_state.simulated_time.value += self.communication_delay
return response
def close(self):
......@@ -110,7 +110,7 @@ class ClimateChamberDummy:
def set_mode(self, mode):
self.simulated_mode = mode
response = 'FIXME!!!'
self.simulated_state.simulated_time += self.communication_delay
self.simulated_state.simulated_time.value += self.communication_delay
return response
def get_const(self):
......@@ -139,7 +139,7 @@ class ClimateChamberDummy:
self.set_humidity(setPoint[1])
def read_mode(self):
self.simulated_state.simulated_time += self.communication_delay
self.simulated_state.simulated_time.value += self.communication_delay
return self.simulated_mode
# deprecated. Use read_humidity etc. instead
......@@ -148,13 +148,13 @@ class ClimateChamberDummy:
return str(self.read_temperature()[0]) + ', ' + str(self.read_humidity()[0]) + ', ' + self.simulated_mode + ',0'
def is_temperature_reached(self):
if abs(self.simulated_state.simulated_temperature - self.target_temperature) < self.target_accuracy[0]:
if abs(self.simulated_state.simulated_temperature.value - self.target_temperature) < self.target_accuracy[0]:
return True
else:
return False
def is_humidity_reached(self):
if abs(self.simulated_state.simulated_humidity - self.target_humidity) < self.target_accuracy[1]:
if abs(self.simulated_state.simulated_humidity.value - self.target_humidity) < self.target_accuracy[1]:
return True
else:
return False
......
import multiprocessing as mp
simulated_state_instance = None
......@@ -11,6 +13,6 @@ def get_simulated_state():
class SimulatedState:
def __init__(self):
self.simulated_time = 0
self.simulated_temperature = 25
self.simulated_humidity = 35
self.simulated_time = mp.Value('d', 0.0)
self.simulated_temperature = mp.Value('d', 25.)
self.simulated_humidity = mp.Value('d', 35.)
......@@ -29,10 +29,11 @@ class VirtualClock:
self.VNA_dummy = VNA_dummy.get_vna_dummy(target_accuracy)
def time(self):
return self.simulated_state.simulated_time
return self.simulated_state.simulated_time.value
def sleep(self, sleep_time):
self.simulated_state.simulated_time += sleep_time
time.sleep(1)
self.simulated_state.simulated_time.value += sleep_time
self.climate_chamber_dummy.run_simulation()
self.VNA_dummy.run_simulation()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment