Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async def recv_messages(self):
- # read updated write-pointer
- self.widx = self.ddr_widx.value
- assert self.widx < self.NUM_MSGS # sanity-check
- position = 0
- force = 0
- timestamp = 0
- # process messages
- while self.ridx != self.widx:
- # note: it may be faster to copy a batch of messages from shared memory
- # instead of directly accessing individual messages and their fields.
- msg = self.msgbuf[ self.ridx ]
- # sanity-check that message id increments monotonically
- self.lastid = ( self.lastid + 1 ) & 0xffffffff
- assert msg.id == self.lastid
- # get 32-bit timestamp (in cycles) from message and unwrap it:
- self.timestamp_cycles += ( msg.timestamp - self.timestamp_cycles ) & 0xfffffff
- # convert to timestamp in seconds
- position = uint32(msg.position)
- angle = self._Count_to_angle(position)
- force_adj = self._Force_correction(angle)
- force = msg.force - force_adj
- timestamp_ms = self.timestamp_cycles // 200000
- timestamp_s = ( timestamp_ms % 60000 ) / 1000
- timestamp_m = timestamp_ms // 60000
- timestamp_h = timestamp_m // 60
- timestamp_m = timestamp_m % 60
- timestamp_label = f'{timestamp_h:02d}:{timestamp_m:02d}:{timestamp_s:06.3f}'
- time = timestamp_s + timestamp_m*60 + timestamp_h *360
- val = [time, timestamp_label, force, angle]
- self.data_queue.put_nowait(val)
- # consume message and update read pointe
- del msg
- # direct access to message forbidden beyond this poin
- self.ridx += 1
- if (self.ridx == self.NUM_MSGS):
- self.ridx = 0
- self.shmem.ridx = self.ridx
- #update user interface
- print(f'\ridx=0x{self.ridx:04x} id=0x{self.lastid:08x} time={timestamp_label} angle={angle} force={force:08d}', end='', flush=True )
- async def data_emitter(self,reader, writer):
- print('emitting data active')
- delay = 0.0001
- terminator = bytes("\r",'ascii')
- seperator = bytes(',','ascii')
- while (self.shut_down != True):
- val_list = []
- data = bytes('','ascii')
- if not (self.data_queue.empty()):
- val_list = self.data_queue.get_nowait()
- for i in range (0,len(val_list)):
- val = str(val_list[i]).encode('ascii')
- data += val + seperator
- data += terminator
- writer.write(data)
- await writer.drain()
- delay = 0.0001
- await asyncio.sleep(delay)
- if (delay <= 0.1):
- delay += 0.001
- print("Close the client socket")
- writer.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement