Beispiel #1
0
 def motorEnd(self):
     util.traceCall()
     self.motorStop()
     self._nb_instances -= 1
     if self._nb_instances == 0:
         # Reset the GPIO pins (turn off motors too)
         GPIO.cleanup()
Beispiel #2
0
    def __init__(self, *args):
        util.traceCall()
        if len(args) == 1 and isinstance(args[0], dict):
            self.__dict__ = args[0]
        elif len(args) == 3:
            name, pinF, pinB = args
            self.name = name
            self._pinForward = pinF
            self._pinBackward = pinB

        # Set the GPIO modes
        util.trace("set the GPIO mode and the pins " + str(self._pinForward) +
                   " and " + str(self._pinBackward))
        if self._nb_instances == 0:
            GPIO.setmode(GPIO.BCM)
            GPIO.setwarnings(False)
        GPIO.setup(self._pinForward, GPIO.OUT)
        GPIO.setup(self._pinBackward, GPIO.OUT)

        # Set the GPIO to software PWM at 'Frequency' Hertz
        self._pwmMotorForward = GPIO.PWM(self._pinForward, self._frequency)
        self._pwmMotorBackward = GPIO.PWM(self._pinBackward, self._frequency)

        # Start the software PWM with a duty cycle of 0 (i.e. not moving)
        self._pwmMotorForward.start(self._stopCycle)
        self._pwmMotorBackward.start(self._stopCycle)

        # end of instance initialization
        self._nb_instances += 1
Beispiel #3
0
 def basicMotorStart(self, way):
     util.traceCall()
     if util.getDebug() > 0:
         return
     if way == self._way[1] or way == 1:
         self.basicMotorBackward()
     else:
         self.basicMotorForward()
Beispiel #4
0
 def robotEnd(self):
     util.traceCall()
     self._wheel_left.motorEnd()
     self._wheel_right.motorEnd()
     self._nb_instances -= 1
     if self._nb_instances == 0:
         # nothing
         pass
Beispiel #5
0
 def motorStop(self):
     util.traceCall()
     if util.getDebug() > 0:
         return
     util.trace("_pwmMotorForward.ChangeDutyCycle to stop")
     util.trace("_pwmMotorBackward.ChangeDutyCycle to stop")
     self._pwmMotorForward.ChangeDutyCycle(self._stopCycle)
     self._pwmMotorBackward.ChangeDutyCycle(self._stopCycle)
Beispiel #6
0
 def setSpeed(self, v):
     util.traceCall()
     if v <= self.getMaxSpeed():
         if self._curSpeed != v:
             # apply the new speed
             self._curSpeed = v
             self.motorUpdate()
         else:
             util.trace("The speed was already " + v)
     else:
         util.trace("The speed must be lower than " + self.getMaxSpeed())
Beispiel #7
0
 def setWay(self, way):
     util.traceCall()
     if way in self._way:
         if self._curWay != way:
             # apply the new way
             self._curWay = way
             self.motorStop()
             self.motorUpdate()
         else:
             util.trace("The way was already " + way)
     else:
         util.trace("The way must be in " + str(self._way))
Beispiel #8
0
    def __init__(self, *args):
        util.traceCall()
        if len(args) == 1 and isinstance(args[0], dict):
            self.__dict__ = args[0]
        elif len(args) == 3:
            name = args
            self.name = name

        # Define the 2 wheels
        util.trace("Define the 2 wheels")
        self._wheel_left = motor.Motor("left_wheel", 8, 7)
        self._wheel_right = motor.Motor("right_wheel", 10, 9)

        # end of instance initialization
        self._nb_instances += 1
Beispiel #9
0
 def motorUpdate(self):
     util.traceCall()
     if util.getDebug() > 0:
         return
     if self._curWay == 0:
         # forward
         util.trace("_pwmMotorForward.ChangeDutyCycle to " +
                    str(self._dutyCycle * self._curSpeed))
         util.trace("_pwmMotorBackward.ChangeDutyCycle to stop")
         self._pwmMotorForward.ChangeDutyCycle(self._dutyCycle *
                                               self._curSpeed)
         self._pwmMotorBackward.ChangeDutyCycle(self._stopCycle)
     else:
         # backward
         util.trace("_pwmMotorForward.ChangeDutyCycle to stop")
         util.trace("_pwmMotorBackward.ChangeDutyCycle to " +
                    str(self._dutyCycle * self._curSpeed))
         self._pwmMotorForward.ChangeDutyCycle(self._stopCycle)
         self._pwmMotorBackward.ChangeDutyCycle(self._dutyCycle *
                                                self._curSpeed)
Beispiel #10
0
 def getSpeed(self):
     util.traceCall()
     return self._curSpeed
Beispiel #11
0
 def getMaxSpeed(self):
     util.traceCall()
     return self._maxSpeed[self._curWay]
Beispiel #12
0
 def turnLeftForward(self):
     util.traceCall()
     return self._curSpeed
Beispiel #13
0
 def setWay(self, way):
     util.traceCall()
Beispiel #14
0
 def robotStop(self):
     util.traceCall()
     self._wheel_left.motorStop()
     self._wheel_right.motorStop()
#!/usr/bin/python

# CamJam EduKit 3 - Robotics
# Worksheet 4 - Driving and Turning

#import RPi.GPIO as GPIO # Import the GPIO Library
import time
#import lib_motors_base as base
import motor_class as motor
import lib_util as util


#base.init()
util.setDebug(0)
util.setTrace(1)
util.traceCall()

wheel = motor.Motor("wheel", 8, 7)
wheel.basicMotorStop()
wheel.basicMotorForward()
time.sleep(0.2)
wheel.basicMotorStop()
time.sleep(0.5)
wheel.basicMotorBackward()
time.sleep(0.2)
wheel.basicMotorStop()
time.sleep(0.5)


wheel.motorEnd()
Beispiel #16
0
 def basicMotorBackward(self):
     util.traceCall()
     if util.getDebug() > 0:
         return
     GPIO.output(self._pinForward, 0)
     GPIO.output(self._pinBackward, 1)
Beispiel #17
0
 def moveStraight(self, way, speed):
     util.traceCall()
     self._wheel_left.setWay(way)
     self._wheel_right.setWay(way)
     self._wheel_left.setSpeed(speed)
     self._wheel_right.setSpeed(speed)