smartpi 0.1.4__py3-none-any.whl → 0.1.6__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.
smartpi/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
1
  __all__ = ["base_driver"]
2
2
 
3
- __version__ = "0.1.4"
3
+ __version__ = "0.1.6"
4
4
 
smartpi/base_driver.py CHANGED
@@ -2,6 +2,7 @@
2
2
  import serial,time,struct
3
3
  from typing import List, Optional
4
4
  from collections import deque
5
+ from . import motor,cw2015,gui
5
6
 
6
7
 
7
8
  # 命令常量
@@ -201,7 +202,7 @@ def read_device_model() -> Optional[bytes]:
201
202
  response =process_received_data()
202
203
  if response:
203
204
  display_data = response[6:-3].decode(errors="ignore")
204
- print(f"设备型号: {display_data}")
205
+ # print(f"设备型号: {display_data}")
205
206
  return display_data
206
207
  else:
207
208
  if time.time() - start_time > 3:
@@ -217,7 +218,7 @@ def read_version() -> Optional[bytes]:
217
218
  response =process_received_data()
218
219
  if response:
219
220
  display_data = response[6:-3].decode(errors="ignore")
220
- print(f"版本号: {display_data}")
221
+ # print(f"版本号: {display_data}")
221
222
  return display_data
222
223
  else:
223
224
  if time.time() - start_time > 3:
@@ -233,7 +234,7 @@ def read_factory_data() -> Optional[bytes]:
233
234
  response =process_received_data()
234
235
  if response:
235
236
  display_data = response[6:-3].decode(errors="ignore")
236
- print(f"厂家信息: {display_data}")
237
+ # print(f"厂家信息: {display_data}")
237
238
  return display_data
238
239
  else:
239
240
  if time.time() - start_time > 3:
@@ -249,7 +250,7 @@ def read_hardware_ID() -> Optional[bytes]:
249
250
  response =process_received_data()
250
251
  if response:
251
252
  display_data = response[6:-3].decode(errors="ignore")
252
- print(f"硬件ID: {display_data}")
253
+ # print(f"硬件ID: {display_data}")
253
254
  return display_data
254
255
  else:
255
256
  if time.time() - start_time > 3:
@@ -265,7 +266,7 @@ def read_device_name() -> Optional[bytes]:
265
266
  response =process_received_data()
266
267
  if response:
267
268
  display_data = response[6:-3].decode(errors="ignore")
268
- print(f"设备名称: {display_data}")
269
+ # print(f"设备名称: {display_data}")
269
270
  return display_data
270
271
  else:
271
272
  if time.time() - start_time > 3:
@@ -282,8 +283,8 @@ def write_device_name(send_data: str) -> Optional[bytes]:
282
283
  response =process_received_data()
283
284
  if response:
284
285
  display_data = response[6:-3].decode(errors="ignore")
285
- print(f"设置状态: {display_data}")
286
- return display_data
286
+ # print(f"设置状态: {display_data}")
287
+ return 0
287
288
  else:
288
289
  if time.time() - start_time > 3:
289
290
  print("读取超时")
@@ -298,7 +299,7 @@ def read_connected() -> Optional[bytes]:
298
299
  response =process_received_data()
299
300
  if response:
300
301
  display_data = response[6:-3].decode(errors="ignore")
301
- print(f"连接方式: {display_data}")
302
+ # print(f"连接方式: {display_data}")
302
303
  return display_data
303
304
  else:
304
305
  if time.time() - start_time > 3:
@@ -306,23 +307,13 @@ def read_connected() -> Optional[bytes]:
306
307
  buffer.clear()
307
308
  return -1
308
309
 
309
- """读取电池电量"""
310
+ """读取电池电量百分比"""
310
311
  def read_battery() -> Optional[bytes]:
311
- write_data(READ_BAT_H, READ_BAT_L)
312
- start_time = time.time()
313
- while True:
314
- response =process_received_data()
315
- if response:
316
- display_data = response[6:-3]
317
- # for x in display_data:
318
- # print(f"{x:02X}", end=' ')
319
- print(f"电池电量: {((display_data[0]*256+display_data[1])/1000):.2f}V")
320
- return display_data
321
- else:
322
- if time.time() - start_time > 3:
323
- print("读取超时")
324
- buffer.clear()
325
- return -1
312
+ sensor = cw2015.CW2015()
313
+ if sensor.init():
314
+ return sensor.get_soc(0)
315
+ else:
316
+ return -1
326
317
 
327
318
  ###############################################################################固件升级
328
319
 
@@ -359,68 +350,68 @@ def read_max_com_len() -> Optional[bytes]:
359
350
  return -1
360
351
 
361
352
  """下载文件的信息"""
362
- def download_massage() -> Optional[bytes]:
363
- write_data(DL_MESSAGE_H, DL_MESSAGE_L, file_data)#文件信息来源从哪获取?
364
- start_time = time.time()
365
- while True:
366
- response =process_received_data()
367
- if response:
368
- display_data = response[6:-3].decode(errors="ignore")
369
- print(f"从机响应: {display_data}")
370
- return display_data
371
- else:
372
- if time.time() - start_time > 3:
373
- print("读取超时")
374
- buffer.clear()
375
- return -1
376
-
377
- """查询设备状态"""
378
- def read_device_status() -> Optional[bytes]:
379
- write_data(READ_STATUS_H, READ_STATUS_L)
380
- start_time = time.time()
381
- while True:
382
- response =process_received_data()
383
- if response:
384
- display_data = response[6:-3].decode(errors="ignore")
385
- print(f"从机响应: {display_data}")
386
- return display_data
387
- else:
388
- if time.time() - start_time > 3:
389
- print("读取超时")
390
- buffer.clear()
391
- return -1
392
-
393
- """发送页校验码"""
394
- def write_page_check() -> Optional[bytes]:
395
- write_data(PAGE_CHECK_H, PAGE_CHECK_L, page_check_data)
396
- start_time = time.time()
397
- while True:
398
- response =process_received_data()
399
- if response:
400
- display_data = response[6:-3].decode(errors="ignore")
401
- print(f"从机响应: {display_data}")
402
- return display_data
403
- else:
404
- if time.time() - start_time > 3:
405
- print("读取超时")
406
- buffer.clear()
407
- return -1
408
-
409
- """发送页数据"""
410
- def write_page_check() -> Optional[bytes]:
411
- write_data(PAGE_SEND_H, PAGE_SEND_L, page_send_data)
412
- start_time = time.time()
413
- while True:
414
- response =process_received_data()
415
- if response:
416
- display_data = response[6:-3].decode(errors="ignore")
417
- print(f"从机响应: {display_data}")
418
- return display_data
419
- else:
420
- if time.time() - start_time > 3:
421
- print("读取超时")
422
- buffer.clear()
423
- return -1
353
+ #def download_massage() -> Optional[bytes]:
354
+ # write_data(DL_MESSAGE_H, DL_MESSAGE_L, file_data)#文件信息来源从哪获取?
355
+ # start_time = time.time()
356
+ # while True:
357
+ # response =process_received_data()
358
+ # if response:
359
+ # display_data = response[6:-3].decode(errors="ignore")
360
+ # print(f"从机响应: {display_data}")
361
+ # return display_data
362
+ # else:
363
+ # if time.time() - start_time > 3:
364
+ # print("读取超时")
365
+ # buffer.clear()
366
+ # return -1
367
+ #
368
+ #"""查询设备状态"""
369
+ #def read_device_status() -> Optional[bytes]:
370
+ # write_data(READ_STATUS_H, READ_STATUS_L)
371
+ # start_time = time.time()
372
+ # while True:
373
+ # response =process_received_data()
374
+ # if response:
375
+ # display_data = response[6:-3].decode(errors="ignore")
376
+ # print(f"从机响应: {display_data}")
377
+ # return display_data
378
+ # else:
379
+ # if time.time() - start_time > 3:
380
+ # print("读取超时")
381
+ # buffer.clear()
382
+ # return -1
383
+ #
384
+ #"""发送页校验码"""
385
+ #def write_page_check() -> Optional[bytes]:
386
+ # write_data(PAGE_CHECK_H, PAGE_CHECK_L, page_check_data)
387
+ # start_time = time.time()
388
+ # while True:
389
+ # response =process_received_data()
390
+ # if response:
391
+ # display_data = response[6:-3].decode(errors="ignore")
392
+ # print(f"从机响应: {display_data}")
393
+ # return display_data
394
+ # else:
395
+ # if time.time() - start_time > 3:
396
+ # print("读取超时")
397
+ # buffer.clear()
398
+ # return -1
399
+ #
400
+ #"""发送页数据"""
401
+ #def write_page_check() -> Optional[bytes]:
402
+ # write_data(PAGE_SEND_H, PAGE_SEND_L, page_send_data)
403
+ # start_time = time.time()
404
+ # while True:
405
+ # response =process_received_data()
406
+ # if response:
407
+ # display_data = response[6:-3].decode(errors="ignore")
408
+ # print(f"从机响应: {display_data}")
409
+ # return display_data
410
+ # else:
411
+ # if time.time() - start_time > 3:
412
+ # print("读取超时")
413
+ # buffer.clear()
414
+ # return -1
424
415
 
425
416
  ###############################################################################读取传感器信息
426
417
 
@@ -506,6 +497,33 @@ def mode_change(send_data: str) -> Optional[bytes]:
506
497
  buffer.clear()
507
498
  return -1
508
499
 
509
-
500
+ """H2-RCU初始化"""
501
+ def smartpi_init():
502
+ uart3_init()
503
+ P_port_init(1)
504
+ P_port_init(2)
505
+ P_port_init(3)
506
+ P_port_init(4)
507
+ P_port_init(5)
508
+ P_port_init(6)
509
+ servo.reset_encode(1)
510
+ servo.reset_encode(2)
511
+ servo.reset_encode(3)
512
+ servo.reset_encode(4)
513
+ servo.reset_encode(5)
514
+ servo.reset_encode(6)
515
+ motor.reset_motor_encoder(1)
516
+ motor.reset_motor_encoder(2)
517
+ motor.reset_motor_encoder(3)
518
+ motor.reset_motor_encoder(4)
519
+ motor.reset_motor_encoder(5)
520
+ motor.reset_motor_encoder(6)
521
+ motor.set_motor(1,0)
522
+ motor.set_motor(2,0)
523
+ motor.set_motor(3,0)
524
+ motor.set_motor(4,0)
525
+ motor.set_motor(5,0)
526
+ motor.set_motor(6,0)
527
+ gui.init()
510
528
 
