ArtusAPI 0.5.0__tar.gz → 1.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- artusapi-1.0.1/ArtusAPI/artus_api.py +250 -0
- artusapi-1.0.1/ArtusAPI/commands/commands.py +171 -0
- artusapi-1.0.1/ArtusAPI/communication/UART/uart.py +104 -0
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/ArtusAPI/communication/WiFi/wifi_server.py +12 -47
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/ArtusAPI/communication/communication.py +53 -13
- artusapi-1.0.1/ArtusAPI/firmware_update/__init__.py +1 -0
- artusapi-1.0.1/ArtusAPI/firmware_update/firmware_updater.py +129 -0
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/ArtusAPI/robot/artus_lite/artus_lite.py +25 -19
- artusapi-1.0.1/ArtusAPI.egg-info/PKG-INFO +5 -0
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/ArtusAPI.egg-info/SOURCES.txt +3 -2
- artusapi-1.0.1/PKG-INFO +5 -0
- artusapi-1.0.1/README.md +152 -0
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/setup.py +4 -3
- ArtusAPI-0.5.0/ArtusAPI/artus_api.py +0 -136
- ArtusAPI-0.5.0/ArtusAPI/commands/commands.py +0 -102
- ArtusAPI-0.5.0/ArtusAPI/communication/UART/uart.py +0 -83
- ArtusAPI-0.5.0/ArtusAPI.egg-info/PKG-INFO +0 -4
- ArtusAPI-0.5.0/PKG-INFO +0 -4
- ArtusAPI-0.5.0/README.md +0 -34
- ArtusAPI-0.5.0/test/test.py +0 -0
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/ArtusAPI/__init__.py +0 -0
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/ArtusAPI/commands/__init__.py +0 -0
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/ArtusAPI/communication/UART/__init__.py +0 -0
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/ArtusAPI/communication/WiFi/__init__.py +0 -0
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/ArtusAPI/communication/__init__.py +0 -0
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/ArtusAPI/robot/__init__.py +0 -0
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/ArtusAPI/robot/artus_lite/__init__.py +0 -0
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/ArtusAPI/robot/artus_lite/artus_lite_left.py +0 -0
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/ArtusAPI/robot/artus_lite/artus_lite_right.py +0 -0
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/ArtusAPI/robot/robot.py +0 -0
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/ArtusAPI.egg-info/dependency_links.txt +0 -0
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/ArtusAPI.egg-info/top_level.txt +0 -0
- {ArtusAPI-0.5.0 → artusapi-1.0.1}/setup.cfg +0 -0
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
|
|
2
|
+
import sys
|
|
3
|
+
import logging
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
# Current file's directory
|
|
6
|
+
current_file_path = Path(__file__).resolve()
|
|
7
|
+
# Add the desired path to the system path
|
|
8
|
+
desired_path = current_file_path.parent.parent
|
|
9
|
+
sys.path.append(str(desired_path))
|
|
10
|
+
print(desired_path)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
from .communication import Communication
|
|
15
|
+
from .commands import Commands
|
|
16
|
+
from .robot import Robot
|
|
17
|
+
from .firmware_update import FirmwareUpdater
|
|
18
|
+
import time
|
|
19
|
+
|
|
20
|
+
class ArtusAPI:
|
|
21
|
+
|
|
22
|
+
def __init__(self,
|
|
23
|
+
# communication
|
|
24
|
+
communication_method='UART',
|
|
25
|
+
communication_channel_identifier='COM9',
|
|
26
|
+
# robot
|
|
27
|
+
robot_type='artus_lite',
|
|
28
|
+
hand_type ='left',
|
|
29
|
+
stream = False,
|
|
30
|
+
communication_frequency = 400, # hz
|
|
31
|
+
logger = None,
|
|
32
|
+
reset_on_start = 0,
|
|
33
|
+
baudrate = 921600
|
|
34
|
+
):
|
|
35
|
+
"""
|
|
36
|
+
ArtusAPI class controls the communication and control of between a system and an Artus Hand by Sarcomere Dynamics Inc.
|
|
37
|
+
:communication_method: communication method that is supported on the Artus Hand
|
|
38
|
+
:communication_channel_identifier: channel identifier for the communication method
|
|
39
|
+
:robot_type: name of the series of robot hand
|
|
40
|
+
:hand_type: left or right
|
|
41
|
+
:stream: streaming feedback data
|
|
42
|
+
:communication_frequency: maximum frequency to stream data
|
|
43
|
+
:logger: python logger settings to inherit
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
self._communication_handler = Communication(communication_method=communication_method,
|
|
47
|
+
communication_channel_identifier=communication_channel_identifier,baudrate=baudrate)
|
|
48
|
+
self._command_handler = Commands(reset_on_start=reset_on_start)
|
|
49
|
+
self._robot_handler = Robot(robot_type = robot_type,
|
|
50
|
+
hand_type = hand_type)
|
|
51
|
+
|
|
52
|
+
self._last_command_sent_time = time.perf_counter()
|
|
53
|
+
self._communication_frequency = 1 / communication_frequency
|
|
54
|
+
self._communication_frequency_us = int(self._communication_frequency * 1000000)
|
|
55
|
+
self.stream = stream
|
|
56
|
+
|
|
57
|
+
if not logger:
|
|
58
|
+
self.logger = logging.getLogger(__name__)
|
|
59
|
+
else:
|
|
60
|
+
self.logger = logger
|
|
61
|
+
|
|
62
|
+
# communication setup
|
|
63
|
+
def connect(self):
|
|
64
|
+
"""
|
|
65
|
+
Open a connection to the Artus Hand
|
|
66
|
+
"""
|
|
67
|
+
self._communication_handler.open_connection()
|
|
68
|
+
|
|
69
|
+
time.sleep(1)
|
|
70
|
+
# send wake command with it
|
|
71
|
+
return self.wake_up()
|
|
72
|
+
|
|
73
|
+
def disconnect(self):
|
|
74
|
+
"""
|
|
75
|
+
Close a connection to the Artus Hand
|
|
76
|
+
"""
|
|
77
|
+
return self._communication_handler.close_connection()
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
# robot states
|
|
83
|
+
def wake_up(self):
|
|
84
|
+
"""
|
|
85
|
+
Wake-up the Artus Hand
|
|
86
|
+
"""
|
|
87
|
+
print(f"communication frequency in useconds = {self._communication_frequency_us}")
|
|
88
|
+
robot_wake_up_command = self._command_handler.get_robot_start_command(self.stream,self._communication_frequency_us) # to ms for masterboard
|
|
89
|
+
self._communication_handler.send_data(robot_wake_up_command)
|
|
90
|
+
|
|
91
|
+
# wait for data back
|
|
92
|
+
if self._communication_handler.wait_for_ack():
|
|
93
|
+
self.logger.info(f'Finished calibration')
|
|
94
|
+
else:
|
|
95
|
+
self.logger.warning(f'Error in calibration')
|
|
96
|
+
|
|
97
|
+
def sleep(self):
|
|
98
|
+
"""
|
|
99
|
+
Sleep the Artus Hand
|
|
100
|
+
"""
|
|
101
|
+
robot_sleep_command = self._command_handler.get_sleep_command()
|
|
102
|
+
return self._communication_handler.send_data(robot_sleep_command)
|
|
103
|
+
def calibrate(self):
|
|
104
|
+
"""
|
|
105
|
+
Calibrate the Artus Hand
|
|
106
|
+
"""
|
|
107
|
+
robot_calibrate_command = self._command_handler.get_calibration_command()
|
|
108
|
+
self._communication_handler.send_data(robot_calibrate_command)
|
|
109
|
+
|
|
110
|
+
# wait for data back
|
|
111
|
+
if self._communication_handler.wait_for_ack():
|
|
112
|
+
self.logger.info(f'Finished calibration')
|
|
113
|
+
else:
|
|
114
|
+
self.logger.warning(f'Error in calibration')
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
# robot control
|
|
118
|
+
def set_joint_angles(self, joint_angles:dict):
|
|
119
|
+
"""
|
|
120
|
+
Set joint angle targets and speed values to the Artus Hand
|
|
121
|
+
:joint_angles: dictionary of input angles and input speeds
|
|
122
|
+
"""
|
|
123
|
+
self._robot_handler.set_joint_angles(joint_angles=joint_angles,name=False)
|
|
124
|
+
robot_set_joint_angles_command = self._command_handler.get_target_position_command(self._robot_handler.robot.hand_joints)
|
|
125
|
+
# check communication frequency
|
|
126
|
+
if not self._check_communication_frequency():
|
|
127
|
+
return False
|
|
128
|
+
return self._communication_handler.send_data(robot_set_joint_angles_command)
|
|
129
|
+
|
|
130
|
+
def set_home_position(self):
|
|
131
|
+
"""
|
|
132
|
+
sends the joints to home positions (0) which opens the Artus Hand
|
|
133
|
+
"""
|
|
134
|
+
self._robot_handler.set_home_position()
|
|
135
|
+
robot_set_home_position_command = self._command_handler.get_target_position_command(hand_joints=self._robot_handler.robot.hand_joints)
|
|
136
|
+
# check communication frequency
|
|
137
|
+
if not self._check_communication_frequency():
|
|
138
|
+
return False
|
|
139
|
+
return self._communication_handler.send_data(robot_set_home_position_command)
|
|
140
|
+
|
|
141
|
+
def _check_communication_frequency(self):
|
|
142
|
+
"""
|
|
143
|
+
check if the communication frequency is too high
|
|
144
|
+
"""
|
|
145
|
+
current_time = time.perf_counter()
|
|
146
|
+
if current_time - self._last_command_sent_time < self._communication_frequency:
|
|
147
|
+
self.logger.warning("Command not sent. Communication frequency is too high.")
|
|
148
|
+
return False
|
|
149
|
+
self._last_command_sent_time = current_time
|
|
150
|
+
return True
|
|
151
|
+
|
|
152
|
+
# robot feedback
|
|
153
|
+
def _receive_feedback(self):
|
|
154
|
+
feedback_command = self._command_handler.get_states_command()
|
|
155
|
+
self._communication_handler.send_data(feedback_command)
|
|
156
|
+
# test
|
|
157
|
+
time.sleep(0.005)
|
|
158
|
+
return self._communication_handler.receive_data()
|
|
159
|
+
|
|
160
|
+
def get_joint_angles(self):
|
|
161
|
+
"""
|
|
162
|
+
Populate feedback fields in self._robot_handler.hand_joints dict
|
|
163
|
+
"""
|
|
164
|
+
feedback_command = self._receive_feedback()
|
|
165
|
+
joint_angles = self._robot_handler.get_joint_angles(feedback_command)
|
|
166
|
+
print(joint_angles)
|
|
167
|
+
return joint_angles
|
|
168
|
+
|
|
169
|
+
# robot feedback stream
|
|
170
|
+
def get_streamed_joint_angles(self):
|
|
171
|
+
"""
|
|
172
|
+
Populate feedback fields in self._robot_handler.hand_joints dict
|
|
173
|
+
"""
|
|
174
|
+
if not self._check_communication_frequency():
|
|
175
|
+
return False
|
|
176
|
+
feedback_command = self._communication_handler.receive_data()
|
|
177
|
+
if not feedback_command:
|
|
178
|
+
return None
|
|
179
|
+
joint_angles = self._robot_handler.get_joint_angles(feedback_command)
|
|
180
|
+
return joint_angles
|
|
181
|
+
|
|
182
|
+
def update_firmware(self):
|
|
183
|
+
file_path = None
|
|
184
|
+
fw_size = 0
|
|
185
|
+
# input to upload a new file
|
|
186
|
+
upload_flag = input(f'Uploading a new BIN file? (y/n) : ')
|
|
187
|
+
upload = True
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
self._firmware_updater = FirmwareUpdater(self._communication_handler,
|
|
191
|
+
self._command_handler)
|
|
192
|
+
|
|
193
|
+
if upload_flag == 'n' or upload_flag == 'N':
|
|
194
|
+
self._firmware_updater.file_location = 'not empty'
|
|
195
|
+
upload = False
|
|
196
|
+
else:
|
|
197
|
+
|
|
198
|
+
self._firmware_updater.file_location = input('Please enter binfile absolute path: ')
|
|
199
|
+
|
|
200
|
+
fw_size = self._firmware_updater.get_bin_file_info()
|
|
201
|
+
|
|
202
|
+
# set which drivers to flash should be 1-8
|
|
203
|
+
drivers_to_flash = int(input(f'Which drivers would you like to flash? \n0:ALL\nor 1-8\n: '))
|
|
204
|
+
if not drivers_to_flash:
|
|
205
|
+
drivers_to_flash = 0
|
|
206
|
+
|
|
207
|
+
firmware_command = self._command_handler.get_firmware_command(fw_size,upload,drivers_to_flash)
|
|
208
|
+
self._communication_handler.send_data(firmware_command)
|
|
209
|
+
if upload:
|
|
210
|
+
self._firmware_updater.update_firmware(fw_size)
|
|
211
|
+
|
|
212
|
+
print(f'File size = {fw_size}')
|
|
213
|
+
print(f'flashing...')
|
|
214
|
+
self._communication_handler.wait_for_ack()
|
|
215
|
+
print(f'Power Cycle the device to take effect')
|
|
216
|
+
|
|
217
|
+
def reset(self):
|
|
218
|
+
j = int(input(f'Enter Joint to reset: '))
|
|
219
|
+
m = int(input(f'Enter Motor to reset: '))
|
|
220
|
+
reset_command = self._command_handler.get_locked_reset_low_command(j,m)
|
|
221
|
+
self._communication_handler.send_data(reset_command)
|
|
222
|
+
|
|
223
|
+
def hard_close(self):
|
|
224
|
+
j = int(input(f'Enter Joint to reset: '))
|
|
225
|
+
m = int(input(f'Enter Motor to reset: '))
|
|
226
|
+
hard_close = self._command_handler.get_hard_close_command(j,m)
|
|
227
|
+
self._communication_handler.send_data(hard_close)
|
|
228
|
+
|
|
229
|
+
def update_param(self):
|
|
230
|
+
com = input('Enter Communication Protocol you would like to change to (default: UART, CAN, RS485): ')
|
|
231
|
+
if com == 'CAN':
|
|
232
|
+
feed = None
|
|
233
|
+
while feed not in ['P','C','ALL']:
|
|
234
|
+
feed = input('Enter feedback information (P: Positions only, C: Positions and Force, ALL: Position, force and temperature): ')
|
|
235
|
+
else:
|
|
236
|
+
feed = None
|
|
237
|
+
command = self._command_handler.update_param_command(com,feed)
|
|
238
|
+
self._communication_handler.send_data(command)
|
|
239
|
+
|
|
240
|
+
def test_artus_api():
|
|
241
|
+
artus_api = ArtusAPI()
|
|
242
|
+
artus_api.connect()
|
|
243
|
+
artus_api.wake_up()
|
|
244
|
+
artus_api.calibrate()
|
|
245
|
+
artus_api.set_home_position()
|
|
246
|
+
time.sleep(2)
|
|
247
|
+
artus_api.disconnect()
|
|
248
|
+
|
|
249
|
+
if __name__ == "__main__":
|
|
250
|
+
test_artus_api()
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import json
|
|
3
|
+
import time
|
|
4
|
+
class Commands:
|
|
5
|
+
|
|
6
|
+
def __init__(self,
|
|
7
|
+
start_command= 0x0B,
|
|
8
|
+
calibrate_command = 0x0D,
|
|
9
|
+
sleep_command = 0x0F,
|
|
10
|
+
firmware_update_command = 0x11,
|
|
11
|
+
reset_command = 0x13,
|
|
12
|
+
hard_close = 0x38,
|
|
13
|
+
|
|
14
|
+
target_command = 0x66,
|
|
15
|
+
get_feedback_command = 0x68,
|
|
16
|
+
update_param_command = 0x44,
|
|
17
|
+
|
|
18
|
+
save_grasp_onboard_command = 0xC8,
|
|
19
|
+
return_grasps_command = 0xD2,
|
|
20
|
+
execute_grasp_command = 0xE0,
|
|
21
|
+
reset_on_start = 0):
|
|
22
|
+
|
|
23
|
+
self.commands = {
|
|
24
|
+
'start_command': start_command,
|
|
25
|
+
'calibrate_command': calibrate_command,
|
|
26
|
+
'sleep_command': sleep_command,
|
|
27
|
+
'firmware_update_command': firmware_update_command,
|
|
28
|
+
'reset_command': reset_command,
|
|
29
|
+
'hard_close_command' : hard_close,
|
|
30
|
+
'target_command': target_command,
|
|
31
|
+
'get_feedback_command': get_feedback_command,
|
|
32
|
+
'save_grasp_onboard_command': save_grasp_onboard_command,
|
|
33
|
+
'return_grasps_command': return_grasps_command,
|
|
34
|
+
'execute_grasp_command': execute_grasp_command,
|
|
35
|
+
'update_param_command' : update_param_command
|
|
36
|
+
}
|
|
37
|
+
self.reset_on_start = reset_on_start
|
|
38
|
+
|
|
39
|
+
def get_robot_start_command(self,stream:bool,freq:int) -> list:
|
|
40
|
+
"""
|
|
41
|
+
Creates a message to start the hand
|
|
42
|
+
"""
|
|
43
|
+
# RTC start time from PC
|
|
44
|
+
year = int(time.localtime().tm_year - 2000)
|
|
45
|
+
month = int(time.localtime().tm_mon)
|
|
46
|
+
day = int(time.localtime().tm_mday)
|
|
47
|
+
hour = int(time.localtime().tm_hour)
|
|
48
|
+
minute = int(time.localtime().tm_min)
|
|
49
|
+
second = int(time.localtime().tm_sec)
|
|
50
|
+
|
|
51
|
+
if stream:
|
|
52
|
+
return [self.commands['start_command'],20,year,month,day,hour,minute,second,1,(freq>>16)&0xff,(freq>>8)&0xff,freq&0xff,self.reset_on_start]
|
|
53
|
+
else:
|
|
54
|
+
return [self.commands['start_command'],20,year,month,day,hour,minute,second,0,0,0,0,self.reset_on_start]
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def get_target_position_command(self,hand_joints:dict) -> list:
|
|
58
|
+
command_list = [0]*32 # create empty buffer
|
|
59
|
+
# fill command list with data
|
|
60
|
+
for name,joint_data in hand_joints.items():
|
|
61
|
+
command_list[joint_data.index] = int(joint_data.target_angle)
|
|
62
|
+
command_list[joint_data.index+16] = int(joint_data.velocity)
|
|
63
|
+
# insert the command
|
|
64
|
+
command_list.insert(0,self.commands['target_command'])
|
|
65
|
+
|
|
66
|
+
return command_list
|
|
67
|
+
|
|
68
|
+
def get_firmware_command(self,fw_size,upload,drivers):
|
|
69
|
+
command_list = [0]*32
|
|
70
|
+
command_list.insert(0,self.commands['firmware_update_command'])
|
|
71
|
+
command_list[1] = (fw_size >> 24) & 0xFF
|
|
72
|
+
command_list[2] = (fw_size >> 16) & 0xff
|
|
73
|
+
command_list[3] = (fw_size >> 8) & 0xff
|
|
74
|
+
command_list[4] = (fw_size) & 0xff
|
|
75
|
+
command_list[5] = upload
|
|
76
|
+
command_list[6] = drivers
|
|
77
|
+
return command_list
|
|
78
|
+
|
|
79
|
+
def get_calibration_command(self):
|
|
80
|
+
command_list = [0]*32
|
|
81
|
+
command_list.insert(0,self.commands['calibrate_command'])
|
|
82
|
+
return command_list
|
|
83
|
+
|
|
84
|
+
def get_sleep_command(self):
|
|
85
|
+
command_list = [0]*32
|
|
86
|
+
command_list.insert(0,self.commands['sleep_command'])
|
|
87
|
+
return command_list
|
|
88
|
+
|
|
89
|
+
def get_states_command(self):
|
|
90
|
+
command_list = [0]*32
|
|
91
|
+
command_list.insert(0,self.commands['get_feedback_command'])
|
|
92
|
+
return command_list
|
|
93
|
+
|
|
94
|
+
def get_firmware_update_command(self):
|
|
95
|
+
command_list = [0]*32
|
|
96
|
+
command_list.insert(0,self.commands['firmware_update_command'])
|
|
97
|
+
return command_list
|
|
98
|
+
|
|
99
|
+
def get_hard_close_command(self,joint=None,motor=None):
|
|
100
|
+
command_list = [0]*32
|
|
101
|
+
command_list.insert(0,self.commands['hard_close_command'])
|
|
102
|
+
|
|
103
|
+
# constraint checker
|
|
104
|
+
if 0 <= joint <= 15:
|
|
105
|
+
command_list[1] = joint
|
|
106
|
+
else:
|
|
107
|
+
# TODO logging
|
|
108
|
+
None
|
|
109
|
+
if 0 <= motor <= 2:
|
|
110
|
+
command_list[2] = motor
|
|
111
|
+
else:
|
|
112
|
+
# TODO logging
|
|
113
|
+
None
|
|
114
|
+
|
|
115
|
+
return command_list
|
|
116
|
+
|
|
117
|
+
def get_locked_reset_low_command(self, joint=None, motor=None):
|
|
118
|
+
command_list = [0]*32
|
|
119
|
+
command_list.insert(0,self.commands['reset_command'])
|
|
120
|
+
|
|
121
|
+
# constraint checker
|
|
122
|
+
if 0 <= joint <= 15:
|
|
123
|
+
command_list[1] = joint
|
|
124
|
+
else:
|
|
125
|
+
# TODO logging
|
|
126
|
+
None
|
|
127
|
+
if 0 <= motor <= 2:
|
|
128
|
+
command_list[2] = motor
|
|
129
|
+
else:
|
|
130
|
+
# TODO logging
|
|
131
|
+
None
|
|
132
|
+
|
|
133
|
+
return command_list
|
|
134
|
+
|
|
135
|
+
def update_param_command(self,communication='UART', feedback=None):
|
|
136
|
+
"""
|
|
137
|
+
Communication Options:
|
|
138
|
+
'UART' - USBC
|
|
139
|
+
'RS485' - RS485
|
|
140
|
+
'CAN' - CAN
|
|
141
|
+
Feedback Options (only if CAN):
|
|
142
|
+
'ALL' - Positions,Currents,Temperatures
|
|
143
|
+
'PC' - Positions,Currentns
|
|
144
|
+
'P' - Positions
|
|
145
|
+
"""
|
|
146
|
+
command_list = [0]*32
|
|
147
|
+
command_list.insert(0,self.commands['update_param_command'])
|
|
148
|
+
|
|
149
|
+
if communication:
|
|
150
|
+
command_list[1] = 1
|
|
151
|
+
if communication == 'UART':
|
|
152
|
+
command_list[1+16] = 1
|
|
153
|
+
elif communication == 'CAN':
|
|
154
|
+
command_list[1+16] = 2
|
|
155
|
+
elif communication == 'RS485':
|
|
156
|
+
command_list[1+16] = 3
|
|
157
|
+
|
|
158
|
+
if feedback:
|
|
159
|
+
command_list[2] = 1
|
|
160
|
+
if feedback == 'ALL':
|
|
161
|
+
command_list[2+16] = 2
|
|
162
|
+
elif feedback == 'PC':
|
|
163
|
+
command_list[2+16] = 1
|
|
164
|
+
elif feedback == 'P':
|
|
165
|
+
command_list[2+16] = 0
|
|
166
|
+
|
|
167
|
+
return command_list
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
if __name__ == "__main__":
|
|
171
|
+
None
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import serial
|
|
2
|
+
import logging
|
|
3
|
+
import time
|
|
4
|
+
|
|
5
|
+
class UART:
|
|
6
|
+
def __init__(self,
|
|
7
|
+
port='COM9',
|
|
8
|
+
baudrate=921600, #115200,
|
|
9
|
+
timeout=0.5,
|
|
10
|
+
logger = None):
|
|
11
|
+
|
|
12
|
+
# automatically connect to the first available port
|
|
13
|
+
self.port = port
|
|
14
|
+
self.baudrate = baudrate
|
|
15
|
+
self.timeout = timeout
|
|
16
|
+
|
|
17
|
+
self.esp32 = serial.Serial(baudrate=self.baudrate, timeout= self.timeout)
|
|
18
|
+
|
|
19
|
+
# required delays for sending message
|
|
20
|
+
self.timer_send = 0.003
|
|
21
|
+
self.timer_recv = self.timer_send*4
|
|
22
|
+
self.maximum_freq = 700 # hz
|
|
23
|
+
|
|
24
|
+
if not logger:
|
|
25
|
+
self.logger = logging.getLogger(__name__)
|
|
26
|
+
else:
|
|
27
|
+
self.logger = logger
|
|
28
|
+
|
|
29
|
+
def open(self):
|
|
30
|
+
try:
|
|
31
|
+
self.esp32.port=self.port
|
|
32
|
+
self.esp32.close()
|
|
33
|
+
self.esp32.open()
|
|
34
|
+
self.logger.info(f"Opening {self.port} @ {self.baudrate} baudrate")
|
|
35
|
+
self.esp32.flush()
|
|
36
|
+
except Exception as e:
|
|
37
|
+
self.logger.error(f'Error opening port {self.port}')
|
|
38
|
+
self.logger.error(e)
|
|
39
|
+
|
|
40
|
+
def send(self, data:bytearray):
|
|
41
|
+
# print(data)
|
|
42
|
+
try:
|
|
43
|
+
self.esp32.write(data)
|
|
44
|
+
|
|
45
|
+
# required delay
|
|
46
|
+
t = time.perf_counter()
|
|
47
|
+
while time.perf_counter() - t < self.timer_send:
|
|
48
|
+
pass
|
|
49
|
+
except Exception as e:
|
|
50
|
+
self.logger.error(f'Unable to send command through {self.port}')
|
|
51
|
+
self.logger.error(e)
|
|
52
|
+
|
|
53
|
+
def receive(self):
|
|
54
|
+
|
|
55
|
+
# required delay
|
|
56
|
+
# t = time.perf_counter()
|
|
57
|
+
# while time.perf_counter() - t < self.timer_recv:
|
|
58
|
+
# pass
|
|
59
|
+
try:
|
|
60
|
+
x = time.perf_counter()
|
|
61
|
+
# print(f'time start: {x}')
|
|
62
|
+
while self.esp32.in_waiting < 65:
|
|
63
|
+
if time.perf_counter() - x > 0.02:
|
|
64
|
+
break
|
|
65
|
+
# print(f'elapsed time: {time.perf_counter() - x}')
|
|
66
|
+
# time.sleep(0.03)
|
|
67
|
+
# check data
|
|
68
|
+
if self.esp32.in_waiting >= 65: # get data if greater or equal to 65
|
|
69
|
+
msg_bytes = self.esp32.read(65)
|
|
70
|
+
print(msg_bytes)
|
|
71
|
+
return msg_bytes
|
|
72
|
+
elif self.esp32.in_waiting > 0: # clear buffer if not correct amount of data
|
|
73
|
+
msg_bytes = self.esp32.read_all()
|
|
74
|
+
self.logger.warning(f"Incomplete data received - package size = {(self.esp32.in_waiting)}")
|
|
75
|
+
return None
|
|
76
|
+
else: # non blocking
|
|
77
|
+
return None
|
|
78
|
+
except Exception as e:
|
|
79
|
+
self.logger.warning(f"No data available to receive {e}")
|
|
80
|
+
self.logger.error(e)
|
|
81
|
+
|
|
82
|
+
def close(self):
|
|
83
|
+
self.esp32.close()
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def test_serial_receive():
|
|
87
|
+
esp32_communication = UART()
|
|
88
|
+
esp32_communication.start()
|
|
89
|
+
while True:
|
|
90
|
+
msg = esp32_communication.receive()
|
|
91
|
+
if msg != "":
|
|
92
|
+
print(msg)
|
|
93
|
+
|
|
94
|
+
def test_serial_send():
|
|
95
|
+
esp32_communication = UART()
|
|
96
|
+
esp32_communication.start()
|
|
97
|
+
while True:
|
|
98
|
+
esp32_communication.send("hello\n")
|
|
99
|
+
time.sleep(1)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
if __name__ == "__main__":
|
|
103
|
+
test_serial_receive()
|
|
104
|
+
# test_serial_send()
|
|
@@ -41,7 +41,7 @@ class WiFiServer:
|
|
|
41
41
|
|
|
42
42
|
def _get_available_ip(self):
|
|
43
43
|
gateway_ip = None
|
|
44
|
-
|
|
44
|
+
|
|
45
45
|
# Get all network interfaces
|
|
46
46
|
interfaces = psutil.net_if_addrs()
|
|
47
47
|
|
|
@@ -69,10 +69,10 @@ class WiFiServer:
|
|
|
69
69
|
source_ip = self._get_available_ip()
|
|
70
70
|
# TCP tuple
|
|
71
71
|
self.esp = (source_ip,self.port)
|
|
72
|
-
|
|
72
|
+
print(self.esp)
|
|
73
73
|
# create server socket
|
|
74
74
|
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
75
|
-
self.server_socket.settimeout(
|
|
75
|
+
self.server_socket.settimeout(10) # blocking timeout to connect
|
|
76
76
|
self.server_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
|
|
77
77
|
self.server_socket.bind(self.esp)
|
|
78
78
|
|
|
@@ -82,11 +82,12 @@ class WiFiServer:
|
|
|
82
82
|
self.logger.info(f"Listening for connection")
|
|
83
83
|
except Exception as e:
|
|
84
84
|
self.logger.error(f"No Network named {self.target_ssid}")
|
|
85
|
+
print(e)
|
|
85
86
|
|
|
86
87
|
# Accept the connection
|
|
87
88
|
try:
|
|
88
89
|
self.conn, self.addr = self.server_socket.accept()
|
|
89
|
-
self.conn.settimeout(0.
|
|
90
|
+
self.conn.settimeout(0.5) # 1 ms timeout for recv and send methods
|
|
90
91
|
# TODO logging
|
|
91
92
|
self.logger.info(f"New Connection: {self.addr} connected.")
|
|
92
93
|
except TimeoutError as e:
|
|
@@ -119,6 +120,8 @@ class WiFiServer:
|
|
|
119
120
|
self.logger.warning(f"Unable to create wifi profile")
|
|
120
121
|
|
|
121
122
|
else:
|
|
123
|
+
# refresh wifi scan
|
|
124
|
+
|
|
122
125
|
# connect to profile
|
|
123
126
|
connect_command = f'netsh wlan connect ssid={self.target_ssid} name={self.target_ssid} interface=Wi-Fi'
|
|
124
127
|
subprocess.run(connect_command, shell=True)
|
|
@@ -130,13 +133,13 @@ class WiFiServer:
|
|
|
130
133
|
# input password
|
|
131
134
|
if not self.password:
|
|
132
135
|
self.password = input('type ssid password:')
|
|
133
|
-
|
|
136
|
+
elif self.password:
|
|
134
137
|
connect_command += f' password "{self.password}"'
|
|
135
138
|
|
|
136
139
|
subprocess.run(connect_command, shell=True)
|
|
137
140
|
|
|
138
141
|
# clear password
|
|
139
|
-
self.password = None
|
|
142
|
+
# self.password = None
|
|
140
143
|
|
|
141
144
|
else:
|
|
142
145
|
# TODO logging
|
|
@@ -157,7 +160,7 @@ class WiFiServer:
|
|
|
157
160
|
|
|
158
161
|
elif system == "Linux":
|
|
159
162
|
# Linux uses the "nmcli" command to disconnect from Wi-Fi networks
|
|
160
|
-
disconnect_command = f'nmcli
|
|
163
|
+
disconnect_command = f'nmcli con down id "{self.target_ssid}"'
|
|
161
164
|
subprocess.run(disconnect_command, shell=True)
|
|
162
165
|
|
|
163
166
|
else:
|
|
@@ -183,7 +186,7 @@ class WiFiServer:
|
|
|
183
186
|
return None
|
|
184
187
|
except Exception as e:
|
|
185
188
|
# TODO logging
|
|
186
|
-
self.logger.warning(f"No data available to receive")
|
|
189
|
+
self.logger.warning(f"No data available to receive {e}")
|
|
187
190
|
return None
|
|
188
191
|
|
|
189
192
|
|
|
@@ -193,42 +196,4 @@ class WiFiServer:
|
|
|
193
196
|
except Exception as e:
|
|
194
197
|
# TODO insert error logging
|
|
195
198
|
self.logger.error(f"Unable to send data")
|
|
196
|
-
None
|
|
197
|
-
|
|
198
|
-
def flash_wifi(self):
|
|
199
|
-
acknowledged = False
|
|
200
|
-
|
|
201
|
-
file_location = input("Enter file path: ")
|
|
202
|
-
# file_location = "C:\\Users\\RyanLee\\Documents\\GitHub\\Artus-3D-actuators-fw\\Debug\\actuator_m0.bin"
|
|
203
|
-
file_size = os.path.getsize(file_location)
|
|
204
|
-
|
|
205
|
-
self.conn.send("file ready\n".encode())
|
|
206
|
-
self.conn.send((str(file_size)+"\n").encode())
|
|
207
|
-
file = open(file_location, "rb")
|
|
208
|
-
file_data = file.read()
|
|
209
|
-
file.close
|
|
210
|
-
self.conn.sendall(file_data)
|
|
211
|
-
|
|
212
|
-
while not acknowledged:
|
|
213
|
-
message = self.conn.recv(1024).decode()
|
|
214
|
-
if message == "failed":
|
|
215
|
-
print("File upload failed\n")
|
|
216
|
-
acknowledged = True
|
|
217
|
-
elif message == "success":
|
|
218
|
-
print("File upload successful\n")
|
|
219
|
-
acknowledged = True
|
|
220
|
-
|
|
221
|
-
print("Flashing...")
|
|
222
|
-
|
|
223
|
-
while True:
|
|
224
|
-
message = self.conn.recv(1024).decode()
|
|
225
|
-
if message == "FLASHED":
|
|
226
|
-
print("Firmware update complete")
|
|
227
|
-
break
|
|
228
|
-
|
|
229
|
-
def upload_logs_wifi():
|
|
230
|
-
while True:
|
|
231
|
-
# receive here
|
|
232
|
-
if """ finishing condition""":
|
|
233
|
-
break
|
|
234
|
-
return
|
|
199
|
+
None
|