moons-motor 0.0.9__tar.gz → 0.0.11__tar.gz

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: moons_motor
3
- Version: 0.0.9
3
+ Version: 0.0.11
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
@@ -213,6 +213,37 @@ class MoonsStepper(Subject):
213
213
  else:
214
214
  print(f"Target device is not opened. Command: {command}")
215
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
+
216
247
  # endregion
217
248
 
218
249
  # region motor motion functions
@@ -261,7 +292,7 @@ class MoonsStepper(Subject):
261
292
  def calibrate(self, motor_address="", speed=0.3, onStart=None, onComplete=None):
262
293
  self.send(self.addressed_cmd(motor_address, "VE{}".format(speed)))
263
294
  # time.sleep(0.01)
264
- self.send(self.addressed_cmd(motor_address, "DI10"))
295
+ # self.send(self.addressed_cmd(motor_address, "DI10"))
265
296
  # time.sleep(0.01)
266
297
  self.send(self.addressed_cmd(motor_address, "SH3F"))
267
298
  # time.sleep(0.01)
@@ -281,63 +312,93 @@ class MoonsStepper(Subject):
281
312
  # region motor status functions
282
313
  def get_position(self, motor_address):
283
314
  self.send(self.addressed_cmd(motor_address, "IP"))
284
- self.new_value_event.wait(timeout=0.5)
285
- return self.get_value()
315
+ return self.read()
316
+ # self.new_value_event.wait(timeout=0.5)
317
+ # return self.get_value()
286
318
 
287
319
  def get_temperature(self, motor_address):
288
320
  self.send(self.addressed_cmd(motor_address, "IT"))
289
- self.new_value_event.wait(timeout=0.5)
290
- 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
291
324
 
292
325
  def get_sensor_status(self, motor_address):
293
326
  self.send(self.addressed_cmd(motor_address, "IS"))
294
- self.new_value_event.wait(timeout=0.5)
295
- return self.get_value()
327
+ return self.read()
328
+ # self.new_value_event.wait(timeout=0.5)
329
+ # return self.get_value()
296
330
 
297
331
  def get_votalge(self, motor_address):
298
332
  self.send(self.addressed_cmd(motor_address, "IU"))
299
- self.new_value_event.wait(timeout=0.5)
300
- return self.get_value()
333
+ return self.read()
334
+ # self.new_value_event.wait(timeout=0.5)
335
+ # return self.get_value()
301
336
 
302
337
  def get_acceleration(self, motor_address):
303
338
  self.send(self.addressed_cmd(motor_address, "AC"))
304
- self.new_value_event.wait(timeout=0.5)
305
- return self.get_value()
339
+ return self.read()
340
+ # self.new_value_event.wait(timeout=0.5)
341
+ # return self.get_value()
306
342
 
307
343
  def get_deceleration(self, motor_address):
308
344
  self.send(self.addressed_cmd(motor_address, "DE"))
309
- self.new_value_event.wait(timeout=0.5)
310
- return self.get_value()
345
+ return self.read()
346
+ # self.new_value_event.wait(timeout=0.5)
347
+ # return self.get_value()
311
348
 
312
349
  def get_velocity(self, motor_address):
313
350
  self.send(self.addressed_cmd(motor_address, "VE"))
314
- self.new_value_event.wait(timeout=0.5)
315
- return self.get_value()
351
+ return self.read()
352
+ # self.new_value_event.wait(timeout=0.5)
353
+ # return self.get_value()
316
354
 
317
355
  def get_distance(self, motor_address):
318
356
  self.send(self.addressed_cmd(motor_address, "DI"))
319
- self.new_value_event.wait(timeout=0.5)
320
- return self.get_value()
357
+ return self.read()
358
+ # self.new_value_event.wait(timeout=0.5)
359
+ # return self.get_value()
321
360
 
322
361
  def get_jog_speed(self, motor_address):
323
362
  self.send(self.addressed_cmd(motor_address, "JS"))
324
- self.new_value_event.wait(timeout=0.5)
325
- return self.get_value()
363
+ # self.new_value_event.wait(timeout=0.5)
364
+ # return self.get_value()
365
+ return self.read()
326
366
 
327
- def get_info(self, motor_address):
367
+ def get_info(self, motor_address, progress=None):
328
368
  self.set_return_format_dexcimal(motor_address)
329
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))
330
391
  info = {
331
- "pos": str(self.get_position(motor_address)),
332
- "temp": str(self.get_temperature(motor_address)),
333
- "sensor": str(self.get_sensor_status(motor_address)),
334
- "vol": str(self.get_votalge(motor_address)),
335
- "accel": str(self.get_acceleration(motor_address)),
336
- "decel": str(self.get_deceleration(motor_address)),
337
- "vel": str(self.get_velocity(motor_address)),
338
- "dis": str(self.get_distance(motor_address)),
339
- "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,
340
398
  }
399
+ if progress:
400
+ progress(round(7 / totalInfoCount))
401
+
341
402
  return info
342
403
 
343
404
  def get_status(self, motor_address) -> str:
@@ -363,26 +424,37 @@ class MoonsStepper(Subject):
363
424
  return f"~{command}"
364
425
  return f"{motor_address}{command}"
365
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
+
366
437
  def get_value(self):
367
438
  print("Waiting for value")
368
439
  self.new_data_event.wait(timeout=0.5)
369
440
  print("Recv:" + self.listeningBufferPre)
370
441
  self.new_data_event.clear()
371
- if "%" in self.listeningBufferPre:
372
- return "success_ack"
373
- if "?" in self.listeningBufferPre:
374
- return "fail_ack"
375
- if "*" in self.listeningBufferPre:
376
- return "buffered_ack"
377
- self.new_value_event.set()
378
- pattern = r"=(\w+(?:\.\w+)?|\d+(?:\.\d+)?)"
379
- result = re.search(pattern, self.listeningBufferPre)
380
- self.listeningBufferPre = ""
381
- self.new_value_event.clear()
382
- if result:
383
- return result.group(1)
384
- else:
385
- 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"
386
458
 
387
459
 
388
460
  # endregion
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.1
1
+ Metadata-Version: 2.2
2
2
  Name: moons_motor
3
- Version: 0.0.9
3
+ Version: 0.0.11
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
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "moons_motor"
7
- version = "0.0.9"
7
+ version = "0.0.11"
8
8
  authors = [{ name = "miroc", email = "mike8503111@gmail.com" }]
9
9
  description = "This is a python library for controlling the Moons' motor through the serial port."
10
10
  readme = "README.md"
File without changes
File without changes
File without changes
File without changes