511
529
 
smartpi/color_sensor.py CHANGED
@@ -3,15 +3,15 @@ import time
3
3
  from typing import List, Optional
4
4
  from smartpi import base_driver
5
5
 
6
- #����ֵ��1-��ɫ��2-��ɫ��3-��ɫ��4-��ɫ��5-��ɫ��6-��ɫ
6
+ #��ɫ��ȡ ��������ֵ��1-��ɫ��2-��ɫ��3-��ɫ��4-��ɫ��5-��ɫ��6-��ɫ�� ��ȡ����-1
7
7
  def get_value(port:bytes) -> Optional[bytes]:
8
8
  color_str=[0xA0, 0x04, 0x00, 0xBE]
9
9
  color_str[0]=0XA0+port
10
10
  color_str[2]=1
11
- response = base_driver.single_operate_sensor(color_str)
12
- if response:
13
- return response[4]
14
- else:
11
+ response = base_driver.single_operate_sensor(color_str)
12
+ if response == -1:
15
13
  return -1
14
+ else:
15
+ return response[4]
16
16
 
17
17
 
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/gui.py ADDED
@@ -0,0 +1,61 @@
1
+ import socket
2
+ import json
3
+ import sys
4
+
5
+ # 连接对象(模块级单例)
6
+ _connection = None
7
+
8
+ def init(host="127.0.0.1", port=65167):
9
+ """初始化GUI连接"""
10
+ global _connection
11
+ if _connection is None:
12
+ _connection = socket.create_connection((host, port))
13
+ _send({"type": "clear"})
14
+
15
+ def _send(cmd):
16
+ """发送命令到服务器"""
17
+ if _connection is None and "pytest" not in sys.modules: # 允许测试环境不初始化
18
+ raise ConnectionError("GUI not initialized. Call gui.init() first.")
19
+ if _connection:
20
+ _connection.sendall((json.dumps(cmd) + "\n").encode())
21
+
22
+ def show_text(x, y, text, color="black", size=16):
23
+ _send({"type": "text", "x": x, "y": y, "text": text, "color": color, "size": size})
24
+
25
+ def print(text): # 使用print作为函数名,因为调用时使用gui.print()
26
+ _send({"type": "print", "text": text})
27
+
28
+ def println(text):
29
+ _send({"type": "println", "text": text})
30
+
31
+ def show_image(x, y, path, width, height):
32
+ _send({"type": "image", "x": x, "y": y, "path": path, "width": width, "height": height})
33
+
34
+ def draw_line(x1, y1, x2, y2, color="black", width=1):
35
+ _send({"type": "line", "x1": x1, "y1": y1, "x2": x2, "y2": y2, "color": color, "width": width})
36
+
37
+ def fill_rect(x, y, w, h, color="black"):
38
+ _send({"type": "fill_rect", "x": x, "y": y, "w": w, "h": h, "color": color})
39
+
40
+ def draw_rect(x, y, w, h, width, color="black"):
41
+ _send({"type": "draw_rect", "x": x, "y": y, "w": w, "h": h, "width": width, "color": color})
42
+
43
+ def fill_circle(cx, cy, r, color="black"):
44
+ _send({"type": "fill_circle", "cx": cx, "cy": cy, "r": r, "color": color})
45
+
46
+ def draw_circle(cx, cy, r, width, color="black"):
47
+ _send({"type": "draw_circle", "cx": cx, "cy": cy, "r": r, "width": width, "color": color})
48
+
49
+ def clear():
50
+ _send({"type": "clear"})
51
+
52
+ def close():
53
+ """关闭GUI连接"""
54
+ global _connection
55
+ if _connection:
56
+ _connection.close()
57
+ _connection = None
58
+
59
+ # 注册程序退出时自动关闭连接
60
+ import atexit
61
+ atexit.register(close)
smartpi/humidity.py CHANGED
@@ -4,17 +4,16 @@ from typing import List, Optional
4
4
  from smartpi import base_driver
5
5
 
6
6
 
7
- #湿度读取 port:连接P端口;
7
+ #湿度读取 port:连接P端口;正常返回:湿度数据; 读取错误:-1
8
8
  def get_value(port:bytes) -> Optional[bytes]:
9
- humiture_str=[0xA0, 0x0C, 0x01, 0x71, 0x00, 0xBE]
10
- humiture_str[0]=0XA0+port
11
- humiture_str[4]=1
12
- response = base_driver.single_operate_sensor(humiture_str)
13
- if response:
14
- return response[4]
15
- else:
9
+ humi_str=[0XA0, 0X0C, 0X01, 0X71, 0X00, 0XBE]
10
+ humi_str[0]=0XA0+port
11
+ humi_str[4]=0X01
12
+ response = base_driver.single_operate_sensor(humi_str)
13
+ if response == -1:
16
14
  return -1
17
-
15
+ else:
16
+ return response[4]
18
17
 
19
18
 
20
19
 
smartpi/led.py CHANGED
@@ -4,7 +4,7 @@ from typing import List, Optional
4
4
  from smartpi import base_driver
5
5
 
6
6
 
7
- #�ʵƿ��� port:����P�˿ڣ�command:0:�صƣ�1:�죻2:�̣�3:����4:�ƣ�5:�ϣ�6:�ࣻ7:��
7
+ #�ʵƿ��� port:����P�˿ڣ�command:0:�صƣ�1:�죻2:�̣�3:����4:�ƣ�5:�ϣ�6:�ࣻ7:�ף� �������أ�0; ��ȡ����-1
8
8
  def set_color(port:bytes,command:bytes) -> Optional[bytes]:
9
9
  color_lamp_str=[0xA0, 0x05, 0x00, 0xBE]
10
10
  color_lamp_str[0]=0XA0+port
smartpi/light_sensor.py CHANGED
@@ -3,40 +3,16 @@ import time
3
3
  from typing import List, Optional
4
4
  from smartpi import base_driver
5
5
 
6
-
6
+ #����ȡ port:����P�˿ڣ� �������أ��Ҷ�����; ��ȡ����-1
7
7
  def get_value(port:bytes) -> Optional[bytes]:
8
8
  light_str=[0xA0, 0x02, 0x00, 0xBE]
9
9
  light_str[0]=0XA0+port
10
10
  light_str[2]=1
11
- response = base_driver.single_operate_sensor(light_str)
12
- if response:
13
- light_data=response[4:-1]
14
- light_num=int.from_bytes(light_data, byteorder='big', signed=True)
15
- return light_num
16
- else:
11
+ response = base_driver.single_operate_sensor(light_str)
12
+ if response == -1:
17
13
  return -1
18
-
19
- def light_on(port:bytes) -> Optional[bytes]:
20
- light_str=[0xA0, 0x02, 0x00, 0xBE]
21
- light_str[0]=0XA0+port
22
- light_str[2]=2
23
- response = base_driver.single_operate_sensor(light_str)
24
- if response:
25
- light_data=response[4:-1]
26
- light_num=int.from_bytes(light_data, byteorder='big', signed=True)
27
- return light_num
28
14
  else:
29
- return -1
30
-
31
- def light_off(port:bytes) -> Optional[bytes]:
32
- light_str=[0xA0, 0x02, 0x00, 0xBE]
33
- light_str[0]=0XA0+port
34
- light_str[2]=3
35
- response = base_driver.single_operate_sensor(light_str)
36
- if response:
37
15
  light_data=response[4:-1]
38
16
  light_num=int.from_bytes(light_data, byteorder='big', signed=True)
39
17
  return light_num
40
- else:
41
- return -1
42
18
 
smartpi/motor.py CHANGED
@@ -8,13 +8,13 @@ from smartpi import base_driver
8
8
  def get_motor_encoder(port:bytes) -> Optional[bytes]:
9
9
  motor_str=[0xA0, 0x01, 0x01, 0xBE]
10
10
  motor_str[0]=0XA0+port
11
- response = base_driver.single_operate_sensor(motor_str)
12
- if response:
11
+ response = base_driver.single_operate_sensor(motor_str)
12
+ if response == -1:
13
+ return -1
14
+ else:
13
15
  code_data=response[4:-1]
14
16
  code_num=int.from_bytes(code_data, byteorder='big', signed=True)
15
17
  return code_num
16
- else:
17
- return -1
18
18
 
19
19
  #����������� port:����M�˿ڣ�
20
20
  def reset_motor_encoder(port:bytes) -> Optional[bytes]:
@@ -135,17 +135,17 @@ def set_motor_constspeed(port:bytes,speed:int) -> Optional[bytes]:
135
135
  else:
136
136
  return -1
137
137
 
138
- #��������ȡ port:����M�˿ڣ�
138
+ #�����ٶȶ�ȡ port:����M�˿ڣ�
139
139
  def get_motor_speed(port:bytes) -> Optional[bytes]:
140
140
  motor_str=[0xA0, 0x01, 0x10, 0xBE]
141
141
  motor_str[0]=0XA0+port
142
- response = base_driver.single_operate_sensor(motor_str)
143
- if response:
142
+ response = base_driver.single_operate_sensor(motor_str)
143
+ if response == -1:
144
+ return -1
145
+ else:
144
146
  code_data=response[4:-1]
145
147
  code_num=int.from_bytes(code_data, byteorder='big', signed=True)
146
- return code_num
147
- else:
148
- return -1
148
+ return code_num
149
149
 
150
150
 
151
151
 
