seed3_command.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # coding:utf-8
3 
4 import serial
5 from threading import Lock
6 
7 class SeedCommand:
8  def __init__(self):
9  pass
10 
11  def open_port(self,port='/dev/ttyACM0',baud=115200):
12  self.ser=serial.Serial(port, baud, timeout=0.1)
13 
14  #COM Port Open
15  def open_com(self):
16  self.ser.write("Z0\r")
17  self.ser.write("O\r")
18 
19  #Com Port Close
20  def close_com(self):
21  self.ser.write("C\r")
22  self.ser.close()
23 
24  #Binary data to Hex String data and decide number of byte
25  def int2str(self,value,byte=1):
26  if value < 0: value = 0xFFFFFF + 1 + value
27  return str(hex(value).upper()[2:][-2:].rjust(2**byte,"0"))
28 
29  #Binary data to Hex data and decide number of byte
30  def int2hex(self,value,byte=1):
31  if value < 0:value = 0xFFFFFF + 1 + value
32  return hex(value).upper()[2:][-2:].rjust(2**byte,"0")
33 
34  #SEED CAN data send
35  def write_buffer(self,send_data):
36  can_data = ''.join(send_data) + '\r'
37  self.ser.write(can_data)
38  #print "send\t:%s" % cand_data
39 
40 ####################################################################################
41  def write_serial_command(self,id_num,d3,d4,d5,d6,d7,d8):
42  send_data = 12*[0]
43  send_data[0] ='t'
44 
45  if d3 == 0x53: send_data[1] = self.int2str(0x00,0)
46  else: send_data[1] = self.int2str(0x03,0)
47 
48  send_data[2] = self.int2str(id_num)
49  send_data[3] = self.int2str(8,0)
50  send_data[4] = self.int2str(0xF0 + id_num)
51  send_data[5] = self.int2str(0x00)
52  send_data[6] = self.int2str(d3)
53  send_data[7] = self.int2str(d4)
54  send_data[8] = self.int2str(d5)
55  send_data[9] = self.int2str(d6)
56  send_data[10] = self.int2str(d7)
57  send_data[11] = self.int2str(d8)
58 
59  self.write_buffer(send_data)
60 
62  ret_str = ''
63  while(self.ser.inWaiting() < 1):
64  pass
65  while(self.ser.read(1) != 't'):
66  pass
67  ret_str = 't'
68  # Get Data Length
69  ret = self.ser.read(4)
70  data_len = int(ret[-1],16)
71  ret_str += ret
72  # Get All data
73  ret = self.ser.read(data_len*2)
74  ret_str += ret
75  #print "Receive Data \t:%s" % ret_str
76  return ret_str
77 
78 #--------- Get inormation command(0x40~0x4F) ---------
79  def get_position(self,id_num):
80  self.ser.flushInput()
81  self.ser.flushOutput()
82  cmd = 0
83  speed = 0
84  position = 0
85 
86  self.write_serial_command(id_num,0x42,id_num,0x00,0x00,0x00,0x00)
87  data = self.read_serial_command()
88  cmd = int(data[9]+data[10],16)
89  if(cmd != 0x42): return [False,0,0]
90  else:
91  speed = int(data[11]+data[12]+data[13]+data[14],16)
92  position = int(data[15]+data[16]+data[17]+data[18]+data[19]+data[20],16)
93 
94  if position > 0xFFFFFF/2: position = position - 0xFFFFFF
95  elif position == 0xFFFFFF: position = 0
96  else : position = position
97 
98  return [True,speed,position]
99 
100 #--------- actuate motor command(0x50~0x5F) ---------
101  def on_servo(self,id_num,data):
102  self.write_serial_command(id_num,0x50,id_num,data,0x00,0x00,0x00)
103 
104  def stop_motor(self,id_num):
105  self.write_serial_command(id_num,0x51,0x00,0x00,0x00,0x00,0x00)
106 
107  def run_script(self,id_num,s_num):
108  if s_num > 0x00 and s_num < 0x0F :
109  self.write_serial_command(id_num,0x5F,id_num,s_num,0x00,0x00,0x00)
110 
111  def wait_for_script_end(self,_number):
112  number_of_end = 0
113  signal = 0
114  if( number_of_end < _number):
115  while(signal != 0xFF):
116  response = self.read_serial_command()
117  signal = int(response[13]+response[14],16)
118  number_of_end += 1
119 
120  def actuate_continuous_absolute_position(self,id_num,time,pos):
121  data = 5*[0]
122 
123  if pos >= 0xFFFFFF/2: pos = 0xFFFFFF/2
124  elif pos <= -0xFFFFFF/2: pos = -0xFFFFFF/2
125 
126  data[0] = time >> 8
127  data[1] = time
128  data[2] = pos >> 16
129  data[3] = pos >> 8
130  data[4] = pos
131 
132  self.write_serial_command(id_num,0x64,data[0],data[1],data[2],data[3],data[4])
def write_serial_command(self, id_num, d3, d4, d5, d6, d7, d8)
def actuate_continuous_absolute_position(self, id_num, time, pos)
def open_port(self, port='/dev/ttyACM0', baud=115200)


seed_smartactuator_sdk
Author(s):
autogenerated on Mon Nov 2 2020 03:39:20