def test_multimeter_measurement_error(self): # Setup self.device = Mock() # Mock() replaces Device self.device.get_measurement_value.side_effect = DeviceError( 'Hardware problem!') self.multimeter = Multimeter(self.device) # Exercise & Verify with self.assertRaises(MeasurementError): self.multimeter.measure()
def test_multimeter_set_range(self): # Setup self.device = Mock() # Mock() replaces Device self.multimeter = Multimeter(self.device) # Exercise self.multimeter.set_range(10) # Verify self.device.set_measurement_range.assert_called_with(10)
def test_multimeter_set_mode(self): # Setup self.device = Mock() # Mock() replaces Device self.multimeter = Multimeter(self.device) # Exercise self.multimeter.set_mode(MODE.DCV) # Verify self.device.set_measurement_mode.assert_called_with("dc_v")
def test_multimeter_measurement(self): # Setup self.device = Mock() # Mock() replaces Device self.device.get_measurement_value.return_value = 5.0 self.multimeter = Multimeter(self.device) # Exercise result = self.multimeter.measure() # Verify self.assertAlmostEqual(5.0, result, 1e-3)
def scanRoutine(port, startWvl, endWvl, numPtsPerWvl, stepSize, fileName, average = False): #Creates necessary objects. Also creates an empty array to store the measurements a = Mono(port) b = Multimeter(visa.ResourceManager()) data = {} avg = {} #If it needs to go backwards, it adjusts the step size if startWvl > endWvl: stepSize = -stepSize my_file = open(fileName, "w") #creates a file to write the measurements into a.goToWavelength(startWvl) #moves to the starting wavelength try: #put into a try statement in case of error #start at start wavelength, and increment by stepSize taking numPtsPerWvl data points at each wavelength for wvl in range(startWvl, endWvl, stepSize): a.goToWavelength(wvl) buf = [] #buffer for storing numPtsPerWvl data points for i in range(numPtsPerWvl): value = b.measureVoltageDC() #measures the Voltage buf.append(value) #adds the measurement to the array my_file.write(str(wvl) + "," + str(value) + "\n") #adds the wavelength and measurement to the file data[wvl] = buf #adds the small array to the larger array if average: avg[wvl] = sum(buf) #Since the upper limit of ranger() is not inclusive, we must additiionally measure the voltage at the designated upper limit. a.goToWavelength(endWvl) buf = [] for i in range(numPtsPerWvl): value = b.measureVoltageDC() buf.append(value) my_file.write(str(endWvl) + "," + str(value) + "\n") data[endWvl] = buf if average: avg[endWvl] = sum(buf) if average: my_file.write("\n\n") sorted_avg = sorted(avg.items(), key=operator.itemgetter(0)) for wvl in sorted_avg: my_file.write(str(wvl[0]) + "," + str(wvl[1]) + "\n") except Exception as e: #runs if an error occured during the try statement print "There was a scan routine error! Check the data file for clues!" print e finally: #closes the file my_file.close() return data
class MultimeterTest(unittest.TestCase): def __init__(self, methodName): super().__init__(methodName) self.device = None self.multimeter = None def test_multimeter_set_mode(self): # Setup self.device = Mock() # Mock() replaces Device self.multimeter = Multimeter(self.device) # Exercise self.multimeter.set_mode(MODE.DCV) # Verify self.device.set_measurement_mode.assert_called_with("dc_v") def test_multimeter_set_range(self): # Setup self.device = Mock() # Mock() replaces Device self.multimeter = Multimeter(self.device) # Exercise self.multimeter.set_range(10) # Verify self.device.set_measurement_range.assert_called_with(10) def test_multimeter_measurement(self): # Setup self.device = Mock() # Mock() replaces Device self.device.get_measurement_value.return_value = 5.0 self.multimeter = Multimeter(self.device) # Exercise result = self.multimeter.measure() # Verify self.assertAlmostEqual(5.0, result, 1e-3) def test_multimeter_measurement_error(self): # Setup self.device = Mock() # Mock() replaces Device self.device.get_measurement_value.side_effect = DeviceError( 'Hardware problem!') self.multimeter = Multimeter(self.device) # Exercise & Verify with self.assertRaises(MeasurementError): self.multimeter.measure()
def main(): file_write = FileWrite() device_detection = Device_detection() device_detection.async_connect() I = device_detection.device # instrument cluster initialization oscilloscope = Oscilloscope(I, file_write) logic_analyser = LogicAnalyser(I, file_write) power_source = Power_source(I, file_write) multimeter = Multimeter(I, file_write) wave_generator = Wave_generator(I, file_write) robotic_arm = RoboticArm(I, file_write) sensors = Sensors(I, file_write) while (True): in_stream_data = input() parsed_stream_data = json.loads(in_stream_data) command = parsed_stream_data['command'] # ---------------------------- Oscilloscope block ------------------------------ if command == 'START_OSC': oscilloscope.start_read() if command == "STOP_OSC": oscilloscope.stop_read() if command == "SET_CONFIG_OSC": old_read_state = oscilloscope.is_reading_voltage or oscilloscope.is_reading_fft or oscilloscope.is_reading_xy_plot if old_read_state: oscilloscope.stop_read() time_base = parsed_stream_data['timeBase'] number_of_samples = parsed_stream_data['numberOfSamples'] ch1 = parsed_stream_data['ch1'] ch2 = parsed_stream_data['ch2'] ch3 = parsed_stream_data['ch3'] mic = parsed_stream_data['mic'] # ch1_map = parsed_stream_data['ch1Map'] # ch2_map = parsed_stream_data['ch2Map'] # ch3_map = parsed_stream_data['ch3Map'] is_trigger_active = parsed_stream_data['isTriggerActive'] trigger_voltage = parsed_stream_data['triggerVoltage'] trigger_channel = parsed_stream_data['triggerChannel'] is_fourier_transform_active = parsed_stream_data[ 'isFourierTransformActive'] fit_type = parsed_stream_data['fitType'] fit_channel1 = parsed_stream_data['fitChannel1'] fit_channel2 = parsed_stream_data['fitChannel2'] is_xy_plot_active = parsed_stream_data['isXYPlotActive'] plot_channel1 = parsed_stream_data['plotChannel1'] plot_channel2 = parsed_stream_data['plotChannel2'] oscilloscope.set_config( time_base, number_of_samples, ch1, ch2, ch3, mic, is_trigger_active, trigger_channel, trigger_voltage, is_fourier_transform_active, fit_type, fit_channel1, fit_channel2, is_xy_plot_active, plot_channel1, plot_channel2) if old_read_state: oscilloscope.start_read() if command == 'GET_CONFIG_OSC': oscilloscope.get_config() # ---------------------------- LA block ------------------------------ if command == 'START_LA': logic_analyser.start_read() if command == "STOP_LA": logic_analyser.stop_read() if command == "SET_CONFIG_LA": number_of_channels = parsed_stream_data['numberOfChannels'] trigger1_type = parsed_stream_data['trigger1Type'] trigger2_type = parsed_stream_data['trigger2Type'] trigger3_type = parsed_stream_data['trigger3Type'] trigger4_type = parsed_stream_data['trigger4Type'] capture_time = parsed_stream_data['captureTime'] logic_analyser.set_config(number_of_channels, trigger1_type, trigger2_type, trigger3_type, trigger4_type, capture_time) if command == 'GET_CONFIG_LA': logic_analyser.get_config() # --------------------------- Multimeter block --------------------------------- if command == 'START_MUL_MET': multimeter.start_read() if command == 'STOP_MUL_MET': multimeter.stop_read() if command == 'SET_CONFIG_MUL_MET': old_read_state = multimeter.is_reading if multimeter.is_reading: multimeter.stop_read() active_category = parsed_stream_data['activeCategory'] active_subtype = parsed_stream_data['activeSubType'] parameter = None if active_category == 'PULSE': parameter = parsed_stream_data['parameter'] multimeter.set_config(active_category, active_subtype, parameter) if old_read_state: multimeter.start_read() if command == 'GET_CONFIG_MUL_MET': multimeter.get_config() # -------------------------- Power Source block --------------------------------- if command == 'SET_CONFIG_PWR_SRC': pcs_value = parsed_stream_data['pcs'] pv1_value = parsed_stream_data['pv1'] pv2_value = parsed_stream_data['pv2'] pv3_value = parsed_stream_data['pv3'] power_source.set_config(pcs_value, pv1_value, pv2_value, pv3_value) if command == 'GET_CONFIG_PWR_SRC': power_source.get_config() if command == 'GET_CONFIG_PWR_SRC_FILE': data_path = parsed_stream_data['dataPath'] power_source.get_config_from_file(data_path) if command == 'SAVE_CONFIG_PWR_SRC': data_path = parsed_stream_data['dataPath'] power_source.save_config(data_path) # -------------------------- Wave Generator block --------------------------------- if command == 'SET_CONFIG_WAV_GEN': wave = parsed_stream_data['wave'] digital = parsed_stream_data['digital'] s1_frequency = parsed_stream_data['s1Frequency'] s2_frequency = parsed_stream_data['s2Frequency'] s2_phase = parsed_stream_data['s2Phase'] wave_form_s1 = parsed_stream_data['waveFormS1'] wave_form_s2 = parsed_stream_data['waveFormS2'] pwm_frequency = parsed_stream_data['pwmFrequency'] sqr1_duty_cycle = parsed_stream_data['sqr1DutyCycle'] sqr2_duty_cycle = parsed_stream_data['sqr2DutyCycle'] sqr2_phase = parsed_stream_data['sqr2Phase'] sqr3_duty_cycle = parsed_stream_data['sqr3DutyCycle'] sqr3_phase = parsed_stream_data['sqr3Phase'] sqr4_duty_cycle = parsed_stream_data['sqr4DutyCycle'] sqr4_phase = parsed_stream_data['sqr4Phase'] wave_generator.set_config(wave, digital, s1_frequency, s2_frequency, s2_phase, wave_form_s1, wave_form_s2, pwm_frequency, sqr1_duty_cycle, sqr2_duty_cycle, sqr2_phase, sqr3_duty_cycle, sqr3_phase, sqr4_duty_cycle, sqr4_phase) if command == 'GET_CONFIG_WAV_GEN': wave_generator.get_config() if command == 'GET_CONFIG_WAV_GEN_FILE': data_path = parsed_stream_data['dataPath'] wave_generator.get_config_from_file(data_path) if command == 'SAVE_CONFIG_WAV_GEN': data_path = parsed_stream_data['dataPath'] wave_generator.save_config(data_path) # ------------------------------- Robtic Arm block ------------------------------- if command == 'SET_ROBO_ARM': angle1 = parsed_stream_data['angle1'] angle2 = parsed_stream_data['angle2'] angle3 = parsed_stream_data['angle3'] angle4 = parsed_stream_data['angle4'] robotic_arm.setServo(angle1, angle2, angle3, angle4) # ------------------------------- Sensors block ---------------------------------- if command == 'SENSORS_SCAN': sensors.scan() # -------------------------------- Write block ----------------------------------- if command == 'START_WRITE': data_path = parsed_stream_data['dataPath'] device_type = parsed_stream_data['deviceType'] file_write.start_recording(data_path, device_type) if command == 'STOP_WRITE': file_write.stop_recording() # -------------------------- Script termination block ---------------------------- if command == 'KILL': exit()