smartpi/move.py ADDED
@@ -0,0 +1,228 @@
1
+ # coding=utf-8
2
+ import time
3
+ from typing import List, Optional
4
+ from smartpi import base_driver
5
+
6
+
7
+ #以速度移动x秒 dir:方向forward、backward、turnright、turnleft ; speed:0~100; second:x秒
8
+ def run_second(dir:bytes,speed:int,second:bytes) -> Optional[bytes]:
9
+ move_str=[0xA0, 0x01, 0x11, 0x71, 0x00, 0x71, 0x00, 0x71, 0x00, 0xBE]
10
+
11
+ if dir=="forward":
12
+ move_str[4]=0x01
13
+ elif dir=="backward":
14
+ move_str[4]=0x02
15
+ elif dir=="turnleft":
16
+ move_str[4]=0x03
17
+ elif dir=="turnright":
18
+ move_str[4]=0x04
19
+
20
+ if speed>100:
21
+ m_par=100
22
+ elif speed>=0 and speed<=100:
23
+ m_par=speed
24
+ elif speed<-100:
25
+ m_par=156
26
+ elif speed<=0 and speed>=-100:
27
+ m_par=256+speed
28
+
29
+ move_str[6]=m_par
30
+ move_str[8]=second
31
+
32
+ response = base_driver.single_operate_sensor(move_str)
33
+ if response == -1:
34
+ return -1
35
+ else:
36
+ return 0
37
+
38
+ #以速度移动x度(编码) dir:方向forward、backward、turnright、turnleft; speed:0~100; angle:x角度
39
+ def run_angle(dir:bytes,speed:bytes,angle:bytes) -> Optional[bytes]:
40
+ move_str=[0xA0, 0x01, 0x12, 0x71, 0x00, 0x71, 0x00, 0x81, 0x00, 0x00, 0xBE]
41
+
42
+ if dir=="forward":
43
+ move_str[4]=0x01
44
+ elif dir=="backward":
45
+ move_str[4]=0x02
46
+ elif dir=="turnright":
47
+ move_str[4]=0x03
48
+ elif dir=="turnleft":
49
+ move_str[4]=0x04
50
+
51
+ if speed>100:
52
+ m_par=100
53
+ elif speed>=0 and speed<=100:
54
+ m_par=speed
55
+ elif speed<-100:
56
+ m_par=156
57
+ elif speed<=0 and speed>=-100:
58
+ m_par=256+speed
59
+
60
+ move_str[6]=m_par
61
+ move_str[8]=angle//256
62
+ move_str[9]=angle%256
63
+
64
+ response = base_driver.single_operate_sensor(move_str)
65
+ if response == -1:
66
+ return -1
67
+ else:
68
+ return 0
69
+
70
+ #以速度移动 dir:方向forward、backward、turnright、turnleft ; speed:0~100;
71
+ def run(dir:bytes,speed:int) -> Optional[bytes]:
72
+ move_str=[0xA0, 0x01, 0x13, 0x71, 0x00, 0x71, 0x00, 0xBE]
73
+
74
+ if dir=="forward":
75
+ move_str[4]=0x01
76
+ elif dir=="backward":
77
+ move_str[4]=0x02
78
+ elif dir=="turnright":
79
+ move_str[4]=0x03
80
+ elif dir=="turnleft":
81
+ move_str[4]=0x04
82
+
83
+ if speed>100:
84
+ m_par=100
85
+ elif speed>=0 and speed<=100:
86
+ m_par=speed
87
+ elif speed<-100:
88
+ m_par=156
89
+ elif speed<=0 and speed>=-100:
90
+ m_par=256+speed
91
+
92
+ move_str[6]=m_par
93
+
94
+ response = base_driver.single_operate_sensor(move_str)
95
+ if response == -1:
96
+ return -1
97
+ else:
98
+ return 0
99
+
100
+ #设置左右轮速度移动x秒 Lspeed:-100~100; Rspeed:-100~100; second:x秒
101
+ def run_speed_second(Lspeed:int,Rspeed:int,second:bytes) -> Optional[bytes]:
102
+ move_str=[0xA0, 0x01, 0x14, 0x71, 0x00, 0x71, 0x00, 0x71, 0x00, 0xBE]
103
+
104
+ if Lspeed>100:
105
+ m_par=100
106
+ elif Lspeed>=0 and Lspeed<=100:
107
+ m_par=Lspeed
108
+ elif Lspeed<-100:
109
+ m_par=156
110
+ elif Lspeed<=0 and Lspeed>=-100:
111
+ m_par=256+Lspeed
112
+
113
+ move_str[4]=m_par
114
+
115
+ if Rspeed>100:
116
+ m_par=100
117
+ elif Rspeed>=0 and Rspeed<=100:
118
+ m_par=Rspeed
119
+ elif Rspeed<-100:
120
+ m_par=156
121
+ elif Rspeed<=0 and Rspeed>=-100:
122
+ m_par=256+Rspeed
123
+
124
+ move_str[6]=m_par
125
+
126
+ move_str[8]=second
127
+
128
+ response = base_driver.single_operate_sensor(move_str)
129
+ if response == -1:
130
+ return -1
131
+ else:
132
+ return 0
133
+
134
+ #设置左右轮速度移动 Lspeed:-100~100; Rspeed:-100~100;
135
+ def run_speed(Lspeed:int,Rspeed:int) -> Optional[bytes]:
136
+ move_str=[0xA0, 0x01, 0x14, 0x71, 0x00, 0x71, 0x00, 0xBE]
137
+
138
+ if Lspeed>100:
139
+ m_par=100
140
+ elif Lspeed>=0 and Lspeed<=100:
141
+ m_par=Lspeed
142
+ elif Lspeed<-100:
143
+ m_par=156
144
+ elif Lspeed<=0 and Lspeed>=-100:
145
+ m_par=256+Lspeed
146
+
147
+ move_str[4]=m_par
148
+
149
+ if Rspeed>100:
150
+ m_par=100
151
+ elif Rspeed>=0 and Rspeed<=100:
152
+ m_par=Rspeed
153
+ elif Rspeed<-100:
154
+ m_par=156
155
+ elif Rspeed<=0 and Rspeed>=-100:
156
+ m_par=256+Rspeed
157
+
158
+ move_str[6]=m_par
159
+
160
+ response = base_driver.single_operate_sensor(move_str)
161
+ if response == -1:
162
+ return -1
163
+ else:
164
+ return 0
165
+
166
+ #设置左右轮功率移动 Lspeed:0~100; Rspeed:0~100;
167
+ def run_power(Lpower:bytes,Rpower:bytes) -> Optional[bytes]:
168
+ move_str=[0xA0, 0x01, 0x17, 0x71, 0x00, 0x71, 0x00, 0xBE]
169
+
170
+ move_str[4]=Lpower
171
+ move_str[6]=Rpower
172
+
173
+ response = base_driver.single_operate_sensor(move_str)
174
+ if response == -1:
175
+ return -1
176
+ else:
177
+ return 0
178
+
179
+ #设置最大功率 Lspeed:0~100; Rspeed:0~100;
180
+ def set_maxpower(M1:bytes,M2:bytes,M3:bytes,M4:bytes,M5:bytes,M6:bytes) -> Optional[bytes]:
181
+ move_str=[0xA0, 0x01, 0x18, 0x71, 0x00, 0x71, 0x00, 0x71, 0x00, 0x71, 0x00, 0x71, 0x00, 0x71, 0x00, 0xBE]
182
+
183
+ move_str[4]=M1
184
+ move_str[6]=M2
185
+ move_str[8]=M3
186
+ move_str[10]=M4
187
+ move_str[12]=M5
188
+ move_str[14]=M6
189
+
190
+ response = base_driver.single_operate_sensor(move_str)
191
+ if response == -1:
192
+ return -1
193
+ else:
194
+ return 0
195
+
196
+ #马达停止
197
+ def stop() -> Optional[bytes]:
198
+ move_str=[0xA0, 0x01, 0x0A, 0xBE]
199
+
200
+ response = base_driver.single_operate_sensor(move_str)
201
+ if response == -1:
202
+ return -1
203
+ else:
204
+ return 0
205
+
206
+ #设置左右轮方向 Lmotor:1~6; Rmotor:1~6; state: no_reversal、all_reversal、left_reversal、right_reversal
207
+ def set_move_init(Lmotor:bytes,Rmotor:bytes,state:bytes) -> Optional[bytes]:
208
+ move_str=[0xA0, 0x01, 0x19, 0x71, 0x00, 0x71, 0x00, 0x71, 0x00, 0xBE]
209
+
210
+ move_str[4]=Lmotor
211
+ move_str[6]=Rmotor
212
+
213
+ if state=="no_reversal":
214
+ move_str[8]=0x01
215
+ elif state=="all_reversal":
216
+ move_str[8]=0x02
217
+ elif state=="left_reversal":
218
+ move_str[8]=0x03
219
+ elif state=="right_reversal":
220
+ move_str[8]=0x04
221
+
222
+ response = base_driver.single_operate_sensor(move_str)
223
+ if response == -1:
224
+ return -1
225
+ else:
226
+ return 0
227
+
228
+
smartpi/servo.py CHANGED
@@ -15,7 +15,7 @@ def steer_angle(port:bytes,angle:bytes) -> Optional[bytes]:
15
15
  else:
