Module Gear
[hide private]
[frames] | no frames]

Source Code for Module Gear

  1  # Gear.py 
  2  # Remote mode 
  3   
  4  from RobotInstance import RobotInstance 
  5  import SharedConstants 
  6   
  7  # ------------------------   Class GearState  ---------------------------------------------- 
8 -class GearState():
9 FORWARD = 0 10 BACKWARD = 1 11 STOPPED = 2 12 LEFT = 3 13 RIGHT = 4 14 LEFTARC = 5 15 RIGHTARC = 6 16 UNDEFINED = 7
17 18 # ------------------------ Class Gear --------------------------------------------------
19 -class Gear():
20 ''' 21 Class that represents the combination of two motors on an axis 22 to perform a car-like movement. 23 ''' 24
25 - def __init__(self):
26 self.state = GearState.UNDEFINED 27 self.speed = SharedConstants.GEAR_DEFAULT_SPEED 28 self.arcRadius = 0 29 self.device = "gear" 30 robot = RobotInstance.getRobot() 31 if robot == None: # deferred registering, because Robot not yet created 32 RobotInstance._partsToRegister.append(self) 33 else: 34 self._setup(robot)
35
36 - def _setup(self, robot):
37 robot.sendCommand(self.device + ".create") 38 robot.sendCommand(self.device + ".setSpeed." + str(self.speed)) 39 self.robot = robot
40
41 - def setSpeed(self, speed):
42 self._checkRobot() 43 speed = int(speed) 44 if self.speed == speed: 45 return 46 self.speed = speed 47 self.robot.sendCommand(self.device + ".setSpeed." + str(speed)) 48 self.state = GearState.UNDEFINED
49
50 - def forward(self, duration = 0):
51 self._checkRobot() 52 if self.state == GearState.FORWARD: 53 return 54 if duration == 0: 55 self.robot.sendCommand(self.device + ".forward") 56 self.state = GearState.FORWARD 57 else: 58 self.robot.sendCommand(self.device + ".forward." + str(duration)) 59 self.state = GearState.STOPPED
60
61 - def backward(self, duration = 0):
62 self._checkRobot() 63 if self.state == GearState.BACKWARD: 64 return 65 if duration == 0: 66 self.robot.sendCommand(self.device + ".backward") 67 self.state = GearState.BACKWARD 68 else: 69 self.robot.sendCommand(self.device + ".backward." + str(duration)) 70 self.state = GearState.STOPPED
71
72 - def left(self, duration = 0):
73 self._checkRobot() 74 if self.state == GearState.LEFT: 75 return 76 if duration == 0: 77 self.robot.sendCommand(self.device + ".left") 78 self.state = GearState.LEFT 79 else: 80 self.robot.sendCommand(self.device + ".left." + str(duration)) 81 self.state = GearState.STOPPED
82
83 - def right(self, duration = 0):
84 self._checkRobot() 85 if self.state == GearState.RIGHT: 86 return 87 if duration == 0: 88 self.robot.sendCommand(self.device + ".right") 89 self.state = GearState.RIGHT 90 else: 91 self.robot.sendCommand(self.device + ".right." + str(duration)) 92 self.state = GearState.STOPPED
93
94 - def leftArc(self, radius, duration = 0):
95 self._checkRobot() 96 radius = int(1000 * radius) 97 if duration == 0: 98 if self.state == GearState.LEFTARC and radius == self.arcRadius: 99 return 100 self.arcRadius = radius 101 self.robot.sendCommand(self.device + ".leftArcMilli." + str(radius)) 102 self.state = GearState.LEFTARC 103 else: 104 self.robot.sendCommand(self.device + ".leftArcMilli." + str(radius) + "." + str(duration)) 105 self.state = GearState.STOPPED
106
107 - def rightArc(self, radius, duration = 0):
108 self._checkRobot() 109 radius = int(1000 * radius) 110 if duration == 0: 111 if self.state == GearState.RIGHTARC and radius == self.arcRadius: 112 return 113 self.arcRadius = radius 114 self.robot.sendCommand(self.device + ".rightArcMilli." + str(radius)) 115 self.state = GearState.RIGHTARC 116 else: 117 self.robot.sendCommand(self.device + ".rightArcMilli." + str(radius) + "." + str(duration)) 118 self.state = GearState.STOPPED
119
120 - def stop(self):
121 self._checkRobot() 122 if self.state == GearState.STOPPED: 123 return 124 self.robot.sendCommand(self.device + ".stop") 125 self.state = GearState.STOPPED
126
127 - def _checkRobot(self):
128 if RobotInstance.getRobot() == None: 129 raise Exception("Create Robot instance first")
130