moons-motor 0.0.8__py3-none-any.whl → 0.0.10__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
moons_motor/motor.py CHANGED
@@ -147,25 +147,28 @@ class MoonsStepper(Subject):
147
147
  ports = list(list_ports.comports())
148
148
  for p in ports:
149
149
  m = re.match(
150
- r"USB\s*VID:PID=(\w+):(\w+)\s*SER=([A-Za-z0-9]+)", p.usb_info()
150
+ r"USB\s*VID:PID=(\w+):(\w+)\s*SER=([A-Za-z0-9]*)", p.usb_info()
151
151
  )
152
+ print(m, p.usb_info())
152
153
  if (
153
154
  m
154
155
  and m.group(1) == self.VID
155
156
  and m.group(2) == self.PID
156
- and m.group(3) == self.SERIAL_NUM
157
+ # and m.group(3) == self.SERIAL_NUM
157
158
  ):
158
- print(
159
- f"Device: {p.description} | VID: {m.group(1)} | PID: {m.group(2)} | SER: {m.group(3)} connected"
160
- )
161
-
162
- self.device = p.description
163
-
164
- attempt_connect(p.device, baudrate)
165
- if callback:
166
- callback(self.device, self.Opened)
159
+ print("find vid pid match")
160
+ if m.group(3) == self.SERIAL_NUM or self.SERIAL_NUM == "":
161
+ print(
162
+ f"Device: {p.description} | VID: {m.group(1)} | PID: {m.group(2)} | SER: {m.group(3)} connected"
163
+ )
164
+
165
+ self.device = p.description
166
+
167
+ attempt_connect(p.device, baudrate)
168
+ if callback:
169
+ callback(self.device, self.Opened)
170
+ break
167
171
  break
168
- break
169
172
 
170
173
  if self.only_simulate:
171
174
  self.device = "Simulate"
@@ -210,6 +213,37 @@ class MoonsStepper(Subject):
210
213
  else:
211
214
  print(f"Target device is not opened. Command: {command}")
212
215
 
216
+ def read(self, timeout=1):
217
+ if self.ser is not None and self.ser.is_open:
218
+ print("reading...")
219
+ try:
220
+ start_time = time.time()
221
+ while time.time() - start_time < timeout:
222
+ if self.ser.in_waiting > 0:
223
+ response = self.ser.read(self.ser.in_waiting)
224
+ response = response.decode("utf-8").strip()
225
+ if self.is_log_message:
226
+ print(
227
+ f"[bold blue]Recv from {self.device} :[/bold blue] {response}"
228
+ )
229
+ return response
230
+ time.sleep(0.01)
231
+ print("reading timeout")
232
+ return None
233
+ except Exception as e:
234
+ print(f"Error when reading serial port: {str(e)}")
235
+ return None
236
+ elif self.only_simulate:
237
+ simulated_response = "simulate response"
238
+ if self.is_log_message:
239
+ print(
240
+ f"[bold blue]Recv from simulate device:[/bold blue] {simulated_response}"
241
+ )
242
+ return simulated_response
243
+ else:
244
+ print("Device not open, read fail.")
245
+ return None
246
+
213
247
  # endregion
214
248
 
215
249
  # region motor motion functions
@@ -278,63 +312,93 @@ class MoonsStepper(Subject):
278
312
  # region motor status functions
279
313
  def get_position(self, motor_address):
280
314
  self.send(self.addressed_cmd(motor_address, "IP"))
281
- self.new_value_event.wait(timeout=0.5)
282
- return self.get_value()
315
+ return self.read()
316
+ # self.new_value_event.wait(timeout=0.5)
317
+ # return self.get_value()
283
318
 
284
319
  def get_temperature(self, motor_address):
285
320
  self.send(self.addressed_cmd(motor_address, "IT"))
286
- self.new_value_event.wait(timeout=0.5)
287
- return int(self.get_value()) / 10
321
+ # self.new_value_event.wait(timeout=0.5)
322
+ return self.read()
323
+ # return int(self.get_value()) / 10
288
324
 