16
16
  return -1
17
17
 
18
- #С������� port:����P�˿ڣ�angle:�Ƕ�0~270��second:1~256
18
+ #С�����ʱ���� port:����P�˿ڣ�angle:�Ƕ�0~270��second:1~256
19
19
  def steer_angle_delay(port:bytes,angle:bytes,second:bytes) -> Optional[bytes]:
20
20
  servo_str=[0xA0, 0x10, 0x01, 0x81, 0x00, 0x00, 0x81, 0x00, 0xBE]
21
21
  servo_str[0]=0XA0+port
@@ -28,7 +28,7 @@ def steer_angle_delay(port:bytes,angle:bytes,second:bytes) -> Optional[bytes]:
28
28
  else:
29
29
  return -1
30
30
 
31
- #BE-9528������� port:����P�˿ڣ�angle:�Ƕ�(0~360)��speed:�ٶ�(0~100)��
31
+ #BE-9528������ٶ�ת���Ƕ� port:����P�˿ڣ�angle:�Ƕ�(0~360)��speed:�ٶ�(0~100)��
32
32
  def set_angle_speed(port:bytes,angle:bytes,speed:bytes) -> Optional[bytes]:
33
33
  servo_str=[0xA0, 0x0D, 0x01, 0x81, 0x00, 0x00, 0x81, 0x00, 0xBE]
34
34
  servo_str[0]=0XA0+port
@@ -69,13 +69,13 @@ def set_init(port:bytes) -> Optional[bytes]:
69
69
  def get_angle(port:bytes) -> Optional[bytes]:
70
70
  servo_str=[0xA0, 0x13, 0x01, 0xBE]
71
71
  servo_str[0]=0XA0+port
72
- response = base_driver.single_operate_sensor(servo_str)
73
- if response:
72
+ response = base_driver.single_operate_sensor(servo_str)
73
+ if response == -1:
74
+ return -1
75
+ else:
74
76
  angle_data=response[4:-1]
75
77
  angle_num=int.from_bytes(angle_data, byteorder='big', signed=True)
76
78
  return angle_num
77
- else:
78
- return -1
79
79
 
80
80
  #���ֶ���ٶ�ת�� port:����P�˿ڣ�
81
81
  def set_speed(port:bytes,speed:int) -> Optional[bytes]:
@@ -135,13 +135,13 @@ def reset_encode(port:bytes) -> Optional[bytes]:
135
135
  def get_encoder(port:bytes) -> Optional[bytes]:
136
136
  servo_str=[0xA0, 0x17, 0x01, 0xBE]
137
137
  servo_str[0]=0XA0+port
138
- response = base_driver.single_operate_sensor(servo_str)
139
- if response:
138
+ response = base_driver.single_operate_sensor(servo_str)
139
+ if response == -1:
140
+ return -1
141
+ else:
140
142
  code_data=response[4:-1]
141
143
  code_num=int.from_bytes(code_data, byteorder='big', signed=True)
142
144
  return code_num
143
- else:
144
- return -1
145
145
 
146
146
 
147
147
 
smartpi/temperature.py CHANGED
@@ -4,14 +4,14 @@ from typing import List, Optional
4
4
  from smartpi import base_driver
5
5
 
6
6
 
7
- #�¶ȶ�ȡ port:����P�˿ڣ�
7
+ #�¶ȶ�ȡ port:����P�˿ڣ��������أ��¶�����; ��ȡ����-1
8
8
  def get_value(port:bytes) -> Optional[bytes]:
9
- temp_str=[0xA0, 0x0C, 0x01, 0x71, 0x00, 0xBE]
9
+ temp_str=[0XA0, 0X0C, 0X01, 0X71, 0X00, 0XBE]
10
10
  temp_str[0]=0XA0+port
11
11
  temp_str[4]=0
12
12
  response = base_driver.single_operate_sensor(temp_str)
13
- if response:
14
- return response[4]
15
- else:
13
+ if response == -1:
16
14
  return -1
15
+ else:
16
+ return response[4]
17
17
 
smartpi/touch_sensor.py CHANGED
@@ -3,13 +3,13 @@ import time
3
3
  from typing import List, Optional
4
4
  from smartpi import base_driver
5
5
 
6
- #���������� port:����P�˿�
6
+ #���������� port:����P�˿� �������أ�1��0; ��ȡ����-1
7
7
  def get_value(port:bytes) -> Optional[bytes]:
8
8
  read_sw_str=[0xA0, 0x03, 0x01, 0xBE]
9
9
  read_sw_str[0]=0XA0+port
10
10
  response = base_driver.single_operate_sensor(read_sw_str)
11
- if response:
12
- return response[4]
13
- else:
11
+ if response == -1:
14
12
  return -1
13
+ else:
14
+ return response[4]
15
15
 
smartpi/ultrasonic.py CHANGED
@@ -3,16 +3,17 @@ import time
3
3
  from typing import List, Optional
4
4
  from smartpi import base_driver
5
5
 
6
-
6
+ #��������ഫ���� port:����P�˿� �������أ��������; ��ȡ����-1
7
7
  def get_value(port:bytes) -> Optional[bytes]:
8
8
  ultrasonic_str=[0xA0, 0x06, 0x00, 0xBE]
9
9
  ultrasonic_str[0]=0XA0+port
10
10
  ultrasonic_str[2]=1
11
11
  response = base_driver.single_operate_sensor(ultrasonic_str)
12
- if response:
12
+
13
+ if response == -1:
14
+ return -1
15
+ else:
13
16
  distance_data=response[4:-1]
14
17
  distance_num=int.from_bytes(distance_data, byteorder='big', signed=True)
15
18
  return distance_num
16
- else:
17
- return -1
18
19
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: smartpi
3
- Version: 0.1.4
3
+ Version: 0.1.6
4
4
  Summary: A library use for H2-RCU
5
5
  Author: ZMROBO
