diff --git a/src/adbasic/.gitignore b/src/adbasic/.gitignore index 68f19be6fa57a6c7077cc8c4b11ac155a984dd8c..01134cbd9a66ce612664259c7b58ae1c1a0a2911 100644 --- a/src/adbasic/.gitignore +++ b/src/adbasic/.gitignore @@ -1 +1,2 @@ *.TC1 +*.ERR diff --git a/src/adbasic/filter_module.inc b/src/adbasic/filter_module.inc index fd094d5224afca30731085030de94a3c0a5431ba..77f43c33c214ca5dc9b969fb3bd96d115b9cb29d 100644 --- a/src/adbasic/filter_module.inc +++ b/src/adbasic/filter_module.inc @@ -137,18 +137,20 @@ Function FilterModule(input_int, control, filter_index, aux) as Float EndIf ' Snap state that waits for aux signal - If ((control And FCR_SW_SNAP) = 1) Then + If ((control And FCR_SW_SNAP) > 0) Then filter_output = 0.0 - snap_value = aux - (snap_config[filter_index] And snap_value_mask) + snap_value = aux - (snap_config[filter_index+1] And snap_value_mask) - If ((snap_config[filter_index] And snap_lg) = 1) Then + If ((snap_config[filter_index+1] And snap_lg) > 0) Then snap_value = -1 * snap_value EndIf - If (snap_value >= 0) Then + If (snap_value <= 0) Then ' Disable snapping control = (control And Not(FCR_SW_SNAP)) ' Enable output control = (control Or FCR_SW_OUTPUT) + ' Stop ramp + rcr = rcr And Not(0fh) EndIf EndIf diff --git a/src/adwin_control/adwin-control.TC1 b/src/adwin_control/adwin-control.TC1 index 9530053a61f0619a77d93959d751350c204bc35e..086795be380eb54da8d81db882b301778617ef73 100755 Binary files a/src/adwin_control/adwin-control.TC1 and b/src/adwin_control/adwin-control.TC1 differ diff --git a/src/adwin_control/mockAdwin.py b/src/adwin_control/mockAdwin.py index c4dd668b3567103b715fe70f7cac6b5fe16cb65d..696aa94784149879ba9eeb0e8ec9729fc60d2b68 100644 --- a/src/adwin_control/mockAdwin.py +++ b/src/adwin_control/mockAdwin.py @@ -105,14 +105,15 @@ class MockADwin(): c = self.Get_Par(10 + channel) # read control bits auxSw = general.readBit(c, 9) + snapSw = general.readBit(c, 3) offsetSw = general.readBit(c, 2) outputSw = general.readBit(c, 1) inputSw = general.readBit(c, 0) - return inputSw, offsetSw, auxSw, outputSw + return inputSw, offsetSw, auxSw, outputSw, snapSw def _constructOutput(self, amount, input, aux): channel = self.Get_Par(4) - inputSw, offsetSw, auxSw, outputSw = self._readSwitches(channel) + inputSw, offsetSw, auxSw, outputSw, snapSw = self._readSwitches(channel) output = np.full(amount, 0x8000).astype(int) if not outputSw: return output @@ -123,6 +124,8 @@ class MockADwin(): output = output + offset gain = self.GetData_Double(2, channel, 1) output = (output - 0x8000) * gain + 0x8000 + if snapSw: + output = np.full(amount, 0x8000).astype(int) if auxSw: output = output + (aux - 0x8000) @@ -178,17 +181,18 @@ class MockADwin(): aux = 0x8000 for servo in range(1, 9): control = self._par[9+servo] - snap_enabled = general.readBit(control, 3) + snap_enabled = control & 8 if snap_enabled: snap_config = self.GetData_Long(7, servo, 1)[0] - snap_value = aux - (snap_config & 0x10000) - if control & 16 == 1: + snap_value = aux - (snap_config & 0xffff) + if (snap_config & 0x10000) > 0: snap_value *= -1 - if snap_value >= 0: - control = general.clearBit(control, 3) - control = general.setBit(control, 1) + if snap_value <= 0: + control = general.clearBit(control, 3) # disable snapSw + control = general.setBit(control, 1) # enable outputSw self._par[9+servo] = control + self._par[2] &= ~(15) # stop the ramp self.Set_Par diff --git a/src/adwin_control/servoDevice.py b/src/adwin_control/servoDevice.py index 153e9da8687eebc4298c28c3c428ffd22a1ac502..e42d58f45c9f7070d57884772fcea069a0fc4d29 100644 --- a/src/adwin_control/servoDevice.py +++ b/src/adwin_control/servoDevice.py @@ -44,7 +44,8 @@ class ServoDevice: def __init__(self, deviceNumber=None, readFromFile=None, - process=DEFAULT_PROCESS): + process=DEFAULT_PROCESS, + reboot=False): """Create a new ServoDevice object.""" raiseExceptions = 1 if deviceNumber == 0: @@ -59,7 +60,7 @@ class ServoDevice: self._monitors = [None] * self.NUMBER_OF_MONITORS try: - self._bootAdwin(process) + self._bootAdwin(process, reboot=reboot) except ADwinError as e: if e.errorNumber == 2001: log.warning('No device connected! Starting with mock device!') diff --git a/src/tests/test_servo.py b/src/tests/test_servo.py index 9d00e7ff5ebec6704035e76c12f88553ff980f8e..76254fe22db7410ad3badf030a475f2b213471c9 100644 --- a/src/tests/test_servo.py +++ b/src/tests/test_servo.py @@ -10,6 +10,7 @@ from math import pow from pandas import DataFrame import os import logging as log +import adwin_control from adwin_control.errors import * from tempfile import TemporaryDirectory from nqlab.analysis import ServoDesign @@ -19,12 +20,8 @@ log.basicConfig(format='%(levelname)s: %(module)s.%(funcName)s: %(message)s', le class TestConfiguration(unittest.TestCase): def test_run_develop_version(self): - import adwin_control if 'site-packages' in adwin_control.__path__[0]: - self.assertTrue(False) - log.critical('Not running the development code!!!') - import sys - sys.exit() + raise Exception('Not running the development code!!!') def test_user_config(self): RUNNING_IN_DOCKER = os.environ.get('RUNNING_IN_DOCKER', False) @@ -95,9 +92,13 @@ class TestVoltConvertion(unittest.TestCase): class TestServo(unittest.TestCase): def setUp(self): - self.sd = ServoDevice(settings.DEVICES_LIST[0]) - self.sd.reboot() + self.sd = ServoDevice(settings.DEVICES_LIST[0], reboot=True) + # self.sd.reboot() self.s = self.sd.servo(2) + import adwin_control + log.warning('adwin_control path: {}'.format(adwin_control.__path__[0])) + if 'site-packages' in adwin_control.__path__[0]: + raise Exception('Not running the development code!!!') def test_checkNumberAndChannel(self): self.assertEqual(self.s._channel, 2) @@ -458,6 +459,7 @@ class TestServo(unittest.TestCase): p.join() def test_check_input_offset(self): + self.s = self.sd.servo(8) self.s.fifoStepsize = 1 self.s._waitForBufferFilling() data = self.s._readoutNewData(self.s._fifo['maxlen']) @@ -519,15 +521,45 @@ class TestServo(unittest.TestCase): self.s.loadSettings('servo.json') self.assertEqual(servoDesign_str, self.s.servoDesign.__str__()) + def test_snapping_disables_output(self): + def out(): + return self.s._readoutNewData(10000)['output'].iloc[-1] + + self.s.enableFifo() + self.s.offset = 9 + self.s.offsetSw = True + self.s.inputSw = True + self.s.outputSw = True + sleep(.01) + self.assertGreater(out(), 2) + self.s.snapValue(5, True) + self.s.snapSw = True + sleep(.01) + self.assertEqual(out(), 0) + def test_enable_snapping(self): self.assertEqual(self.s._adw.Get_Par(12), 0) - self.s.snapValue(5, True) + self.s.snapValue(-5, False) self.s.snapSw = True self.assertEqual(self.s._adw.Get_Par(12), 8) # Check if it snaps self.s.snapValue(5, False) sleep(1e-3) self.assertEqual(self.s._adw.Get_Par(12), 2) + # Snap when signal is higher + self.s.snapValue(-5, True) + self.s.snapSw = True + sleep(1e-3) + self.assertEqual(self.s._adw.Get_Par(12), 2) + + def test_stop_ramp_when_snapping(self): + self.s.enableRamp(50, 10, False) + self.s.snapValue(5, False) + self.s.snapSw = True + # Check if it snaps + sleep(1e-3) + # Ramp stopped? + self.assertFalse(self.s.rampEnabled) def test_apply_old_settings(self): self.s.outputSw = True