1 |
#!/usr/bin/python |
1 |
#!/usr/bin/python |
2 |
# ------------------------------------------- |
2 |
# ------------------------------------------- |
3 |
# HBSTEP01B Stepper Motor control test code |
3 |
# HBSTEP01B Stepper Motor control test code |
4 |
# ------------------------------------------- |
4 |
# ------------------------------------------- |
5 |
# |
5 |
# |
6 |
# Program uses MLAB Python modules library from https://github.com/MLAB-project/pymlab |
6 |
# Program uses MLAB Python modules library from https://github.com/MLAB-project/pymlab |
7 |
|
7 |
|
8 |
|
8 |
|
9 |
#uncomment for debbug purposes |
9 |
#uncomment for debbug purposes |
10 |
#import logging |
10 |
#import logging |
11 |
#logging.basicConfig(level=logging.DEBUG) |
11 |
#logging.basicConfig(level=logging.DEBUG) |
12 |
|
12 |
|
13 |
import sys |
13 |
import sys |
14 |
import time |
14 |
import time |
15 |
from pymlab import config |
15 |
from pymlab import config |
16 |
import pylirc # infrared receiver binding |
16 |
import pylirc # infrared receiver binding |
17 |
|
17 |
|
18 |
#### Script Arguments ############################################### |
18 |
#### Script Arguments ############################################### |
19 |
|
19 |
|
20 |
if len(sys.argv) == 2: |
20 |
if len(sys.argv) == 2: |
21 |
SPEED = eval(sys.argv[1]) |
21 |
SPEED = eval(sys.argv[1]) |
22 |
|
22 |
|
23 |
else: |
23 |
else: |
24 |
sys.stderr.write("Invalid number of arguments.\n") |
24 |
sys.stderr.write("Invalid number of arguments.\n") |
25 |
sys.stderr.write("Usage: %s BASE_SPEED (in steps/s)\n" % (sys.argv[0], )) |
25 |
sys.stderr.write("Usage: %s BASE_SPEED (in steps/s)\n" % (sys.argv[0], )) |
26 |
sys.exit(1) |
26 |
sys.exit(1) |
27 |
|
27 |
|
28 |
# Begin of Class Axis -------------------------------------------------- |
28 |
# Begin of Class Axis -------------------------------------------------- |
29 |
|
29 |
|
30 |
class axis: |
30 |
class axis: |
31 |
def __init__(self, SPI_CS, Direction, StepsPerUnit, MaxSpeed): |
31 |
def __init__(self, SPI_CS, Direction, StepsPerUnit, MaxSpeed): |
32 |
' One axis of robot ' |
32 |
' One axis of robot ' |
33 |
self.CS = SPI_CS |
33 |
self.CS = SPI_CS |
34 |
self.Dir = Direction |
34 |
self.Dir = Direction |
35 |
self.SPU = StepsPerUnit |
35 |
self.SPU = StepsPerUnit |
36 |
self.maxspeed = MaxSpeed |
36 |
self.maxspeed = MaxSpeed |
37 |
|
37 |
|
38 |
self.L6470_ABS_POS =0x01 |
38 |
self.L6470_ABS_POS =0x01 |
39 |
self.L6470_EL_POS =0x02 |
39 |
self.L6470_EL_POS =0x02 |
40 |
self.L6470_MARK =0x03 |
40 |
self.L6470_MARK =0x03 |
41 |
self.L6470_SPEED =0x04 |
41 |
self.L6470_SPEED =0x04 |
42 |
self.L6470_ACC =0x05 |
42 |
self.L6470_ACC =0x05 |
43 |
self.L6470_DEC =0x06 |
43 |
self.L6470_DEC =0x06 |
44 |
self.L6470_MAX_SPEED =0x07 |
44 |
self.L6470_MAX_SPEED =0x07 |
45 |
self.L6470_MIN_SPEED =0x08 |
45 |
self.L6470_MIN_SPEED =0x08 |
46 |
self.L6470_FS_SPD =0x15 |
46 |
self.L6470_FS_SPD =0x15 |
47 |
self.L6470_KVAL_HOLD =0x09 |
47 |
self.L6470_KVAL_HOLD =0x09 |
48 |
self.L6470_KVAL_RUN =0x0A |
48 |
self.L6470_KVAL_RUN =0x0A |
49 |
self.L6470_KVAL_ACC =0x0B |
49 |
self.L6470_KVAL_ACC =0x0B |
50 |
self.L6470_KVAL_DEC =0x0C |
50 |
self.L6470_KVAL_DEC =0x0C |
51 |
self.L6470_INT_SPEED =0x0D |
51 |
self.L6470_INT_SPEED =0x0D |
52 |
self.L6470_ST_SLP =0x0E |
52 |
self.L6470_ST_SLP =0x0E |
53 |
self.L6470_FN_SLP_ACC =0x0F |
53 |
self.L6470_FN_SLP_ACC =0x0F |
54 |
self.L6470_FN_SLP_DEC =0x10 |
54 |
self.L6470_FN_SLP_DEC =0x10 |
55 |
self.L6470_K_THERM =0x11 |
55 |
self.L6470_K_THERM =0x11 |
56 |
self.L6470_ADC_OUT =0x12 |
56 |
self.L6470_ADC_OUT =0x12 |
57 |
self.L6470_OCD_TH =0x13 |
57 |
self.L6470_OCD_TH =0x13 |
58 |
self.L6470_STALL_TH =0x14 |
58 |
self.L6470_STALL_TH =0x14 |
59 |
self.L6470_STEP_MODE =0x16 |
59 |
self.L6470_STEP_MODE =0x16 |
60 |
self.L6470_ALARM_EN =0x17 |
60 |
self.L6470_ALARM_EN =0x17 |
61 |
self.L6470_CONFIG =0x18 |
61 |
self.L6470_CONFIG =0x18 |
62 |
self.L6470_STATUS =0x19 |
62 |
self.L6470_STATUS =0x19 |
63 |
|
63 |
|
64 |
self.Reset() |
64 |
self.Reset() |
65 |
self.Initialize() |
65 |
self.Initialize() |
66 |
self.MaxSpeed(self.maxspeed) |
66 |
self.MaxSpeed(self.maxspeed) |
67 |
|
67 |
|
68 |
def Reset(self): |
68 |
def Reset(self): |
69 |
'Reset the Axis' |
69 |
'Reset the Axis' |
70 |
spi.SPI_write_byte(self.CS, 0xC0) # reset |
70 |
spi.SPI_write_byte(self.CS, 0xC0) # reset |
71 |
|
71 |
|
72 |
def Initialize(self): |
72 |
def Initialize(self): |
73 |
'set default parameters for H-bridge ' |
73 |
'set default parameters for H-bridge ' |
74 |
# spi.SPI_write_byte(self.CS, 0x14) # Stall Treshold setup |
74 |
# spi.SPI_write_byte(self.CS, 0x14) # Stall Treshold setup |
75 |
# spi.SPI_write_byte(self.CS, 0xFF) |
75 |
# spi.SPI_write_byte(self.CS, 0xFF) |
76 |
# spi.SPI_write_byte(self.CS, 0x13) # Over Current Treshold setup |
76 |
# spi.SPI_write_byte(self.CS, 0x13) # Over Current Treshold setup |
77 |
# spi.SPI_write_byte(self.CS, 0xFF) |
77 |
# spi.SPI_write_byte(self.CS, 0xFF) |
78 |
spi.SPI_write_byte(self.CS, 0x15) # Full Step speed |
78 |
spi.SPI_write_byte(self.CS, 0x15) # Full Step speed |
79 |
spi.SPI_write_byte(self.CS, 0xFF) |
79 |
spi.SPI_write_byte(self.CS, 0xFF) |
80 |
spi.SPI_write_byte(self.CS, 0xFF) |
80 |
spi.SPI_write_byte(self.CS, 0xFF) |
81 |
spi.SPI_write_byte(self.CS, 0x05) # ACC |
81 |
spi.SPI_write_byte(self.CS, 0x05) # ACC |
82 |
spi.SPI_write_byte(self.CS, 0x00) |
82 |
spi.SPI_write_byte(self.CS, 0x00) |
83 |
spi.SPI_write_byte(self.CS, 0x10) |
83 |
spi.SPI_write_byte(self.CS, 0x10) |
84 |
spi.SPI_write_byte(self.CS, 0x06) # DEC |
84 |
spi.SPI_write_byte(self.CS, 0x06) # DEC |
85 |
spi.SPI_write_byte(self.CS, 0x00) |
85 |
spi.SPI_write_byte(self.CS, 0x00) |
86 |
spi.SPI_write_byte(self.CS, 0x10) |
86 |
spi.SPI_write_byte(self.CS, 0x10) |
87 |
spi.SPI_write_byte(self.CS, self.L6470_KVAL_RUN) # KVAL_RUN |
87 |
spi.SPI_write_byte(self.CS, self.L6470_KVAL_RUN) # KVAL_RUN |
88 |
spi.SPI_write_byte(self.CS, 0x58) |
88 |
spi.SPI_write_byte(self.CS, 0x58) |
89 |
spi.SPI_write_byte(self.CS, self.L6470_KVAL_ACC) # KVAL_ACC |
89 |
spi.SPI_write_byte(self.CS, self.L6470_KVAL_ACC) # KVAL_ACC |
90 |
spi.SPI_write_byte(self.CS, 0x58) |
90 |
spi.SPI_write_byte(self.CS, 0x58) |
91 |
spi.SPI_write_byte(self.CS, self.L6470_KVAL_DEC) # KVAL_DEC |
91 |
spi.SPI_write_byte(self.CS, self.L6470_KVAL_DEC) # KVAL_DEC |
92 |
spi.SPI_write_byte(self.CS, 0x58) |
92 |
spi.SPI_write_byte(self.CS, 0x58) |
93 |
spi.SPI_write_byte(self.CS, 0x18) # CONFIG |
93 |
spi.SPI_write_byte(self.CS, 0x18) # CONFIG |
94 |
spi.SPI_write_byte(self.CS, 0b00101110) # spolecny byte pro obe konfigurace |
94 |
spi.SPI_write_byte(self.CS, 0b00101110) # spolecny byte pro obe konfigurace |
95 |
spi.SPI_write_byte(self.CS, 0b10000000) # konfigurace pro interni oscilator |
95 |
spi.SPI_write_byte(self.CS, 0b10000000) # konfigurace pro interni oscilator |
96 |
# spi.SPI_write_byte(self.CS, 0b10000110) # konfigurace s externim oscilatorem |
96 |
# spi.SPI_write_byte(self.CS, 0b10000110) # konfigurace s externim oscilatorem |
97 |
self.MaxSpeed(self.maxspeed) |
97 |
self.MaxSpeed(self.maxspeed) |
98 |
|
98 |
|
99 |
def setKVAL(self, hold = 0.5, run = 0.5, acc = 0.5, dec = 0.5): |
99 |
def setKVAL(self, hold = 0.5, run = 0.5, acc = 0.5, dec = 0.5): |
100 |
""" The available range is from 0 to 0.996 x VS with a resolution of 0.004 x VS """ |
100 |
""" The available range is from 0 to 0.996 x VS with a resolution of 0.004 x VS """ |
101 |
|
101 |
|
102 |
def setOverCurrentTH(self, hold = 0.5, run = 0.5, acc = 0.5, dec = 0.5): |
102 |
def setOverCurrentTH(self, hold = 0.5, run = 0.5, acc = 0.5, dec = 0.5): |
103 |
""" The available range is from 375 mA to 6 A, in steps of 375 mA """ |
103 |
""" The available range is from 375 mA to 6 A, in steps of 375 mA """ |
104 |
|
104 |
|
105 |
def MaxSpeed(self, speed): |
105 |
def MaxSpeed(self, speed): |
106 |
'Setup of maximum speed in steps/s. The available range is from 15.25 to 15610 step/s with a resolution of 15.25 step/s.' |
106 |
'Setup of maximum speed in steps/s. The available range is from 15.25 to 15610 step/s with a resolution of 15.25 step/s.' |
107 |
speed_value = int(speed / 15.25) |
107 |
speed_value = int(speed / 15.25) |
108 |
if (speed_value <= 0): |
108 |
if (speed_value <= 0): |
109 |
speed_value = 1 |
109 |
speed_value = 1 |
110 |
elif (speed_value >= 1023): |
110 |
elif (speed_value >= 1023): |
111 |
speed_value = 1023 |
111 |
speed_value = 1023 |
112 |
|
112 |
|
113 |
data = [(speed_value >> i & 0xff) for i in (8,0)] |
113 |
data = [(speed_value >> i & 0xff) for i in (8,0)] |
114 |
spi.SPI_write_byte(self.CS, self.L6470_MAX_SPEED) # Max Speed setup |
114 |
spi.SPI_write_byte(self.CS, self.L6470_MAX_SPEED) # Max Speed setup |
115 |
spi.SPI_write_byte(self.CS, data[0]) |
115 |
spi.SPI_write_byte(self.CS, data[0]) |
116 |
spi.SPI_write_byte(self.CS, data[1]) |
116 |
spi.SPI_write_byte(self.CS, data[1]) |
117 |
return (speed_value * 15.25) |
117 |
return (speed_value * 15.25) |
118 |
|
118 |
|
119 |
def ReleaseSW(self): |
119 |
def ReleaseSW(self): |
120 |
' Go away from Limit Switch ' |
120 |
' Go away from Limit Switch ' |
121 |
while self.ReadStatusBit(2) == 1: # is Limit Switch ON ? |
121 |
while self.ReadStatusBit(2) == 1: # is Limit Switch ON ? |
122 |
spi.SPI_write_byte(self.CS, 0x92 | (~self.Dir & 1)) # release SW |
122 |
spi.SPI_write_byte(self.CS, 0x92 | (~self.Dir & 1)) # release SW |
123 |
while self.IsBusy(): |
123 |
while self.IsBusy(): |
124 |
pass |
124 |
pass |
125 |
self.MoveWait(10) # move 10 units away |
125 |
self.MoveWait(10) # move 10 units away |
126 |
|
126 |
|
127 |
def GoZero(self, speed): |
127 |
def GoZero(self, speed): |
128 |
' Go to Zero position ' |
128 |
' Go to Zero position ' |
129 |
self.ReleaseSW() |
129 |
self.ReleaseSW() |
130 |
|
130 |
|
131 |
spi.SPI_write_byte(self.CS, 0x82 | (self.Dir & 1)) # Go to Zero |
131 |
spi.SPI_write_byte(self.CS, 0x82 | (self.Dir & 1)) # Go to Zero |
132 |
spi.SPI_write_byte(self.CS, 0x00) |
132 |
spi.SPI_write_byte(self.CS, 0x00) |
133 |
spi.SPI_write_byte(self.CS, speed) |
133 |
spi.SPI_write_byte(self.CS, speed) |
134 |
while self.IsBusy(): |
134 |
while self.IsBusy(): |
135 |
pass |
135 |
pass |
136 |
time.sleep(0.3) |
136 |
time.sleep(0.3) |
137 |
self.ReleaseSW() |
137 |
self.ReleaseSW() |
138 |
|
138 |
|
139 |
def GetStatus(self): |
139 |
def GetStatus(self): |
140 |
#self.spi.xfer([0b11010000]) # Get status command from datasheet - does not work for uknown rasons |
140 |
#self.spi.xfer([0b11010000]) # Get status command from datasheet - does not work for uknown rasons |
141 |
spi.SPI_write_byte(self.CS, 0x39) # Gotparam command on status register |
141 |
spi.SPI_write_byte(self.CS, 0x39) # Gotparam command on status register |
142 |
spi.SPI_write_byte(self.CS, 0x00) |
142 |
spi.SPI_write_byte(self.CS, 0x00) |
143 |
data = [spi.SPI_read_byte()] |
143 |
data = [spi.SPI_read_byte()] |
144 |
spi.SPI_write_byte(self.CS, 0x00) |
144 |
spi.SPI_write_byte(self.CS, 0x00) |
145 |
data = data + [spi.SPI_read_byte()] |
145 |
data = data + [spi.SPI_read_byte()] |
146 |
|
146 |
|
147 |
status = dict([('SCK_MOD',data[0] & 0x80 == 0x80), #The SCK_MOD bit is an active high flag indicating that the device is working in Step-clock mode. In this case the step-clock signal should be provided through the STCK input pin. The DIR bit indicates the current motor direction |
147 |
status = dict([('SCK_MOD',data[0] & 0x80 == 0x80), #The SCK_MOD bit is an active high flag indicating that the device is working in Step-clock mode. In this case the step-clock signal should be provided through the STCK input pin. The DIR bit indicates the current motor direction |
148 |
('STEP_LOSS_B',data[0] & 0x40 == 0x40), |
148 |
('STEP_LOSS_B',data[0] & 0x40 == 0x40), |
149 |
('STEP_LOSS_A',data[0] & 0x20 == 0x20), |
149 |
('STEP_LOSS_A',data[0] & 0x20 == 0x20), |
150 |
('OCD',data[0] & 0x10 == 0x10), |
150 |
('OCD',data[0] & 0x10 == 0x10), |
151 |
('TH_SD',data[0] & 0x08 == 0x08), |
151 |
('TH_SD',data[0] & 0x08 == 0x08), |
152 |
('TH_WRN',data[0] & 0x04 == 0x04), |
152 |
('TH_WRN',data[0] & 0x04 == 0x04), |
153 |
('UVLO',data[0] & 0x02 == 0x02), |
153 |
('UVLO',data[0] & 0x02 == 0x02), |
154 |
('WRONG_CMD',data[0] & 0x01 == 0x01), #The NOTPERF_CMD and WRONG_CMD flags are active high and indicate, respectively, that the command received by SPI cannot be performed or does not exist at all. |
154 |
('WRONG_CMD',data[0] & 0x01 == 0x01), #The NOTPERF_CMD and WRONG_CMD flags are active high and indicate, respectively, that the command received by SPI cannot be performed or does not exist at all. |
155 |
('NOTPERF_CMD',data[1] & 0x80 == 0x80), |
155 |
('NOTPERF_CMD',data[1] & 0x80 == 0x80), |
156 |
('MOT_STATUS',data[1] & 0x60), |
156 |
('MOT_STATUS',data[1] & 0x60), |
157 |
('DIR',data[1] & 0x10 == 0x10), |
157 |
('DIR',data[1] & 0x10 == 0x10), |
158 |
('SW_EVN',data[1] & 0x08 == 0x08), |
158 |
('SW_EVN',data[1] & 0x08 == 0x08), |
159 |
('SW_F',data[1] & 0x04 == 0x04), #The SW_F flag reports the SW input status (low for open and high for closed). |
159 |
('SW_F',data[1] & 0x04 == 0x04), #The SW_F flag reports the SW input status (low for open and high for closed). |
160 |
('BUSY',data[1] & 0x02 != 0x02), |
160 |
('BUSY',data[1] & 0x02 != 0x02), |
161 |
('HIZ',data[1] & 0x01 == 0x01)]) |
161 |
('HIZ',data[1] & 0x01 == 0x01)]) |
162 |
return status |
162 |
return status |
163 |
|
163 |
|
164 |
def GetACC(self): |
164 |
def GetACC(self): |
165 |
# self.spi.xfer([0x29]) # Gotparam command on status register |
165 |
# self.spi.xfer([0x29]) # Gotparam command on status register |
166 |
spi.SPI_write_byte(self.CS, self.L6470_ACC + 0x20) # TODO check register read address seting |
166 |
spi.SPI_write_byte(self.CS, self.L6470_ACC + 0x20) # TODO check register read address seting |
167 |
spi.SPI_write_byte(self.CS, 0x00) |
167 |
spi.SPI_write_byte(self.CS, 0x00) |
168 |
data = spi.SPI_read_byte() |
168 |
data = spi.SPI_read_byte() |
169 |
spi.SPI_write_byte(self.CS, 0x00) |
169 |
spi.SPI_write_byte(self.CS, 0x00) |
170 |
data = data + [spi.SPI_read_byte()] |
170 |
data = data + [spi.SPI_read_byte()] |
171 |
print data # return speed in real units |
171 |
print data # return speed in real units |
172 |
|
172 |
|
173 |
|
173 |
|
174 |
def Move(self, units): |
174 |
def Move(self, units): |
175 |
' Move some distance units from current position ' |
175 |
' Move some distance units from current position ' |
176 |
steps = units * self.SPU # translate units to steps |
176 |
steps = units * self.SPU # translate units to steps |
177 |
if steps > 0: # look for direction |
177 |
if steps > 0: # look for direction |
178 |
spi.SPI_write_byte(self.CS, 0x40 | (~self.Dir & 1)) |
178 |
spi.SPI_write_byte(self.CS, 0x40 | (~self.Dir & 1)) |
179 |
else: |
179 |
else: |
180 |
spi.SPI_write_byte(self.CS, 0x40 | (self.Dir & 1)) |
180 |
spi.SPI_write_byte(self.CS, 0x40 | (self.Dir & 1)) |
181 |
steps = int(abs(steps)) |
181 |
steps = int(abs(steps)) |
182 |
spi.SPI_write_byte(self.CS, (steps >> 16) & 0xFF) |
182 |
spi.SPI_write_byte(self.CS, (steps >> 16) & 0xFF) |
183 |
spi.SPI_write_byte(self.CS, (steps >> 8) & 0xFF) |
183 |
spi.SPI_write_byte(self.CS, (steps >> 8) & 0xFF) |
184 |
spi.SPI_write_byte(self.CS, steps & 0xFF) |
184 |
spi.SPI_write_byte(self.CS, steps & 0xFF) |
185 |
|
185 |
|
186 |
def Run(self, direction, speed): |
186 |
def Run(self, direction, speed): |
187 |
speed_value = int(speed / 0.015) |
187 |
speed_value = int(speed / 0.015) |
188 |
|
188 |
|
189 |
command = 0b01010000 + int(direction) |
189 |
command = 0b01010000 + int(direction) |
190 |
data = [(speed_value >> i & 0xff) for i in (16,8,0)] |
190 |
data = [(speed_value >> i & 0xff) for i in (16,8,0)] |
191 |
spi.SPI_write_byte(self.CS, command) # Max Speed setup |
191 |
spi.SPI_write_byte(self.CS, command) # Max Speed setup |
192 |
spi.SPI_write_byte(self.CS, data[0]) |
192 |
spi.SPI_write_byte(self.CS, data[0]) |
193 |
spi.SPI_write_byte(self.CS, data[1]) |
193 |
spi.SPI_write_byte(self.CS, data[1]) |
194 |
spi.SPI_write_byte(self.CS, data[2]) |
194 |
spi.SPI_write_byte(self.CS, data[2]) |
195 |
return (speed_value * 0.015) |
195 |
return (speed_value * 0.015) |
196 |
|
196 |
|
197 |
def MoveWait(self, units): |
197 |
def MoveWait(self, units): |
198 |
' Move some distance units from current position and wait for execution ' |
198 |
' Move some distance units from current position and wait for execution ' |
199 |
self.Move(units) |
199 |
self.Move(units) |
200 |
while self.GetStatus()['BUSY']: |
200 |
while self.GetStatus()['BUSY']: |
201 |
time.sleep(0.1) |
201 |
time.sleep(0.1) |
202 |
|
202 |
|
203 |
def Float(self, hard = False): |
203 |
def Float(self, hard = False): |
204 |
' switch H-bridge to High impedance state ' |
204 |
' switch H-bridge to High impedance state ' |
205 |
if (hard == False): |
205 |
if (hard == False): |
206 |
spi.SPI_write_byte(self.CS, 0xA0) |
206 |
spi.SPI_write_byte(self.CS, 0xA0) |
207 |
else: |
207 |
else: |
208 |
spi.SPI_write_byte(self.CS, 0xA8) |
208 |
spi.SPI_write_byte(self.CS, 0xA8) |
209 |
|
209 |
|
210 |
|
210 |
|
211 |
# End Class axis -------------------------------------------------- |
211 |
# End Class axis -------------------------------------------------- |
212 |
|
212 |
|
213 |
|
213 |
|
214 |
print "Clock motor control script started. \r\n" |
214 |
print "Clock motor control script started. \r\n" |
215 |
print "Requested speed is: %f steps/s" % SPEED |
215 |
print "Requested speed is: %f steps/s" % SPEED |
216 |
|
216 |
|
217 |
pylirc.init("pylirc", "/home/odroid/conf") |
217 |
pylirc.init("pylirc", "/home/odroid/conf") |
218 |
|
218 |
|
219 |
|
219 |
|
220 |
|
220 |
|
221 |
cfg = config.Config( |
221 |
cfg = config.Config( |
222 |
i2c = { |
222 |
i2c = { |
223 |
"port": 1, |
223 |
"port": 1, |
224 |
}, |
224 |
}, |
225 |
|
225 |
|
226 |
bus = [ |
226 |
bus = [ |
227 |
{ |
227 |
{ |
228 |
"name":"spi", |
228 |
"name":"spi", |
229 |
"type":"i2cspi", |
229 |
"type":"i2cspi", |
230 |
"address": 0x2e, |
230 |
"address": 0x2e, |
231 |
}, |
231 |
}, |
232 |
], |
232 |
], |
233 |
) |
233 |
) |
234 |
|
234 |
|
235 |
|
235 |
|
236 |
cfg.initialize() |
236 |
cfg.initialize() |
237 |
|
237 |
|
238 |
spi = cfg.get_device("spi") |
238 |
spi = cfg.get_device("spi") |
239 |
|
239 |
|
240 |
spi.route() |
240 |
spi.route() |
241 |
|
241 |
|
242 |
|
242 |
|
243 |
try: |
243 |
try: |
244 |
print "Configuring SPI.." |
244 |
print "Configuring SPI.." |
245 |
spi.SPI_config(spi.I2CSPI_MSB_FIRST| spi.I2CSPI_MODE_CLK_IDLE_HIGH_DATA_EDGE_TRAILING| spi.I2CSPI_CLK_461kHz) |
245 |
spi.SPI_config(spi.I2CSPI_MSB_FIRST| spi.I2CSPI_MODE_CLK_IDLE_HIGH_DATA_EDGE_TRAILING| spi.I2CSPI_CLK_461kHz) |
246 |
time.sleep(0.1) |
246 |
time.sleep(0.1) |
247 |
|
247 |
|
- |
|
248 |
maximum_speed = 2 * SPEED |
- |
|
249 |
|
248 |
print "Configuring stepper motor.." |
250 |
print "Configuring stepper motor.." |
249 |
X = axis(spi.I2CSPI_SS0, 0, 1, MaxSpeed = 2*SPEED) # set Number of Steps per axis Unit and set Direction of Rotation |
251 |
X = axis(spi.I2CSPI_SS0, 0, 1, MaxSpeed = maximum_speed) # set Number of Steps per axis Unit and set Direction of Rotation |
250 |
|
252 |
|
251 |
|
253 |
|
252 |
print "Motor speed limit is: %f steps/s" % maximum_speed |
254 |
print "Motor speed limit is: %f steps/s" % maximum_speed |
253 |
running = False |
255 |
running = False |
254 |
|
256 |
|
255 |
print "Waiting for IR command.." |
257 |
print "Waiting for IR command.." |
256 |
while True: # set maximal motor speed |
258 |
while True: # set maximal motor speed |
257 |
key = pylirc.nextcode() ## preccessing the IR remote control commands. |
259 |
key = pylirc.nextcode() ## preccessing the IR remote control commands. |
258 |
|
260 |
|
259 |
if key == ['start']: |
261 |
if key == ['start']: |
260 |
running = True |
262 |
running = True |
261 |
direction = True |
263 |
direction = True |
262 |
requested_speed = SPEED |
264 |
requested_speed = SPEED |
263 |
|
265 |
|
264 |
if key == ['faster']: |
266 |
if key == ['faster']: |
265 |
running = True |
267 |
running = True |
266 |
direction = True |
268 |
direction = True |
267 |
requested_speed = SPEED * 1.2 # runnig the motor at 120% of the base motor speed |
269 |
requested_speed = SPEED * 1.2 # runnig the motor at 120% of the base motor speed |
268 |
|
270 |
|
269 |
if key == ['slower']: |
271 |
if key == ['slower']: |
270 |
running = True |
272 |
running = True |
271 |
direction = False |
273 |
direction = False |
272 |
requested_speed = SPEED * 0.8 |
274 |
requested_speed = SPEED * 0.8 |
273 |
|
275 |
|
274 |
if key == ['stop']: |
276 |
if key == ['stop']: |
275 |
running = False |
277 |
running = False |
276 |
|
278 |
|
277 |
time.sleep(0.1) |
279 |
time.sleep(0.1) |
278 |
|
280 |
|
279 |
if running == True: |
281 |
if running == True: |
280 |
real_speed = X.Run(direction, requested_speed) |
282 |
real_speed = X.Run(direction, requested_speed) |
281 |
print "Motor running at: %f steps/s" % real_speed |
283 |
print "Motor running at: %f steps/s" % real_speed |
282 |
else: |
284 |
else: |
283 |
X.Reset() |
285 |
X.Reset() |
284 |
X.Initialize() |
286 |
X.Initialize() |
285 |
X.Float(hard=False) # release power |
287 |
X.Float(hard=False) # release power |
286 |
print "Stopping the motor." |
288 |
print "Stopping the motor." |
287 |
|
289 |
|
288 |
except KeyboardInterrupt: |
290 |
except KeyboardInterrupt: |
289 |
print "stop" |
291 |
print "stop" |
290 |
X.Float(hard=False) # release power |
292 |
X.Float(hard=False) # release power |
291 |
sys.exit(0) |
293 |
sys.exit(0) |
292 |
|
294 |
|
293 |
except Exception, e: |
295 |
except Exception, e: |
294 |
X.Float(hard=False) # release power |
296 |
X.Float(hard=False) # release power |
295 |
print >> sys.stderr, "Exception: %s" % str(e) |
297 |
print >> sys.stderr, "Exception: %s" % str(e) |
296 |
sys.exit(1) |
298 |
sys.exit(1) |