Advertisement
Guest User

Untitled

a guest
Dec 9th, 2021
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. async def recv_messages(self):
  2.  
  3. # read updated write-pointer
  4. self.widx = self.ddr_widx.value
  5. assert self.widx < self.NUM_MSGS # sanity-check
  6.  
  7. position = 0
  8. force = 0
  9. timestamp = 0
  10. # process messages
  11.  
  12. while self.ridx != self.widx:
  13. # note: it may be faster to copy a batch of messages from shared memory
  14. # instead of directly accessing individual messages and their fields.
  15. msg = self.msgbuf[ self.ridx ]
  16. # sanity-check that message id increments monotonically
  17. self.lastid = ( self.lastid + 1 ) & 0xffffffff
  18. assert msg.id == self.lastid
  19. # get 32-bit timestamp (in cycles) from message and unwrap it:
  20. self.timestamp_cycles += ( msg.timestamp - self.timestamp_cycles ) & 0xfffffff
  21. # convert to timestamp in seconds
  22. position = uint32(msg.position)
  23. angle = self._Count_to_angle(position)
  24. force_adj = self._Force_correction(angle)
  25. force = msg.force - force_adj
  26. timestamp_ms = self.timestamp_cycles // 200000
  27. timestamp_s = ( timestamp_ms % 60000 ) / 1000
  28. timestamp_m = timestamp_ms // 60000
  29. timestamp_h = timestamp_m // 60
  30. timestamp_m = timestamp_m % 60
  31. timestamp_label = f'{timestamp_h:02d}:{timestamp_m:02d}:{timestamp_s:06.3f}'
  32. time = timestamp_s + timestamp_m*60 + timestamp_h *360
  33. val = [time, timestamp_label, force, angle]
  34. self.data_queue.put_nowait(val)
  35. # consume message and update read pointe
  36. del msg
  37. # direct access to message forbidden beyond this poin
  38. self.ridx += 1
  39. if (self.ridx == self.NUM_MSGS):
  40. self.ridx = 0
  41. self.shmem.ridx = self.ridx
  42. #update user interface
  43. print(f'\ridx=0x{self.ridx:04x} id=0x{self.lastid:08x} time={timestamp_label} angle={angle} force={force:08d}', end='', flush=True )
  44.  
  45.  
  46. async def data_emitter(self,reader, writer):
  47. print('emitting data active')
  48. delay = 0.0001
  49. terminator = bytes("\r",'ascii')
  50. seperator = bytes(',','ascii')
  51. while (self.shut_down != True):
  52. val_list = []
  53. data = bytes('','ascii')
  54. if not (self.data_queue.empty()):
  55. val_list = self.data_queue.get_nowait()
  56. for i in range (0,len(val_list)):
  57. val = str(val_list[i]).encode('ascii')
  58. data += val + seperator
  59. data += terminator
  60. writer.write(data)
  61. await writer.drain()
  62. delay = 0.0001
  63. await asyncio.sleep(delay)
  64. if (delay <= 0.1):
  65. delay += 0.001
  66. print("Close the client socket")
  67. writer.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement