smartpi 0.1.38__py3-none-any.whl

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.
Files changed (44) hide show
  1. smartpi/__init__.py +8 -0
  2. smartpi/_gui.py +66 -0
  3. smartpi/base_driver.py +566 -0
  4. smartpi/camera.py +84 -0
  5. smartpi/color_sensor.py +18 -0
  6. smartpi/cw2015.py +179 -0
  7. smartpi/flash.py +130 -0
  8. smartpi/humidity.py +20 -0
  9. smartpi/led.py +19 -0
  10. smartpi/light_sensor.py +72 -0
  11. smartpi/motor.py +177 -0
  12. smartpi/move.py +218 -0
  13. smartpi/onnx_hand_workflow.py +201 -0
  14. smartpi/onnx_image_workflow.py +176 -0
  15. smartpi/onnx_pose_workflow.py +482 -0
  16. smartpi/onnx_text_workflow.py +173 -0
  17. smartpi/onnx_voice_workflow.py +437 -0
  18. smartpi/posemodel/__init__.py +0 -0
  19. smartpi/posemodel/posenet.tflite +0 -0
  20. smartpi/posenet_utils.py +222 -0
  21. smartpi/rknn_hand_workflow.py +245 -0
  22. smartpi/rknn_image_workflow.py +405 -0
  23. smartpi/rknn_pose_workflow.py +592 -0
  24. smartpi/rknn_text_workflow.py +240 -0
  25. smartpi/rknn_voice_workflow.py +394 -0
  26. smartpi/servo.py +178 -0
  27. smartpi/temperature.py +18 -0
  28. smartpi/text_gte_model/__init__.py +0 -0
  29. smartpi/text_gte_model/config/__init__.py +0 -0
  30. smartpi/text_gte_model/config/config.json +30 -0
  31. smartpi/text_gte_model/config/quantize_config.json +30 -0
  32. smartpi/text_gte_model/config/special_tokens_map.json +7 -0
  33. smartpi/text_gte_model/config/tokenizer.json +14924 -0
  34. smartpi/text_gte_model/config/tokenizer_config.json +23 -0
  35. smartpi/text_gte_model/config/vocab.txt +14760 -0
  36. smartpi/text_gte_model/gte/__init__.py +0 -0
  37. smartpi/text_gte_model/gte/gte_model.onnx +0 -0
  38. smartpi/touch_sensor.py +16 -0
  39. smartpi/trace.py +120 -0
  40. smartpi/ultrasonic.py +20 -0
  41. smartpi-0.1.38.dist-info/METADATA +17 -0
  42. smartpi-0.1.38.dist-info/RECORD +44 -0
  43. smartpi-0.1.38.dist-info/WHEEL +5 -0
  44. smartpi-0.1.38.dist-info/top_level.txt +1 -0
smartpi/camera.py ADDED
@@ -0,0 +1,84 @@
1
+ # coding: utf-8
2
+ import cv2
3
+ import os
4
+ import time
5
+ import platform
6
+
7
+ class Camera:
8
+ def __init__(self, indexes=[0, 1, 2, 3], target_width=640, target_height=480):
9
+ self.cap = None
10
+ self.indexes = indexes
11
+ self.target_width = target_width
12
+ self.target_height = target_height
13
+ self.open_camera()
14
+
15
+ def open_camera(self):
16
+ """打开摄像头(硬件加速+参数优化)"""
17
+ for idx in self.indexes:
18
+ try:
19
+ # 适配linux/Android的V4L2硬件加速(RK芯片优先)
20
+ if platform.system() == "Linux":
21
+ cap = cv2.VideoCapture(idx, cv2.CAP_V4L2)
22
+ # 尝试启用硬件加速(兼容不同OpenCV版本)
23
+ try:
24
+ # 对于较新版本的OpenCV
25
+ if hasattr(cv2, 'CAP_PROP_HW_ACCELERATION') and hasattr(cv2, 'VIDEO_ACCELERATION_ANY'):
26
+ cap.set(cv2.CAP_PROP_HW_ACCELERATION, cv2.VIDEO_ACCELERATION_ANY)
27
+ except AttributeError as ae:
28
+ print(f"硬件加速设置不支持,使用默认配置: {ae}")
29
+ else:
30
+ cap = cv2.VideoCapture(idx)
31
+
32
+ if cap.isOpened():
33
+ # 尝试设置分辨率
34
+ cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.target_width)
35
+ cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.target_height)
36
+
37
+ # 获取实际设置的分辨率
38
+ actual_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
39
+ actual_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
40
+
41
+ print(f"摄像头 {idx} 已打开, 分辨率: {actual_width}x{actual_height}")
42
+ self.cap = cap
43
+ return True
44
+ except Exception as e:
45
+ print(f"尝试打开摄像头 {idx} 失败: {e}")
46
+ continue
47
+
48
+ print("无法打开任何摄像头")
49
+ return False
50
+
51
+ def read_frame(self):
52
+ """读取一帧并自动处理错误"""
53
+ if not self.cap or not self.cap.isOpened():
54
+ return False, None
55
+
56
+ ret, frame = self.cap.read()
57
+ if not ret:
58
+ print("读取帧失败,尝试重新打开摄像头...")
59
+ self.release()
60
+ time.sleep(1)
61
+ if self.open_camera():
62
+ return self.read_frame()
63
+ return False, None
64
+
65
+ # 调整到目标分辨率
66
+ if frame.shape[1] != self.target_width or frame.shape[0] != self.target_height:
67
+ frame = cv2.resize(frame, (self.target_width, self.target_height))
68
+
69
+ return True, frame
70
+
71
+ def get_resolution(self):
72
+ """获取当前分辨率"""
73
+ if self.cap and self.cap.isOpened():
74
+ return (
75
+ int(self.cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
76
+ int(self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
77
+ )
78
+ return self.target_width, self.target_height
79
+
80
+ def release(self):
81
+ """释放摄像头资源"""
82
+ if self.cap and self.cap.isOpened():
83
+ self.cap.release()
84
+ self.cap = None
@@ -0,0 +1,18 @@
1
+ # coding=utf-8
2
+ import time
3
+ from typing import List, Optional
4
+ from smartpi import base_driver
5
+
6
+ #��ɫ��ȡ ��������ֵ��1-��ɫ��2-��ɫ��3-��ɫ��4-��ɫ��5-��ɫ��6-��ɫ�� ��ȡ����-1
7
+ def get_value(port:bytes) -> Optional[bytes]:
8
+ color_str=[0xA0, 0x04, 0x00, 0xBE]
9
+ color_str[0]=0XA0+port
10
+ color_str[2]=1
11
+ time.sleep(0.005)
12
+ response = base_driver.single_operate_sensor(color_str,0)
13
+ if response == None:
14
+ return None
15
+ else:
16
+ return response[4]
17
+
18
+
smartpi/cw2015.py ADDED
@@ -0,0 +1,179 @@
1
+ #!/usr/bin/env python3
2
+ import os
3
+ import sys
4
+ import fcntl
5
+ import time
6
+
7
+ # I2C设备文件路径
8
+ I2C_DEV_PATH = "/dev/i2c-2" # 根据实际情况修改总线号
9
+
10
+ # I2C从设备地址
11
+ CW2015_ADDR = 0x62 # 7位地址
12
+
13
+ # I2C通信常量
14
+ I2C_SLAVE = 0x0703
15
+ I2C_SMBUS = 0x0720 # SMBus传输
16
+
17
+ # SMBus命令结构
18
+ class i2c_smbus_ioctl_data:
19
+ def __init__(self, read_write, command, size, data):
20
+ self.read_write = read_write # 0 = write, 1 = read
21
+ self.command = command # 寄存器地址
22
+ self.size = size # 数据大小
23
+ self.data = data # 数据指针
24
+
25
+ # 寄存器地址定义
26
+ VERSION = 0x00
27
+ VCELL_H = 0x02
28
+ VCELL_L = 0x03
29
+ SOC_B = 0x04
30
+ SOC = 0x05
31
+ RRT_H = 0x06
32
+ RRT_L = 0x07
33
+ CONFIG = 0x08
34
+ MOOD = 0x0A
35
+
36
+ class I2CDevice:
37
+ """通过设备文件直接访问I2C设备"""
38
+ def __init__(self, device_path, device_addr):
39
+ """
40
+ 初始化I2C设备
41
+ :param device_path: I2C设备文件路径 (例如 "/dev/i2c-2")
42
+ :param device_addr: I2C从设备地址 (7位地址)
43
+ """
44
+ self.device_path = device_path
45
+ self.device_addr = device_addr
46
+ self.fd = None
47
+
48
+ try:
49
+ # 打开设备文件
50
+ self.fd = os.open(device_path, os.O_RDWR)
51
+ # 设置从设备地址
52
+ fcntl.ioctl(self.fd, I2C_SLAVE, device_addr)
53
+ #print(f"成功打开 {device_path} 并设置地址 0x{device_addr:02X}")
54
+ except Exception as e:
55
+ print(f"打开I2C设备失败: {e}")
56
+ if self.fd:
57
+ os.close(self.fd)
58
+ sys.exit(1)
59
+
60
+ def __del__(self):
61
+ """关闭设备文件"""
62
+ if self.fd:
63
+ os.close(self.fd)
64
+
65
+ def write_byte(self, reg_addr, value):
66
+ """
67
+ 向寄存器写入一个字节
68
+ :param reg_addr: 寄存器地址
69
+ :param value: 要写入的值
70
+ :return: 成功返回True,失败返回False
71
+ """
72
+ try:
73
+ # 构造写入数据:寄存器地址 + 值
74
+ data = bytes([reg_addr, value])
75
+ os.write(self.fd, data)
76
+ return True
77
+ except Exception as e:
78
+ print(f"写入寄存器0x{reg_addr:02X}失败: {e}")
79
+ return False
80
+
81
+ def read_byte(self, reg_addr):
82
+ """
83
+ 从寄存器读取一个字节
84
+ :param reg_addr: 寄存器地址
85
+ :return: 读取到的字节值,失败返回0
86
+ """
87
+ try:
88
+ # 先写入寄存器地址
89
+ os.write(self.fd, bytes([reg_addr]))
90
+ # 然后读取一个字节
91
+ value = os.read(self.fd, 1)
92
+ return value[0] if value else 0
93
+ except Exception as e:
94
+ print(f"读取寄存器0x{reg_addr:02X}失败: {e}")
95
+ return 0
96
+
97
+ def read_word(self, reg_addr):
98
+ """
99
+ 从寄存器读取两个字节(先高字节后低字节)
100
+ :param reg_addr: 寄存器地址(高字节寄存器)
101
+ :return: (高字节 << 8) | 低字节
102
+ """
103
+ try:
104
+ # 先写入寄存器地址
105
+ os.write(self.fd, bytes([reg_addr]))
106
+ # 读取两个字节
107
+ data = os.read(self.fd, 2)
108
+ if len(data) == 2:
109
+ return (data[0] << 8) | data[1]
110
+ else:
111
+ return 0
112
+ except Exception as e:
113
+ print(f"读取寄存器0x{reg_addr:02X}失败: {e}")
114
+ return 0
115
+
116
+ class CW2015:
117
+ """CW2015电池监测芯片驱动"""
118
+ def __init__(self, i2c_dev_path=I2C_DEV_PATH, i2c_addr=CW2015_ADDR):
119
+ """
120
+ 初始化CW2015
121
+ :param i2c_dev_path: I2C设备路径
122
+ :param i2c_addr: I2C地址
123
+ """
124
+ self.device = I2CDevice(i2c_dev_path, i2c_addr)
125
+
126
+ def init(self):
127
+ """初始化芯片配置"""
128
+ # 写入配置
129
+ if not self.device.write_byte(CONFIG, 0x50):
130
+ print("配置寄存器写入失败")
131
+ return False
132
+ if not self.device.write_byte(MOOD, 0x00):
133
+ print("模式寄存器写入失败")
134
+ return False
135
+
136
+ time.sleep(0.05) # 50ms延时
137
+ #print("CW2015初始化完成")
138
+ return True
139
+
140
+ def get_id(self):
141
+ """获取芯片ID"""
142
+ return self.device.read_byte(VERSION)
143
+
144
+ def get_voltage(self):
145
+ """读取电池电压(mV)"""
146
+ # 读取电压高字节寄存器
147
+ vh = self.device.read_byte(VCELL_H)
148
+ # 读取电压低字节寄存器
149
+ vl = self.device.read_byte(VCELL_L)
150
+
151
+ # 组合14位ADC值
152
+ adc_value = ((vh & 0x3F) << 8) | vl
153
+ # 转换为电压值 (305μV/LSB)
154
+ voltage = adc_value * 305 / 1000
155
+ return int(voltage)
156
+
157
+ def get_soc(self, mode=0):
158
+ """读取电池剩余电量"""
159
+ if mode == 0:
160
+ return self.device.read_byte(SOC_B) # 百分比整数
161
+ else:
162
+ return self.device.read_byte(SOC) # 高精度值
163
+
164
+ def get_remaining_time(self):
165
+ """获取剩余工作时间(分钟)"""
166
+ # 读取时间高字节寄存器
167
+ rh = self.device.read_byte(RRT_H)
168
+ # 读取时间低字节寄存器
169
+ rl = self.device.read_byte(RRT_L)
170
+
171
+ # 提取剩余时间(13位)
172
+ remaining_time = ((rh & 0x1F) << 8) | rl
173
+ return remaining_time
174
+
175
+ def get_alert_status(self):
176
+ """获取告警状态"""
177
+ rh = self.device.read_byte(RRT_H)
178
+ return (rh >> 7) & 0x01
179
+
smartpi/flash.py ADDED
@@ -0,0 +1,130 @@
1
+ import os
2
+ import struct
3
+ import fcntl
4
+
5
+ # 配置文件路径 (当前目录下的flash.bin)
6
+ FLASH_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "/home/Interface/flash/flash.bin")
7
+ FLASH_FILE_2 = os.path.join(os.path.dirname(os.path.abspath(__file__)), "/home/Interface/flash/flash_2.bin")
8
+
9
+ DATA_SIZE = 2 # 每个数据2字节
10
+ TOTAL_SLOTS = 120 # 100个数据槽
11
+
12
+ def _init_flash_file():
13
+ """初始化存储文件"""
14
+ if not os.path.exists(FLASH_FILE):
15
+ with open(FLASH_FILE, "wb") as f:
16
+ f.write(b'\x00' * DATA_SIZE * TOTAL_SLOTS)
17
+
18
+ def write(address, data):
19
+ """
20
+ 写入数据到指定地址
21
+ :param address: 地址编号 (1-100)
22
+ :param data: 要写入的整数数据 (2字节范围)
23
+ """
24
+ if not 1 <= address <= TOTAL_SLOTS:
25
+ raise ValueError(f"地址必须在1-{TOTAL_SLOTS}范围内")
26
+
27
+ # 2字节数据范围: 0-65535 (0xFFFF)
28
+ if not 0 <= data <= 0xFFFF:
29
+ raise ValueError("数据必须在0-65535范围内")
30
+
31
+ _init_flash_file()
32
+
33
+ # 计算文件偏移量 (每个地址2字节)
34
+ offset = (address - 1) * DATA_SIZE
35
+
36
+ with open(FLASH_FILE, "r+b") as f:
37
+ # 使用文件锁确保写入安全
38
+ fcntl.flock(f, fcntl.LOCK_EX)
39
+ f.seek(offset)
40
+ # 将整数打包为2字节小端格式
41
+ f.write(struct.pack('<H', data))
42
+ f.flush()
43
+ fcntl.flock(f, fcntl.LOCK_UN)
44
+
45
+ def read(address):
46
+ """
47
+ 从指定地址读取数据
48
+ :param address: 地址编号 (1-100)
49
+ :return: 读取到的整数数据
50
+ """
51
+ if not 1 <= address <= TOTAL_SLOTS:
52
+ raise ValueError(f"地址必须在1-{TOTAL_SLOTS}范围内")
53
+
54
+ _init_flash_file()
55
+
56
+ offset = (address - 1) * DATA_SIZE
57
+
58
+ with open(FLASH_FILE, "rb") as f:
59
+ fcntl.flock(f, fcntl.LOCK_SH) # 共享锁
60
+ f.seek(offset)
61
+ # 读取2字节并解包为整数
62
+ data_bytes = f.read(DATA_SIZE)
63
+ fcntl.flock(f, fcntl.LOCK_UN)
64
+
65
+ if len(data_bytes) != DATA_SIZE:
66
+ return 0 # 返回默认值0
67
+
68
+ return struct.unpack('<H', data_bytes)[0]
69
+
70
+ # 可选扩展功能
71
+ def erase_all():
72
+ """擦除所有数据(重置为0)"""
73
+ with open(FLASH_FILE, "wb") as f:
74
+ f.write(b'\x00' * DATA_SIZE * TOTAL_SLOTS)
75
+
76
+
77
+ def _init_flash_file_2():
78
+ """初始化存储文件"""
79
+ if not os.path.exists(FLASH_FILE_2):
80
+ with open(FLASH_FILE_2, "wb") as f:
81
+ f.write(b'\x00' * DATA_SIZE * TOTAL_SLOTS)
82
+
83
+ def write_2(address, data):
84
+ if not 1 <= address <= TOTAL_SLOTS:
85
+ raise ValueError(f"地址必须在1-{TOTAL_SLOTS}范围内")
86
+
87
+ # 2字节数据范围: 0-65535 (0xFFFF)
88
+ if not 0 <= data <= 0xFFFF:
89
+ raise ValueError("数据必须在0-65535范围内")
90
+
91
+ _init_flash_file_2()
92
+
93
+ # 计算文件偏移量 (每个地址2字节)
94
+ offset = (address - 1) * DATA_SIZE
95
+
96
+ with open(FLASH_FILE_2, "r+b") as f:
97
+ # 使用文件锁确保写入安全
98
+ fcntl.flock(f, fcntl.LOCK_EX)
99
+ f.seek(offset)
100
+ # 将整数打包为2字节小端格式
101
+ f.write(struct.pack('<H', data))
102
+ f.flush()
103
+ fcntl.flock(f, fcntl.LOCK_UN)
104
+
105
+ def read_2(address):
106
+ if not 1 <= address <= TOTAL_SLOTS:
107
+ raise ValueError(f"地址必须在1-{TOTAL_SLOTS}范围内")
108
+
109
+ _init_flash_file_2()
110
+
111
+ offset = (address - 1) * DATA_SIZE
112
+
113
+ with open(FLASH_FILE_2, "rb") as f:
114
+ fcntl.flock(f, fcntl.LOCK_SH) # 共享锁
115
+ f.seek(offset)
116
+ # 读取2字节并解包为整数
117
+ data_bytes = f.read(DATA_SIZE)
118
+ fcntl.flock(f, fcntl.LOCK_UN)
119
+
120
+ if len(data_bytes) != DATA_SIZE:
121
+ return 0 # 返回默认值0
122
+
123
+ return struct.unpack('<H', data_bytes)[0]
124
+
125
+ # 可选扩展功能
126
+ def erase_all_2():
127
+ """擦除所有数据(重置为0)"""
128
+ with open(FLASH_FILE_2, "wb") as f:
129
+ f.write(b'\x00' * DATA_SIZE * TOTAL_SLOTS)
130
+
smartpi/humidity.py ADDED
@@ -0,0 +1,20 @@
1
+ # coding=utf-8
2
+ import time
3
+ from typing import List, Optional
4
+ from smartpi import base_driver
5
+
6
+
7
+ #湿度读取 port:连接P端口;正常返回:湿度数据; 读取错误:-1
8
+ def get_value(port:bytes) -> Optional[bytes]:
9
+ humi_str=[0XA0, 0X0C, 0X01, 0X71, 0X00, 0XBE]
10
+ humi_str[0]=0XA0+port
11
+ humi_str[4]=0X01
12
+ time.sleep(0.005)
13
+ response = base_driver.single_operate_sensor(humi_str,0)
14
+ if response == None:
15
+ return None
16
+ else:
17
+ return response[4]
18
+
19
+
20
+
smartpi/led.py ADDED
@@ -0,0 +1,19 @@
1
+ # coding=utf-8
2
+ import time
3
+ from typing import List, Optional
4
+ from smartpi import base_driver
5
+
6
+
7
+ #�ʵƿ��� port:����P�˿ڣ�command:0:�صƣ�1:�죻2:�̣�3:����4:�ƣ�5:�ϣ�6:�ࣻ7:�ף� �������أ�0; ��ȡ����-1
8
+ def set_color(port:bytes,command:bytes) -> Optional[bytes]:
9
+ color_lamp_str=[0xA0, 0x05, 0x00, 0xBE]
10
+ color_lamp_str[0]=0XA0+port
11
+ color_lamp_str[2]=command
12
+ # response = base_driver.single_operate_sensor(color_lamp_str,0)
13
+ time.sleep(0.005)
14
+ base_driver.write_data(0X01, 0X02, color_lamp_str)
15
+ # if response == None:
16
+ # return None
17
+ # else:
18
+ return 0
19
+
@@ -0,0 +1,72 @@
1
+ # coding=utf-8
2
+ import time
3
+ from typing import List, Optional
4
+ from smartpi import base_driver
5
+
6
+ #����ֵ��ȡ port:����P�˿ڣ� �������أ���ֵ����; ��ȡ����-1
7
+ def turn_off(port:bytes) -> Optional[bytes]:
8
+ light_str=[0xA0, 0x02, 0x00, 0xBE]
9
+ light_str[0]=0XA0+port
10
+ light_str[2]=0x03
11
+ # response = base_driver.single_operate_sensor(light_str,0)
12
+ base_driver.write_data(0X01, 0X02, light_str)
13
+ # if response == None:
14
+ # return None
15
+ # else:
16
+ return 0
17
+
18
+ #����ֵ��ȡ port:����P�˿ڣ� �������أ���ֵ����; ��ȡ����-1
19
+ def get_value(port:bytes) -> Optional[bytes]:
20
+ light_str=[0xA0, 0x02, 0x00, 0xBE]
21
+ light_str[0]=0XA0+port
22
+ light_str[2]=0x01
23
+ time.sleep(0.005)
24
+ response = base_driver.single_operate_sensor(light_str,0)
25
+ if response == None:
26
+ return None
27
+ else:
28
+ light_data=response[4:-1]
29
+ light_num=int.from_bytes(light_data, byteorder='big', signed=True)
30
+ return light_num
31
+
32
+ #�����ֵ���� port:����P�˿ڣ� threshold�����õ���ֵ0~4000
33
+ def set_threshold(port:bytes,threshold:int) -> Optional[bytes]:
34
+ light_str=[0xA0, 0x02, 0x00, 0x81, 0x00, 0x00, 0xBE]
35
+ light_str[0]=0XA0+port
36
+ light_str[2]=0x04
37
+ light_str[4]=threshold//256
38
+ light_str[5]=threshold%256
39
+ time.sleep(0.005)
40
+ base_driver.write_data(0X01, 0X02, light_str)
41
+ # response = base_driver.single_operate_sensor(light_str,0)
42
+ # if response == None:
43
+ # return None
44
+ # else:
45
+ return 0
46
+
47
+ #�����ֵ��ȡ port:����P�˿ڣ�
48
+ def get_threshold(port:bytes) -> Optional[bytes]:
49
+ light_str=[0xA0, 0x02, 0x00, 0xBE]
50
+ light_str[0]=0XA0+port
51
+ light_str[2]=0x05
52
+ time.sleep(0.005)
53
+ response = base_driver.single_operate_sensor(light_str,0)
54
+ if response == None:
55
+ return None
56
+ else:
57
+ light_data=response[4:-1]
58
+ light_num=int.from_bytes(light_data, byteorder='big', signed=True)
59
+ return light_num
60
+
61
+ #����ȡ��ǰֵ���趨��ֵ�ȽϺ��boolֵ port:����P�˿ڣ�
62
+ def get_bool_data(port:bytes) -> Optional[bytes]:
63
+ light_str=[0xA0, 0x02, 0x00, 0xBE]
64
+ light_str[0]=0XA0+port
65
+ light_str[2]=0x06
66
+ time.sleep(0.005)
67
+ response = base_driver.single_operate_sensor(light_str,0)
68
+ if response == None:
69
+ return None
70
+ else:
71
+ return response[4]
72
+
smartpi/motor.py ADDED
@@ -0,0 +1,177 @@
1
+ # coding=utf-8
2
+ import time
3
+ import struct
4
+ from typing import List, Optional
5
+ from smartpi import base_driver
6
+
7
+
8
+ #��������ȡ port:����M�˿ڣ�
9
+ def get_motor_encoder(port:bytes) -> Optional[bytes]:
10
+ motor_str=[0xA0, 0x01, 0x01, 0xBE]
11
+ motor_str[0]=0XA0+port
12
+ time.sleep(0.005)
13
+ response = base_driver.single_operate_sensor(motor_str,0)
14
+ if response == None:
15
+ return None
16
+ else:
17
+ code_data=response[4:-1]
18
+ code_num=int.from_bytes(code_data, byteorder='big', signed=True)
19
+ return code_num
20
+
21
+ #����������� port:����M�˿ڣ�
22
+ def reset_motor_encoder(port:bytes) -> Optional[bytes]:
23
+ motor_str=[0xA0, 0x01, 0x03, 0xBE]
24
+ motor_str[0]=0XA0+port
25
+ # response = base_driver.single_operate_sensor(motor_str,0)
26
+ time.sleep(0.005)
27
+ base_driver.write_data(0X01, 0X02, motor_str)
28
+ # if response == None:
29
+ # return None
30
+ # else:
31
+ return 0
32
+
33
+ #���﷽����� port:����M�˿ڣ�dir:0��1
34
+ def set_motor_direction(port:bytes,direc:bytes) -> Optional[bytes]:
35
+ motor_str=[0xA0, 0x01, 0x06, 0x71, 0x00, 0xBE]
36
+ motor_str[0]=0XA0+port
37
+ motor_str[4]=direc
38
+ # response = base_driver.single_operate_sensor(motor_str,0)
39
+ time.sleep(0.005)
40
+ base_driver.write_data(0X01, 0X02, motor_str)
41
+ # if response == None:
42
+ # return None
43
+ # else:
44
+ return 0
45
+
46
+ #�����ٶ�ת�� port:����M�˿ڣ�speed:-100~100
47
+ def set_motor(port:bytes,speed:int) -> Optional[bytes]:
48
+ motor_str=[0xA0, 0x01, 0x02, 0x71, 0x00, 0xBE]
49
+ motor_str[0]=0XA0+port
50
+ if speed>100:
51
+ m_par=100
52
+ elif speed>=0 and speed<=100:
53
+ m_par=speed
54
+ elif speed<-100:
55
+ m_par=156
56
+ elif speed<=0 and speed>=-100:
57
+ m_par=256+speed
58
+
59
+ motor_str[4]=m_par
60
+
61
+ # response = base_driver.single_operate_sensor(motor_str,0)
62
+ time.sleep(0.005)
63
+ base_driver.write_data(0X01, 0X02, motor_str)
64
+ # if response == None:
65
+ # return None
66
+ # else:
67
+ return 0
68
+
69
+ #����ֹͣ port:����M�˿ڣ�
70
+ def set_motor_stop(port:bytes) -> Optional[bytes]:
71
+ motor_str=[0xA0, 0x01, 0x0B, 0xBE]
72
+ motor_str[0]=0XA0+port
73
+ # response = base_driver.single_operate_sensor(motor_str,0)
74
+ time.sleep(0.005)
75
+ base_driver.write_data(0X01, 0X02, motor_str)
76
+ # if response == None:
77
+ # return None
78
+ # else:
79
+ return 0
80
+
81
+ #����Ƕȿ��� port:����M�˿ڣ�speed:-100~100��degree:0~65535
82
+ def set_motor_angle(port:bytes,speed:int,degree:int) -> Optional[bytes]:
83
+ motor_str=[0xA0, 0x01, 0x04, 0x81, 0x00, 0x81, 0x00, 0x00, 0xBE]
84
+ motor_str[0]=0XA0+port
85
+
86
+ if speed>100:
87
+ m_par=100
88
+ elif speed>=0 and speed<=100:
89
+ m_par=speed
90
+ elif speed<-100:
91
+ m_par=156
92
+ elif speed<=0 and speed>=-100:
93
+ m_par=256+speed
94
+
95
+ motor_str[4]=m_par
96
+ motor_str[6]=degree//256
97
+ motor_str[7]=degree%256
98
+ # response = base_driver.single_operate_sensor(motor_str,0)
99
+ time.sleep(0.005)
100
+ base_driver.write_data(0X01, 0X02, motor_str)
101
+ # if response == None:
102
+ # return None
103
+ # else:
104
+ return 0
105
+
106
+ #���ﶨʱת�� port:����M�˿ڣ�speed:-100~100��second:1~256
107
+ def set_motor_second(port:bytes,speed:int,second:float) -> Optional[bytes]:
108
+ motor_str=[0xA0, 0x01, 0x08, 0x81, 0x00, 0x82, 0x00, 0x00, 0x00, 0x00, 0xBE]
109
+ motor_str[0]=0XA0+port
110
+
111
+ if speed>100:
112
+ m_par=100
113
+ elif speed>=0 and speed<=100:
114
+ m_par=speed
115
+ elif speed<-100:
116
+ m_par=156
117
+ elif speed<=0 and speed>=-100:
118
+ m_par=256+speed
119
+
120
+ motor_str[4]=m_par
121
+
122
+ byte_data = struct.pack('f', second)
123
+ byte_array = list(byte_data)
124
+
125
+ motor_str[6]=byte_array[0]
126
+ motor_str[7]=byte_array[1]
127
+ motor_str[8]=byte_array[2]
128
+ motor_str[9]=byte_array[3]
129
+
130
+ # response = base_driver.single_operate_sensor(motor_str,0)
131
+ time.sleep(0.005)
132
+ base_driver.write_data(0X01, 0X02, motor_str)
133
+ # if response == None:
134
+ # return None
135
+ # else:
136
+ return 0
137
+
138
+ #���ﶨ��ת�� port:����M�˿ڣ�speed:-100~100
139
+ def set_motor_constspeed(port:bytes,speed:int) -> Optional[bytes]:
140
+ motor_str=[0xA0, 0x01, 0x09, 0x71, 0x00, 0xBE]
141
+ motor_str[0]=0XA0+port
142
+
143
+ if speed>100:
144
+ m_par=100
145
+ elif speed>=0 and speed<=100:
146
+ m_par=speed
147
+ elif speed<-100:
148
+ m_par=156
149
+ elif speed<=0 and speed>=-100:
150
+ m_par=256+speed
151
+
152
+ motor_str[4]=m_par
153
+
154
+ # response = base_driver.single_operate_sensor(motor_str,0)
155
+ time.sleep(0.005)
156
+ base_driver.write_data(0X01, 0X02, motor_str)
157
+ # if response == None:
158
+ # return None
159
+ # else:
160
+ return 0
161
+
162
+ #�����ٶȶ�ȡ port:����M�˿ڣ�
163
+ def get_motor_speed(port:bytes) -> Optional[bytes]:
164
+ motor_str=[0xA0, 0x01, 0x10, 0xBE]
165
+ motor_str[0]=0XA0+port
166
+ time.sleep(0.005)
167
+ response = base_driver.single_operate_sensor(motor_str,0)
168
+ if response == None:
169
+ return None
170
+ else:
171
+ code_data=response[4:-1]
172
+ code_num=int.from_bytes(code_data, byteorder='big', signed=True)
173
+ return code_num
174
+
175
+
176
+
177
+