-
Notifications
You must be signed in to change notification settings - Fork 0
/
stepperweblib.py
241 lines (205 loc) · 8.23 KB
/
stepperweblib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/python
# classes to communicate with Nanotech N5-1-3 motor controller via REST API
# authored by Gary Stein at AlphaProto June 2018
# refactored by Brandon Aleson July 2018
# updated by Gary Stein August 2018
# updated 9/3/18
import urllib2
import urllib
import httplib
import struct
import time
import sys
from datetime import datetime
class StepperComms:
# type matters; only listed in documentation or object directory
# Signed 16 (short)
typeS16 = 0
# Unsigned 16 (unsigned short)
typeU16 = 1
# Signed32 (int)
typeS32 = 2
# Unsigned 32 (uint)
typeU32 = 3
# Signed 8 bit (char)
typeS08 = 4
# Unsigned 8 bit (uchar)
typeU08 = 5
def make_url(self, host, index, subindex):
url = "http://%s/od/%04X/%02X" % (host, index, subindex)
return url
def make_value(self, value, type):
svalue = ""
if(type == self.typeS16):
s = struct.pack(">h", value)
c = struct.unpack("<BB", s)
svalue = '"%02X%02X"' % (c)
elif(type == self.typeU16):
s = struct.pack(">H", value)
c = struct.unpack("<BB", s)
svalue = '"%02X%02X"' % (c)
elif(type == self.typeS32):
s = struct.pack(">i", value)
c = struct.unpack("<BBBB", s)
svalue = '"%02X%02X%02X%02X"' % (c)
# unsigned only
elif(type == self.typeU32):
s = struct.pack(">I", value)
c = struct.unpack("<BBBB", s)
svalue = '"%02X%02X%02X%02X"' % (c)
elif(type == self.typeS08):
s = struct.pack(">b", value)
c = struct.unpack("<B", s)
svalue = '"%02X"' % (c)
elif(type == self.typeU08):
s = struct.pack(">B", value)
c = struct.unpack("<B", s)
svalue = '"%02X"' % (c)
return svalue
def set_register(self, host, index, subindex, value, type):
# Create URL scheme
url = self.make_url(host, index, subindex)
# Motor controller needs 1.0 not 1.0
# httplib.HTTPConnection._http_vsn = 10
# httplib.HTTPConnection._http_vsn_str = 'HTTP/1.0'
# value needs to be in Hex with quotes
svalue = self.make_value(value, type)
request = urllib2.Request(url)
# Add to request
request.add_data(svalue)
# Send to server
response = urllib2.urlopen(request)
# response empty on success, check?
# html = response.read()
# print html
response.close()
# Recommends wait after every command
time.sleep(.01)
def get_register(self, host, index, subindex):
# Create URL scheme
url = self.make_url(host, index, subindex)
# Motor controller needs 1.0 not 1.0
# httplib.HTTPConnection._http_vsn = 10
# httplib.HTTPConnection._http_vsn_str = 'HTTP/1.0'
# on get, open url
response = urllib2.urlopen(url)
# Get response (should be hex in quotes)
html = response.read()
# print html
response.close()
# value is retruns in quotes, strip
svalue = html.strip('"')
# Convert to actual int
# Sign might matter here, check
# print svalue
value = int(svalue, 16)
return value
def check_up(self, host):
url = "http://%s/od/6041/00" % (host)
try:
# see if the server is up, timeout 1 second
ret = urllib2.urlopen(url, data=None, timeout=1.0)
return 1
except IOError:
print("Host {} is down: {}".format(host, datetime.today()))
sys.stdout.flush()
return 0
class StepperControl:
ControlWord = 0x6040
StatusWord = 0x6041
ProfileVelocity = 0x6081
TargetPosition = 0x607A
Inputs = 0x60FD
ActualPosition = 0x6064
OperatingMode = 0x6060
InputVoltageRange = 0x3240
ClosedLoop = 0x3202
def __init__(self, host):
self.host = host
self.connected = 0
# Create comms
self.comms = StepperComms()
self.check_controller()
self.initialize_controller()
def initialize_controller(self):
# Set Input 1 to 24 Volt Range
self.comms.set_register(self.host, self.InputVoltageRange, 0x06, 1, self.comms.typeU32)
# Set As Position Mode
self.comms.set_register(self.host, self.OperatingMode, 0x00, 1, self.comms.typeU08)
# Initial Move to set up states
self.move_relative(0, 0)
# Always come up in halt
# self.halt()
def check_controller(self):
count = 0
while(self.comms.check_up(self.host) == 0):
count += 1
# If it ever failed, assume it was down
# and reinitialize
if count > 0:
self.initialize_controller()
# Speed between 0 and 250 (positive only)
# Already ramps up and down, max speed
# Position negative and positive
def move_relative(self, Speed, Steps):
# Always need to check if controller is up
self.check_controller()
# Set speed as unsigned number
self.comms.set_register(self.host, self.ProfileVelocity, 0, Speed, self.comms.typeU32)
# Set relative position in ticks (25000 = 1 revolution of motor shaft)
self.comms.set_register(self.host, self.TargetPosition, 0, Steps, self.comms.typeS32)
# Bit 4 start
# Bit 5 immediate
# Bit 6 Absolute 0 vs Relative 1
# Bit 8 halt
# Bit 9 speed is not changed until target, no braking?
# must be moved to completely
# Quick Stop, Enable Voltage
self.comms.set_register(self.host, self.ControlWord, 0, 0x06, self.comms.typeU16)
# Switch On
self.comms.set_register(self.host, self.ControlWord, 0, 0x07, self.comms.typeU16)
# Enable Operation, Set Relative Motion
self.comms.set_register(self.host, self.ControlWord, 0, 0x4F, self.comms.typeU16)
# Start
self.comms.set_register(self.host, self.ControlWord, 0, 0x5F, self.comms.typeU16)
# Halt and clear everything?
def halt(self):
# Always need to check if controller is up
self.check_controller()
# Halt Free Spin
# self.comms.set_register(self.host, self.ControlWord, 0, 0x1000, self.comms.typeU16)
# Halt powered?
# self.comms.set_register(self.host, self.ControlWord, 0, 0x1007, self.comms.typeU16)
# Cancel move
self.comms.set_register(self.host, self.ControlWord, 0, 0x0007, self.comms.typeU16)
# Hold
self.comms.set_register(self.host, self.ControlWord, 0, 0x000F, self.comms.typeU16)
def check_reached(self):
# Always need to check if controller is up
self.check_controller()
# In status? 0x6041
# Bit 10 Target Reached
# Bit 12 Point Acknowledged
reach = self.comms.get_register(self.host, self.StatusWord, 0)
if(reach & 0x0400):
return 1
else:
return 0
def check_flag(self):
# Always need to check if controller is up
self.check_controller()
flag = self.comms.get_register(self.host, self.Inputs, 0)
# print flag
return 1 if(flag & 0x10000 == 0) else 0
def get_status(self):
# Always need to check if controller is up
self.check_controller()
v = self.comms.get_register(self.host, self.StatusWord, 0)
# print "%04X" % (v)
print(v)
def get_closed(self):
# Always need to check if controller is up
self.check_controller()
v = self.comms.get_register(self.host, self.ClosedLoop, 0)
# print "%04X" % (v)
print(v)