1   
 2   
 3  ''' 
 4  Class that represents a servo motor. 
 5   
 6   This software is part of the raspibrick module. 
 7   It is Open Source Free Software, so you may 
 8   - run the code for any purpose 
 9   - study how the code works and adapt it to your needs 
10   - integrate all or parts of the code in your own programs 
11   - redistribute copies of the code777 
12   - improve the code and release your improvements to the public 
13   However the use of the code is entirely your responsibility. 
14   ''' 
15   
16  from Tools import Tools 
17  from RobotInstance import RobotInstance 
18  import SharedConstants 
19   
21      ''' 
22      Class that represents a servo motor. 
23      ''' 
25          ''' 
26          Creates a servo motor instance and sets it at home position. For most servos: 
27          home = 300, inc = 2 
28          @param id: the id of the motor (0..3) 
29          @param home: the PWM duty cycle for the home position (0..4095) 
30          @param inc: the increment factor (inc_duty/inc_position) 
31          ''' 
32          self.id = id 
33          self.home = home 
34          self.inc = inc 
35          self._checkRobot() 
36          self.robot = RobotInstance.getRobot() 
37          self.robot.pwm.setDuty(SharedConstants.SERVO_0 + self.id, home) 
38          Tools.debug("ServoMotor instance created") 
 39   
41          ''' 
42          Sets the relative position of the servo motor. 
43          @param position: the position with respect to home and using the inc_duty/inc_position factor 
44          For most servo motors in range -200 .. 200 
45          ''' 
46          self._checkRobot() 
47          pos = self.home + self.inc * position 
48          self.robot.pwm.setDuty(SharedConstants.SERVO_0 + self.id, pos) 
 49   
51          ''' 
52          Sets the absolute position of the servo motor. 
53          @param position: the position in arbitrary units in range 0..4095 (determines PWM duty cycle) 
54          For most servo motors in range 100..500 
55          ''' 
56          self._checkRobot() 
57          self.robot.pwm.setDuty(SharedConstants.SERVO_0 + self.id, position) 
 58   
 62