6
6
  Classifier: Programming Language :: Python :: 3
@@ -0,0 +1,18 @@
1
+ smartpi/__init__.py,sha256=0B0ifjRnjFk7ubY8GaCY6TAxKq72V5fx3gH_rqOu5yE,54
2
+ smartpi/base_driver.py,sha256=wYfho3veNKaL0mr_9UnEo2MSmsaKIAZY83UkiDWTWCw,17810
3
+ smartpi/color_sensor.py,sha256=IubJb8zd87oWf3qTn07wTHM2vjUADwgSdXUChGgovHw,492
4
+ smartpi/cw2015.py,sha256=1lAF-pi_ye_ya1AZQS1sjbgsDf7MThO5IAskKsNGBzA,5695
5
+ smartpi/gui.py,sha256=E98_soyWbEf_dwYhXZgMSXrgY5QuYoDTuFCPK63flIQ,2102
6
+ smartpi/humidity.py,sha256=-awlSiy7ea6iwtirUMdDAMmmdIgVQZ3D7cDzgXmy314,493
7
+ smartpi/led.py,sha256=92SBKjQbg1HN6rtKRH1NNWvAFRBP2-5OUGM9muOvhyQ,526
8
+ smartpi/light_sensor.py,sha256=wsW0_ZnKeHN6IWRH3CIybKLdHPVK2I8jSTCqXlUECVk,565
9
+ smartpi/motor.py,sha256=ePBKQu7FSrqjnM-d1kDcT1nYMLpnLeALJ0agI2dznoQ,4447
10
+ smartpi/move.py,sha256=1LpNlveTJLnObyD4nzbHIC0rlkPuPUwPtnF3sgr1t6g,6599
11
+ smartpi/servo.py,sha256=3cGyBattPY0nXQvJl3b462VGyyzca7S33X_9xIp2tUQ,4533
12
+ smartpi/temperature.py,sha256=JeuiwsV6AUjnFQsQxgvjM3AuBrVvK5DuSE-BbX1Di2s,456
13
+ smartpi/touch_sensor.py,sha256=jlKqvcoSmfZzH9zJeafBeVEQhxIYkkzXe4_fifX0COI,434
14
+ smartpi/ultrasonic.py,sha256=5MS0EdQNirAl8iZbdwZ63fnjp6axHRZHw6dJwcwuXnQ,618
15
+ smartpi-0.1.6.dist-info/METADATA,sha256=7Vgj7YD3AWgt5LM-IUQxXqvT__TVMf-6scr1I7K8fWw,310
16
+ smartpi-0.1.6.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
17
+ smartpi-0.1.6.dist-info/top_level.txt,sha256=PoLhUCmWAiQUg5UeN2fS-Y1iQyBbF2rdUlizXtpHGRQ,8
18
+ smartpi-0.1.6.dist-info/RECORD,,
@@ -1,15 +0,0 @@
1
- smartpi/__init__.py,sha256=Pavr-N-hqEpqeTD1ck9SZw9vGtn_Ju7cbQF81qkkDxg,54
2
- smartpi/base_driver.py,sha256=xnAfn8PjaJUhlFLxKRXDbtKY5nmp_yiDGtx4lyHEp9U,17441
3
- smartpi/color_sensor.py,sha256=HJAUGO8CPaoodgLwgzx_LVdvf0MoG63fg91-Y5Qp3fk,446
4
- smartpi/humidity.py,sha256=Y8vLrK8FD5hdR_KshQxka2ygfJcCbuWs48gGr7zBqu4,456
5
- smartpi/led.py,sha256=f8K7ebHFy9kIlCLWKEyQbmPQv4z2TMTNGs1vaKOtMAs,495
6
- smartpi/light_sensor.py,sha256=QdY5jB_yg0mh3r7eUBsULqAEmQgaQgaUQHnKR0z3FAs,1279
7
- smartpi/motor.py,sha256=igk9OFJjsS4nq16Sfl8s1ok8qolALreTFWXUeuDmHi4,4424
8
- smartpi/servo.py,sha256=NFaoiDPdgs6wUklNWhL175CHIF0dJSRNDyOYRfum1xM,4492
9
- smartpi/temperature.py,sha256=FUSoR7FeLZ4a_xj7ej60jXqXbX1frUTz3wDXzSTkob8,418
10
- smartpi/touch_sensor.py,sha256=Y491KYiQBljX3IAvldld3Z8nXkBOAqqx7cTXda3Y-tA,396
11
- smartpi/ultrasonic.py,sha256=lGPJRxDZN_uIIxqFMUa715miuN7ZXoKJ9npD03dyuXU,539
12
- smartpi-0.1.4.dist-info/METADATA,sha256=fevHCCy6ZRYLCt1tl8cHMi-eIC8WHRIMc768xVPaIzo,310
13
- smartpi-0.1.4.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
14
- smartpi-0.1.4.dist-info/top_level.txt,sha256=PoLhUCmWAiQUg5UeN2fS-Y1iQyBbF2rdUlizXtpHGRQ,8
15
- smartpi-0.1.4.dist-info/RECORD,,