Skip to content

Commit 306ec82

Browse files
committed
Use time.monotonic() replace time.time()
1 parent 408a3d9 commit 306ec82

File tree

17 files changed

+91
-91
lines changed

17 files changed

+91
-91
lines changed

example/wrapper/common/8000-load_identify_current.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ def progress(item):
5050

5151

5252
arm.register_iden_progress_changed_callback(progress)
53-
start_time = time.time()
53+
start_time = time.monotonic()
5454
code, result = arm.iden_tcp_load()
55-
end_time = time.time()
55+
end_time = time.monotonic()
5656
print('code={}, result={}, cost_time={}'.format(code, result, end_time - start_time))
5757
arm.release_iden_progress_changed_callback(progress)
5858
arm.disconnect()

example/wrapper/common/8004-load_identify.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ def progress(item):
5050

5151

5252
arm.register_iden_progress_changed_callback(progress)
53-
start_time = time.time()
53+
start_time = time.monotonic()
5454
arm.ft_sensor_enable(1)
5555
code, result = arm.ft_sensor_iden_load()
56-
end_time = time.time()
56+
end_time = time.monotonic()
5757
print('code={}, result={}, cost_time={}'.format(code, result, end_time - start_time))
5858
arm.release_iden_progress_changed_callback(progress)
5959
arm.ft_sensor_app_set(0)

xarm/core/comm/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def recv_report_proc(self):
196196

197197
# # buffer[494:502]
198198
# data_curr_us = convert.bytes_to_u64(buffer[-8:])
199-
# recv_curr_us = time.time() * 1000000
199+
# recv_curr_us = time.monotonic() * 1000000
200200
#
201201
# if data_prev_us != 0 and recv_prev_us != 0:
202202
# data_interval_us = data_curr_us - data_prev_us

xarm/core/wrapper/uxbus_cmd.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ def __init__(self):
3737
self.lock = threading.Lock()
3838
self._GET_TIMEOUT = XCONF.UxbusConf.GET_TIMEOUT / 1000
3939
self._SET_TIMEOUT = XCONF.UxbusConf.SET_TIMEOUT / 1000
40-
self._last_comm_time = time.time()
41-
self._last_modbus_comm_time = time.time()
40+
self._last_comm_time = time.monotonic()
41+
self._last_modbus_comm_time = time.monotonic()
4242