289
325
  def get_sensor_status(self, motor_address):
290
326
  self.send(self.addressed_cmd(motor_address, "IS"))
291
- self.new_value_event.wait(timeout=0.5)
292
- return self.get_value()
327
+ return self.read()
328
+ # self.new_value_event.wait(timeout=0.5)
329
+ # return self.get_value()
293
330
 
294
331
  def get_votalge(self, motor_address):
295
332
  self.send(self.addressed_cmd(motor_address, "IU"))
296
- self.new_value_event.wait(timeout=0.5)
297
- return self.get_value()
333
+ return self.read()
334
+ # self.new_value_event.wait(timeout=0.5)
335
+ # return self.get_value()
298
336
 
299
337
  def get_acceleration(self, motor_address):
300
338
  self.send(self.addressed_cmd(motor_address, "AC"))
301
- self.new_value_event.wait(timeout=0.5)
302
- return self.get_value()
339
+ return self.read()
340
+ # self.new_value_event.wait(timeout=0.5)
341
+ # return self.get_value()
303
342
 
304
343
  def get_deceleration(self, motor_address):
305
344
  self.send(self.addressed_cmd(motor_address, "DE"))
306
- self.new_value_event.wait(timeout=0.5)
307
- return self.get_value()
345
+ return self.read()
346
+ # self.new_value_event.wait(timeout=0.5)
347
+ # return self.get_value()
308
348
 
309
349
  def get_velocity(self, motor_address):
310
350
  self.send(self.addressed_cmd(motor_address, "VE"))
311
- self.new_value_event.wait(timeout=0.5)
312
- return self.get_value()
351
+ return self.read()
352
+ # self.new_value_event.wait(timeout=0.5)
353
+ # return self.get_value()
313
354
 
314
355
  def get_distance(self, motor_address):
315
356
  self.send(self.addressed_cmd(motor_address, "DI"))
316
- self.new_value_event.wait(timeout=0.5)
317
- return self.get_value()
357
+ return self.read()
358
+ # self.new_value_event.wait(timeout=0.5)
359
+ # return self.get_value()
318
360
 
319
361
  def get_jog_speed(self, motor_address):
320
362
  self.send(self.addressed_cmd(motor_address, "JS"))
321
- self.new_value_event.wait(timeout=0.5)
322
- return self.get_value()
363
+ # self.new_value_event.wait(timeout=0.5)
364
+ # return self.get_value()
365
+ return self.read()
323
366
 
324
- def get_info(self, motor_address):
367
+ def get_info(self, motor_address, progress=None):
325
368
  self.set_return_format_dexcimal(motor_address)
326
369
  self.motor_wait(motor_address, 0.1)
370
+ totalInfoCount = 7
371
+ pos = self.extractValueFromResponse(self.get_position(motor_address))
372
+ if progress:
373
+ progress(round(1 / totalInfoCount, 1))
374
+ temp = (
375
+ int(self.extractValueFromResponse(self.get_temperature(motor_address))) / 10
376
+ )
377
+ if progress:
378
+ progress(round(2 / totalInfoCount, 1))
379
+ vol = int(self.extractValueFromResponse(self.get_votalge(motor_address))) / 10
380
+ if progress:
381
+ progress(round(3 / totalInfoCount, 1))
382
+ accel = self.extractValueFromResponse(self.get_acceleration(motor_address))
383
+ if progress:
384
+ progress(round(4 / totalInfoCount, 1))
385
+ decel = self.extractValueFromResponse(self.get_deceleration(motor_address))
386
+ if progress:
387
+ progress(round(5 / totalInfoCount, 1))
388
+ jogsp = self.extractValueFromResponse(self.get_jog_speed(motor_address))
389
+ if progress:
390
+ progress(round(6 / totalInfoCount, 1))
327
391
  info = {
328
- "pos": str(self.get_position(motor_address)),
329
- "temp": str(self.get_temperature(motor_address)),
330
- "sensor": str(self.get_sensor_status(motor_address)),
331
- "vol": str(self.get_votalge(motor_address)),
332
- "accel": str(self.get_acceleration(motor_address)),
333
- "decel": str(self.get_deceleration(motor_address)),
334
- "vel": str(self.get_velocity(motor_address)),
335
- "dis": str(self.get_distance(motor_address)),
336
- "jogsp": str(self.get_jog_speed(motor_address)),
392
+ "pos": pos,
393
+ "temp": temp,
394
+ "vol": vol,
395
+ "accel": accel,
396
+ "decel": decel,
397
+ "jogsp": jogsp,
337
398
  }
