Skip to content

Commit b83ea28

Browse files
committed
Initial pass at configurable motor test script
- Input script is JSON file of timed attribute/value pairs - Script name is hard wired - Logs speed, duty_cycle, position
1 parent e627510 commit b83ea28

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

tests/motor/run-direct.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{ "meta": { "name": "run-direct-test",
2+
"interval": 10 ,
3+
"max_time": 4000 },
4+
"data": [ { "port": "outA",
5+
"commands": [ { "time": 0,
6+
"attributes": [ { "duty_cycle_sp": 0 },
7+
{ "command": "run-direct" } ] },
8+
{ "time": 400,
9+
"attributes": [ { "duty_cycle_sp": 20 } ] },
10+
{ "time": 800,
11+
"attributes": [ { "duty_cycle_sp": 40 } ] },
12+
{ "time": 1200,
13+
"attributes": [ { "duty_cycle_sp": 60 } ] },
14+
{ "time": 1600,
15+
"attributes": [ { "duty_cycle_sp": 80 } ] },
16+
{ "time": 2000,
17+
"attributes": [ { "duty_cycle_sp": 100 } ] },
18+
{ "time": 3000,
19+
"attributes": [ { "duty_cycle_sp": 0 } ] },
20+
{ "time": 3500,
21+
"attributes": [ { "command": "stop" } ] } ] } ]
22+
}

tests/motor/test.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#! /usr/bin/python2.7
2+
3+
import json
4+
import time
5+
import ev3dev.ev3 as ev3
6+
7+
def next_commands():
8+
for d in test['data']:
9+
for c in d['commands']:
10+
yield c
11+
12+
test = json.loads( open( './run-direct.json' ).read() )
13+
14+
print test['data'][0]['port']
15+
m = ev3.Motor( test['data'][0]['port'] )
16+
17+
name = test['meta']['name']
18+
interval = test['meta']['interval']
19+
max_time = test['meta']['max_time']
20+
21+
start = time.clock()
22+
end = start + max_time/1000
23+
24+
intervals = [x/1000.0 for x in range( 0, max_time, interval )]
25+
26+
now = time.clock()
27+
28+
commands = next_commands()
29+
30+
c = commands.next()
31+
32+
next_interval = c['time']/1000.0
33+
results = []
34+
35+
for i in intervals:
36+
i = i + start
37+
while now < i:
38+
now = time.clock()
39+
if now >= start + next_interval:
40+
print i, next_interval, now
41+
for a in c['attributes']:
42+
for k in a.keys():
43+
print " ", k, " = ", a[k]
44+
setattr(m,k,a[k])
45+
try:
46+
c = commands.next()
47+
next_interval = c['time']/1000.0
48+
except StopIteration:
49+
next_interval = max_time/1000.0
50+
results.append( (now-start, (m.speed, m.position, m.duty_cycle)) )
51+
52+
53+
print 'data = ['
54+
55+
first = True
56+
for r in results:
57+
if first:
58+
print( ' ({0}, ({1}, {2}, {3}))'.format(r[0], r[1][0], r[1][1], r[1][2]))
59+
first = False
60+
else:
61+
print( ' , ({0}, ({1}, {2}, {3}))'.format(r[0], r[1][0], r[1][1], r[1][2]))
62+
63+
print ']'
64+
65+

0 commit comments

Comments
 (0)