4343
@property
4444
def last_comm_time(self):
@@ -732,16 +732,16 @@ def tgpio_set_modbus(self, modbus_t, len_t, host_id=XCONF.TGPIO_HOST_ID, limit_s
732732
txdata = bytes([host_id])
733733
txdata += bytes(modbus_t)
734734
if limit_sec > 0:
735-
diff_time = time.time() - self._last_modbus_comm_time
735+
diff_time = time.monotonic() - self._last_modbus_comm_time
736736
if diff_time < limit_sec:
737737
time.sleep(limit_sec - diff_time)
738738
ret = self.send_xbus(XCONF.UxbusReg.TGPIO_MODBUS, txdata, len_t + 1)
739739
if ret != 0:
740-
self._last_modbus_comm_time = time.time()
740+
self._last_modbus_comm_time = time.monotonic()
741741
return [XCONF.UxbusState.ERR_NOTTCP] * (7 + 1)
742742

743743
ret = self.send_pend(XCONF.UxbusReg.TGPIO_MODBUS, -1, self._GET_TIMEOUT)
744-
self._last_modbus_comm_time = time.time()
744+
self._last_modbus_comm_time = time.monotonic()
745745
return ret
746746

747747
@lock_require

xarm/core/wrapper/uxbus_cmd_ser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ def check_xbus_prot(self, data, funcode=0):
6161

6262
def send_pend(self, funcode, num, timeout):
6363
ret = [0] * 254 if num == -1 else [0] * (num + 1)
64-
expired = time.time() + timeout
64+
expired = time.monotonic() + timeout
6565
ret[0] = XCONF.UxbusState.ERR_TOUT
66-
while time.time() < expired:
67-
remaining = expired - time.time()
66+
while time.monotonic() < expired:
67+
remaining = expired - time.monotonic()
6868
rx_data = self.arm_port.read(remaining)
6969
if rx_data != -1 and len(rx_data) > 5:
7070
if self._debug:

xarm/core/wrapper/uxbus_cmd_tcp.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(self, arm_port):
3737
self.prot_flag = TX2_PROT_CON
3838
self.TX2_PROT_CON = TX2_PROT_CON
3939
self._has_err_warn = False
40-
self._last_comm_time = time.time()
40+
self._last_comm_time = time.monotonic()
4141

4242
@property
4343
def has_err_warn(self):
@@ -95,12 +95,12 @@ def check_xbus_prot(self, data, funcode):
9595
def send_pend(self, funcode, num, timeout):
9696
ret = [0] * 320 if num == -1 else [0] * (num + 1)
9797
ret[0] = XCONF.UxbusState.ERR_TOUT
98-
expired = time.time() + timeout
99-
while time.time() < expired:
100-
remaining = expired - time.time()
98+
expired = time.monotonic() + timeout
99+
while time.monotonic() < expired:
100+
remaining = expired - time.monotonic()
101101
rx_data = self.arm_port.read(remaining)
102102
if rx_data != -1 and len(rx_data) > 7:
103-
self._last_comm_time = time.time()
103+
self._last_comm_time = time.monotonic()
104104
if self._debug:
105105
debug_log_datas(rx_data, label='recv({})'.format(funcode))
106106
ret[0] = self.check_xbus_prot(rx_data, funcode)

xarm/tools/blockly/_blockly_handler.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -785,11 +785,11 @@ def _handle_controls_repeat_ext(self, block, indent=0, arg_map=None):
785785
statement = self._get_node('statement', root=block)
786786
if statement:
787787
if self._loop_interval_sec > 0:
788-
self._append_main_code(' t1 = time.time()', indent=indent+2)
788+
self._append_main_code(' t1 = time.monotonic()', indent=indent+2)
789789
self._parse_block(statement, indent+1, arg_map=arg_map)
790790
if self._loop_interval_sec > 0:
791791
# limit frequency
792-
self._append_main_code(' interval = time.time() - t1', indent=indent+2)
792+
self._append_main_code(' interval = time.monotonic() - t1', indent=indent+2)
793793
self._append_main_code(' if interval < {}:'.format(self._loop_interval_sec), indent=indent+2)
794794
self._append_main_code(' time.sleep({} - interval)'.format(self._loop_interval_sec), indent=indent+2)
795795
else:
@@ -811,11 +811,11 @@ def _handle_controls_whileUntil(self, block, indent=0, arg_map=None):
811811
statement = self._get_node('statement', root=block)
812812
if statement:
813813
if self._loop_interval_sec > 0:
814-
self._append_main_code(' t1 = time.time()', indent=indent+2)
814+
self._append_main_code(' t1 = time.monotonic()', indent=indent+2)
815815
self._parse_block(statement, indent+1, arg_map=arg_map)
816816
if self._loop_interval_sec > 0:
817817
# limit frequency
818-
self._append_main_code(' interval = time.time() - t1', indent=indent+2)
818+
self._append_main_code(' interval = time.monotonic() - t1', indent=indent+2)
819819
self._append_main_code(' if interval < {}:'.format(self._loop_interval_sec), indent=indent+2)
820820
self._append_main_code(' time.sleep({} - interval)'.format(self._loop_interval_sec), indent=indent+2)
821821
else:
@@ -829,11 +829,11 @@ def _handle_loop_run_forever(self, block, indent=0, arg_map=None):
829829
statement = self._get_node('statement', root=block)
830830
if statement:
831831
if self._loop_interval_sec > 0:
832-
self._append_main_code(' t1 = time.time()', indent=indent+2)
832+
self._append_main_code(' t1 = time.monotonic()', indent=indent+2)
833833
self._parse_block(statement, indent+1, arg_map=arg_map)
834834
if self._loop_interval_sec > 0:
835835
# limit frequency
836-
self._append_main_code(' interval = time.time() - t1', indent=indent+2)
836+
self._append_main_code(' interval = time.monotonic() - t1', indent=indent+2)
837837
self._append_main_code(' if interval < {}:'.format(self._loop_interval_sec), indent=indent+2)
838838
self._append_main_code(' time.sleep({} - interval)'.format(self._loop_interval_sec), indent=indent+2)
839839
else:

xarm/tools/blockly_tool-bak.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,8 @@ def _handle_set_bio_gripper_init(self, block, prefix=''):
786786
self._append_to_file('{}# set_bio_gripper_enable(True)'.format(prefix))
787787
self._append_to_file('{}if not params[\'quit\'] and arm.set_bio_gripper_enable(True) != 0:'.format(prefix))
788788
self._append_to_file('{} params[\'quit\'] = True'.format(prefix))
789-
self._append_to_file('{}expired = time.time() + 2'.format(prefix))
790-
self._append_to_file('{}while not params[\'quit\'] and time.time() < expired:'.format(prefix))
789+
self._append_to_file('{}expired = time.monotonic() + 2'.format(prefix))
790+
self._append_to_file('{}while not params[\'quit\'] and time.monotonic() < expired:'.format(prefix))
791791
self._append_to_file('{} time.sleep(0.1)'.format(prefix))
792792

793793
def _handle_set_bio_gripper(self, block, prefix=''):

xarm/tools/blockly_tool.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,8 @@ def _handle_set_bio_gripper_init(self, block, prefix='', arg_map=None):
988988
self._append_to_file('{} if code != 0:'.format(prefix))
989989
self._append_to_file('{} params[\'quit\'] = True'.format(prefix))
990990
self._append_to_file('{} pprint(\'set_bio_gripper_enable, code={{}}\'.format(code))'.format(prefix))
991-
# self._append_to_file('{}expired = time.time() + 2'.format(prefix))
992-
# self._append_to_file('{}while not params[\'quit\'] and time.time() < expired:'.format(prefix))
991+
# self._append_to_file('{}expired = time.monotonic() + 2'.format(prefix))
992+
# self._append_to_file('{}while not params[\'quit\'] and time.monotonic() < expired:'.format(prefix))
993993
# self._append_to_file('{} time.sleep(0.1)'.format(prefix))
994994

995995
def _handle_set_bio_gripper(self, block, prefix='', arg_map=None):
@@ -1427,10 +1427,10 @@ def _handle_controls_repeat_ext(self, block, prefix='', arg_map=None):
14271427
statement = self.get_node('statement', root=block)
14281428
if statement:
14291429
if self._highlight_callback:
1430-
self._append_to_file('{}t1 = time.time()'.format(prefix))
1430+
self._append_to_file('{}t1 = time.monotonic()'.format(prefix))
14311431
self.parse(statement, prefix, arg_map=arg_map)
14321432
if self._highlight_callback:
1433-
self._append_to_file('{}interval = time.time() - t1'.format(prefix))
1433+
self._append_to_file('{}interval = time.monotonic() - t1'.format(prefix))
14341434
self._append_to_file('{}if interval < 0.001:'.format(prefix))
14351435
self._append_to_file('{} time.sleep(0.001 - interval)'.format(prefix))
14361436
else:
@@ -1453,10 +1453,10 @@ def _handle_controls_whileUntil(self, block, prefix='', arg_map=None):
14531453
statement = self.get_node('statement', root=block)
14541454
if statement:
14551455
if self._highlight_callback:
1456-
self._append_to_file('{}t1 = time.time()'.format(prefix))
1456+
self._append_to_file('{}t1 = time.monotonic()'.format(prefix))
14571457
self.parse(statement, prefix, arg_map=arg_map)
14581458
if self._highlight_callback:
1459-
self._append_to_file('{}interval = time.time() - t1'.format(prefix))
1459+
self._append_to_file('{}interval = time.monotonic() - t1'.format(prefix))
14601460
self._append_to_file('{}if interval < 0.001:'.format(prefix))
14611461
self._append_to_file('{} time.sleep(0.001 - interval)'.format(prefix))
14621462
else:
@@ -1470,10 +1470,10 @@ def _handle_loop_run_forever(self, block, prefix='', arg_map=None):
14701470
statement = self.get_node('statement', root=block)
14711471
if statement:
14721472
if self._highlight_callback:
1473-
self._append_to_file('{}t1 = time.time()'.format(prefix))
1473+
self._append_to_file('{}t1 = time.monotonic()'.format(prefix))
14741474
self.parse(statement, prefix, arg_map=arg_map)
14751475
if self._highlight_callback:
1476-
self._append_to_file('{}interval = time.time() - t1'.format(prefix))
1476+
self._append_to_file('{}interval = time.monotonic() - t1'.format(prefix))
14771477
self._append_to_file('{}if interval < 0.001:'.format(prefix))
14781478
self._append_to_file('{} time.sleep(0.001 - interval)'.format(prefix))
14791479
else:

xarm/tools/blockly_tool_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,8 +910,8 @@ def _handle_set_bio_gripper_init(self, block, prefix=''):
910910
self._append_to_file('{}# set_bio_gripper_enable(True)'.format(prefix))
911911
self._append_to_file('{}if not params[\'quit\'] and arm.set_bio_gripper_enable(True) != 0:'.format(prefix))
912912
self._append_to_file('{} params[\'quit\'] = True'.format(prefix))
913-
self._append_to_file('{}expired = time.time() + 2'.format(prefix))
914-
self._append_to_file('{}while not params[\'quit\'] and time.time() < expired:'.format(prefix))
913+
self._append_to_file('{}expired = time.monotonic() + 2'.format(prefix))
914+
self._append_to_file('{}while not params[\'quit\'] and time.monotonic() < expired:'.format(prefix))
915915
self._append_to_file('{} time.sleep(0.1)'.format(prefix))
916916

917917
def _handle_set_bio_gripper(self, block, prefix=''):

0 commit comments

Comments
 (0)