399
+ if progress:
400
+ progress(round(7 / totalInfoCount))
401
+
338
402
  return info
339
403
 
340
404
  def get_status(self, motor_address) -> str:
@@ -360,26 +424,37 @@ class MoonsStepper(Subject):
360
424
  return f"~{command}"
361
425
  return f"{motor_address}{command}"
362
426
 
427
+ def extractValueFromResponse(self, response):
428
+ pattern = r"=(.*)"
429
+ if response == None:
430
+ return None
431
+ result = re.search(pattern, response)
432
+ if result:
433
+ return result.group(1)
434
+ else:
435
+ return None
436
+
363
437
  def get_value(self):
364
438
  print("Waiting for value")
365
439
  self.new_data_event.wait(timeout=0.5)
366
440
  print("Recv:" + self.listeningBufferPre)
367
441
  self.new_data_event.clear()
368
- if "%" in self.listeningBufferPre:
369
- return "success_ack"
370
- if "?" in self.listeningBufferPre:
371
- return "fail_ack"
372
- if "*" in self.listeningBufferPre:
373
- return "buffered_ack"
374
- self.new_value_event.set()
375
- pattern = r"=(\w+(?:\.\w+)?|\d+(?:\.\d+)?)"
376
- result = re.search(pattern, self.listeningBufferPre)
377
- self.listeningBufferPre = ""
378
- self.new_value_event.clear()
379
- if result:
380
- return result.group(1)
381
- else:
382
- return "No_value_found"
442
+ return self.listeningBufferPre
443
+ # if "%" in self.listeningBufferPre:
444
+ # return "success_ack"
445
+ # if "?" in self.listeningBufferPre:
446
+ # return "fail_ack"
447
+ # if "*" in self.listeningBufferPre:
448
+ # return "buffered_ack"
449
+ # self.new_value_event.set()
450
+ # pattern = r"=(\w+(?:\.\w+)?|\d+(?:\.\d+)?)"
451
+ # result = re.search(pattern, self.listeningBufferPre)
452
+ # self.listeningBufferPre = ""
453
+ # self.new_value_event.clear()
454
+ # if result:
455
+ # return result.group(1)
456
+ # else:
457
+ # return "No_value_found"
383
458
 
384
459
 
385
460
  # endregion
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: moons_motor
3
- Version: 0.0.8
3
+ Version: 0.0.10
4
4
  Summary: This is a python library for controlling the Moons' motor through the serial port.
5
5
  Author-email: miroc <mike8503111@gmail.com>
6
6
  Project-URL: Repository, https://github.com/miroc99/moons_motor.git
@@ -22,15 +22,20 @@ Requires-Dist: requests
22
22
  This is a python library for control moons motor through serial port.
23
23
 
24
24
  ## Compatibility
25
+
25
26
  Now only support Windows.
26
27
 
27
28
  ## Installing
29
+
28
30
  Install through `pip`
31
+
29
32
  ```bash
30
33
  python -m pip install moons_motor
31
34
 
32
35
  ```
36
+
33
37
  ## Usage
38
+
34
39
  ```python
35
40
  from motor import MoonsStepper, StepperModules
36
41
  import simulate
@@ -46,7 +51,9 @@ sleep(5)
46
51
  motor.stop_jog()
47
52
 
48
53
  ```
