|
| 1 | +#Script to control a NXT 2-axis CNC "Pancake maker" |
| 2 | +#Illustrates controlling more than one motor at the same time without trying to |
| 3 | +#sync them. Uses the thread module. |
| 4 | +#Written 2/3/11 by Marcus Wanner |
| 5 | +# |
| 6 | +#For more info and warnings see: |
| 7 | +#http://groups.google.com/group/nxt-python/browse_thread/thread/f6ef0865ae768ef |
| 8 | + |
| 9 | +import nxt, thread, time |
| 10 | +b = nxt.find_one_brick(name = 'MyNXT') |
| 11 | +mx = nxt.Motor(b, nxt.PORT_A) |
| 12 | +my = nxt.Motor(b, nxt.PORT_B) |
| 13 | +motors = [mx, my] |
| 14 | + |
| 15 | +def turnmotor(m, power, degrees): |
| 16 | + m.turn(power, degrees) |
| 17 | + |
| 18 | +#here are the instructions... |
| 19 | +#the first value is the time to start the instruction |
| 20 | +#the second is the axis (0 for x, 1 for y) |
| 21 | +#the third is the power |
| 22 | +#the fourth is the degrees |
| 23 | +#it's probably not a good idea to run simultaneous turn |
| 24 | +#functions on a single motor, so be careful with this |
| 25 | +instructions = ( |
| 26 | + [0, 0, 80, 360], |
| 27 | + [0, 1, -40, 1080], |
| 28 | + [1, 0, -80, 360], |
| 29 | + [2, 0, 80, 360], |
| 30 | + [3, 1, 100, 360], |
| 31 | + [3, 0, -100, 360], |
| 32 | +) |
| 33 | +#how long from start until the last instruction is ended |
| 34 | +length = 5 |
| 35 | + |
| 36 | +def runinstruction(i): |
| 37 | + motorid, speed, degrees = i |
| 38 | + #THIS IS THE IMPORTANT PART! |
| 39 | + thread.start_new_thread( |
| 40 | + turnmotor, |
| 41 | + (motors[motorid], speed, degrees)) |
| 42 | + |
| 43 | +#main loop |
| 44 | +seconds = 0 |
| 45 | +while 1: |
| 46 | + print "Tick %d" % seconds |
| 47 | + for i in instructions: |
| 48 | + if i[0] == seconds: |
| 49 | + runinstruction(i[1:]) |
| 50 | + seconds = seconds + 1 |
| 51 | + if seconds >= length: |
| 52 | + break |
| 53 | + time.sleep(1) |
0 commit comments