Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Rethusan committed Oct 11, 2023
1 parent 885d3b6 commit 3c5bf11
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
31 changes: 16 additions & 15 deletions platform/panduza_platform/connectors/uart_ftdi_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ def __init__(self,**kwargs):
"""
# Init local mutex
self._mutex = asyncio.Lock()
self._mutex2 = asyncio.Lock()

key = kwargs["serial_port_name"]

Expand All @@ -100,23 +101,22 @@ async def connect(self):
"""Start the serial connection
"""

self.reader,_ = await serial_asyncio.open_serial_connection(loop = self.loop,url=self.port_name, baudrate=self.baudrate)
self.reader,self.writer = await serial_asyncio.open_serial_connection(loop = self.loop,url=self.port_name, baudrate=self.baudrate)

# We need to wait for the serial connection
await asyncio.sleep(4)



###########################################################################
###########################################################################


async def read_uart(self):
# async with self._mutex:
async with self._mutex:
try:

#await asyncio.sleep(1)
data = await asyncio.wait_for(self.reader.readuntil(b'\n'), timeout=2.0)
decoded_data = data.decode('utf-8').strip()

return decoded_data

except asyncio.TimeoutError as e:
Expand All @@ -127,30 +127,31 @@ async def read_uart(self):
###########################################################################
###########################################################################


async def write_uart(self,message):
#async with self._mutex:
_, self.writer = await serial_asyncio.open_serial_connection(loop = self.loop,url=self.port_name, baudrate=self.baudrate)
#await asyncio.sleep(0.3)
self.writer.write(message.encode())
await self.writer.drain()
self.writer.close()
async with self._mutex:
try:

self.writer.write(message.encode())
await self.writer.drain()
except Exception as e:
raise Exception('Error during writing to uart').with_traceback(e.__traceback__)


###########################################################################
###########################################################################

async def write_read_uart(self,message):
async with self._mutex:

async with self._mutex2:
await self.write_uart(message)
data = await self.read_uart()

return data







###########################################################################
# SERIAL SYNCHRONOUS
###########################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ async def _PZA_DRV_loop_init(self, loop, tree):

# Get the gate connector
self.uart_connector = await ConnectorUartFtdiSerial.Get(**settings)

# We need to wait for the serial connection
await asyncio.sleep(2.2)

self.__fakes = {
Expand All @@ -58,27 +60,27 @@ async def _PZA_DRV_SERVOMOTOR_get_position_value(self):

response = await self.uart_connector.write_read_uart(f"get {self.servo_id}")
self.__fakes["position"]["value"] = int(response)

#self.__fakes["position"]["value"] = await self.__round_value(self.__fakes["position"]["value"])
self.__fakes["position"]["value"] = await self.change_to_even_nuber(self.__fakes["position"]["value"])

return self.__fakes["position"]["value"]



async def _PZA_DRV_SERVOMOTOR_set_position_value(self,value):

#value = await self.__round_value(value)
print(f"command set {self.servo_id} {value}")
# print(f"command set {self.servo_id} {value}")
await self.uart_connector.write_read_uart(f"set {self.servo_id} {value}")

self.__fakes["position"]["value"] = value


async def __round_value(self,value):
return ((value + 2) // 5) * 5





# async def __round_value(self,value):
# return ((value + 2) // 5) * 5

async def change_to_even_nuber(self,number):
if number % 2 != 0:
number += 1
return number
# async def change_to_even_nuber(self,number):
# if number % 2 != 0:
# number += 1
# return number
4 changes: 2 additions & 2 deletions platform/panduza_platform/meta_drivers/servomotor.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ async def __polling_task(self):
while self.alive:
await asyncio.sleep(self.__polling_cycle)

#await self._update_attribute("position", "value", await self._PZA_DRV_SERVOMOTOR_get_position_value(), 'always')
await self._update_attribute("position", "value", await self._PZA_DRV_SERVOMOTOR_get_position_value(), 'on-change')
await self._update_attribute("position", "value", await self._PZA_DRV_SERVOMOTOR_get_position_value(), 'always')
#await self._update_attribute("position", "value", await self._PZA_DRV_SERVOMOTOR_get_position_value(), 'on-change')

# await self._update_attributes_from_dict({
# "position": {
Expand Down

0 comments on commit 3c5bf11

Please sign in to comment.