forked from FizzyMcPhysics/NXT
/
Robot.py
129 lines (98 loc) · 3.71 KB
/
Robot.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
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 10 18:04:52 2013
@author: benjamin
"""
# Works great! =D
from time import sleep
from nxt.brick import Brick
from nxt.locator import find_one_brick
#from nxt.motor import Motor, TachoInfo, PORT_A, PORT_B, PORT_C
from mymotor import Motor, PORT_A, PORT_B, PORT_C
from nxt.sensor import Light, Sound, Touch, Ultrasonic
from nxt.sensor import PORT_1, PORT_2, PORT_3, PORT_4
FORTH = 100
BACK = -100
class Robot(object):
def __init__(self, brick='NXT'):
r'''Creates a new Robot controller.
brick
Either an nxt.brick.Brick object, or an NXT brick's name as a
string. If omitted, a Brick named 'NXT' is looked up.
'''
if isinstance(brick, basestring):
brick = find_one_brick(name=brick)
self.brick = brick
self.tool = Motor(brick, PORT_B)
self.tracks = [Motor(brick, PORT_A), Motor(brick, PORT_C)]
self.ultrasonic = Ultrasonic(brick, PORT_1)
self.sound = Sound(brick, PORT_2)
self.light = Light(brick, PORT_3)
self.touch = Touch(brick, PORT_4)
def turn(self, power, angle):
for motor in self.tracks:
motor.turn(power, angle)
def move(self, power=FORTH):
r'''Simultaneously activates the tracks motors, causing Robot to move.
power
The strength effected by the motors. Positive values will cause
Robot to move forward, while negative values will cause it
to move backwards. If you are unsure about how much force to
apply, the special values FORTH and BACK provide reasonable
defaults. If omitted, FORTH is used.
'''
for motor in self.tracks:
motor.run(power=power)
def wait(self, seconds):
''' secsonds
How long the motors will rotate.
Will this take values < 0? Most motor commands work in ms.
Try passing sleep 1/seconds (miliseconds)
'''
sleep(seconds)
def stop(self):
for motor in self.tracks:
motor.idle()
def tacho(self):
'''
returns an array of two elements which are the motor tacho readings
'''
tachos = []
for motor in self.tracks:
#tachos.append(motor.get_tacho())
tachos.append(motor.tacho_count) #, rotation_count
return tachos
def act(self, power=FORTH):
r'''Make Robot move its tool.
power
The strength effected by the motor. If omitted, (100) is used.
'''
self.tool.run(power=power)
def echolocate(self):
r'''Reads the Ultrasonic sensor's output.
'''
return self.ultrasonic.get_sample()
def feel(self):
r'''Reads the Touch sensor's output.
'''
return self.touch.get_sample()
def hear(self):
r'''Reads the Sound sensor's output.
'''
return self.sound.get_sample()
def say(self, line, times=1):
r'''Plays a sound file named (line + '.rso'), which is expected to be
stored in the brick. The file is played (times) times.
line
The name of a sound file stored in the brick.
times
How many times the sound file will be played before this method
returns.
'''
for i in range(0, times):
self.brick.play_sound_file(False, line + '.rso')
sleep(1)
def see(self):
r'''Reads the Light sensor's output.
'''
return self.light.get_sample()