54
+
49
55
  ## Tested Motor
56
+
50
57
  1. STM17S-3RN
51
58
 
52
59
  ## Reference
@@ -0,0 +1,11 @@
1
+ moons_motor/__init__.py,sha256=qOpsRwizV-DpKSvNzyvj8ju3cs6vwgIICur1Oe6sxOA,27
2
+ moons_motor/motor.py,sha256=BA_dMHJeqWuUEknL2lMGgGXt7PH5hFsl5L7spiFnB2Q,16075
3
+ moons_motor/observer.py,sha256=PXzuPYKRb2HpjArJcD8HakYIPfFGAs1uBDIL8PSizgA,124
4
+ moons_motor/simulate.py,sha256=J0y1fZhoOim9i-BAkprxnPern1SAdkDfKPqT2MWyDwU,2561
5
+ moons_motor/status.py,sha256=jXQZFZTt9ugHktkWKLII8MpEQQaeO-UjlwTrrP4LJNE,2872
6
+ moons_motor/subject.py,sha256=L_GS6fvJTeX7X23o3T92oiZ4rtLVKA2OEd9GpHn_Dz4,445
7
+ moons_motor-0.0.10.dist-info/LICENSE,sha256=nsYjO800SjIjI85y2kVHR5mC3tca2vs4kK_BhNe89bM,1074
8
+ moons_motor-0.0.10.dist-info/METADATA,sha256=mnp8YTh-_GclQE8tS44AaHdgMVwGx2-W5Sz3H9vmcZo,1282
9
+ moons_motor-0.0.10.dist-info/WHEEL,sha256=OVMc5UfuAQiSplgO0_WdW7vXVGAt9Hdd6qtN4HotdyA,91
10
+ moons_motor-0.0.10.dist-info/top_level.txt,sha256=0dE-CR5_NYBw34jHIDGQNWpMllzO6mtUIuKyRv_rJLg,12
11
+ moons_motor-0.0.10.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (74.0.0)
2
+ Generator: setuptools (75.2.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,11 +0,0 @@
1
- moons_motor/__init__.py,sha256=qOpsRwizV-DpKSvNzyvj8ju3cs6vwgIICur1Oe6sxOA,27
2
- moons_motor/motor.py,sha256=KM2DIHB3RRx21pMPp7q6q1XNFDfSoiNV8Lk0jaqYyD4,13221
3
- moons_motor/observer.py,sha256=PXzuPYKRb2HpjArJcD8HakYIPfFGAs1uBDIL8PSizgA,124
4
- moons_motor/simulate.py,sha256=J0y1fZhoOim9i-BAkprxnPern1SAdkDfKPqT2MWyDwU,2561
5
- moons_motor/status.py,sha256=jXQZFZTt9ugHktkWKLII8MpEQQaeO-UjlwTrrP4LJNE,2872
6
- moons_motor/subject.py,sha256=L_GS6fvJTeX7X23o3T92oiZ4rtLVKA2OEd9GpHn_Dz4,445
7
- moons_motor-0.0.8.dist-info/LICENSE,sha256=nsYjO800SjIjI85y2kVHR5mC3tca2vs4kK_BhNe89bM,1074
8
- moons_motor-0.0.8.dist-info/METADATA,sha256=qbzG4yrvxm5yjawnb_4zGG2f7hPhQtQJSyYSDNQ3Sgk,1267
9
- moons_motor-0.0.8.dist-info/WHEEL,sha256=UvcQYKBHoFqaQd6LKyqHw9fxEolWLQnlzP0h_LgJAfI,91
10
- moons_motor-0.0.8.dist-info/top_level.txt,sha256=0dE-CR5_NYBw34jHIDGQNWpMllzO6mtUIuKyRv_rJLg,12
11
- moons_motor-0.0.8.dist-info/RECORD,,