当前位置: 首页>>代码示例>>Python>>正文


Python Motor.setSpeed方法代码示例

本文整理汇总了Python中motor.Motor.setSpeed方法的典型用法代码示例。如果您正苦于以下问题:Python Motor.setSpeed方法的具体用法?Python Motor.setSpeed怎么用?Python Motor.setSpeed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在motor.Motor的用法示例。


在下文中一共展示了Motor.setSpeed方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: MotorDriver

# 需要导入模块: from motor import Motor [as 别名]
# 或者: from motor.Motor import setSpeed [as 别名]
class TantrumMotors:
    """
    This class is designed to control the motors of a Tyco Tantrum
    RC car. The Tantrum has a front motor controlling direction and
    a rear motor for moving forwards and backwards.
    """

    _front = 0  # Motor
    _rear = 0  # Motor
    _frontDriver = MotorDriver("P9_11", "P9_14")
    _rearDriver = MotorDriver("P9_12", "P9_16")

    # <public>
    # Initialize motors to forward, stopped
    def __init__(self):
        self._front = Motor(self._frontDriver)
        self._rear = Motor(self._rearDriver)
        self._front.setDirCw()
        self._front.setSpeed(0)
        self._rear.setDirCcw()
        self._rear.setSpeed(0)

    # Set the speed for all motors, if a speed is negative then the
    # motor is put in reverse.
    def setSpeed(self, front, rear):
        front = int(front)
        rear = int(rear)

        self._front.setSpeed(front)
        if (front < 0):
            self._front.setDirCcw()
        else:
            self._front.setDirCw()

        self._rear.setSpeed(rear)
        if (rear < 0):
            self._rear.setDirCcw()
        else:
            self._rear.setDirCw()

    def getFrontSpeed(self):
        return self._front.getSpeed()

    def getRearSpeed(self):
        return self._rear.getSpeed()

    def getFrontDirection(self):
        return self._front.getDirection()

    def getRearDirection(self):
        return self._rear.getDirection()

    def updateMotorState(self):
        self._frontDriver.updateState()
        self._rearDriver.updateState()

    def __exit__(self, type, value, traceback):
        self._frontDriver.shutdown()
        self._rearDriver.shutdown()
开发者ID:Grated,项目名称:Beaglebone,代码行数:61,代码来源:tantrum_motors.py

示例2: parameters

# 需要导入模块: from motor import Motor [as 别名]
# 或者: from motor.Motor import setSpeed [as 别名]

#.........这里部分代码省略.........

	def __exit__(self, exc_type, exc_value, traceback):
		self.interface.terminate()

	#####################
	### Motor control ###
	#####################
	
	"""
	Sets PID values for all motors
	"""
	def setPID(self, p, i, d):
		self.motorL.setPID(p, i, d)
		self.motorR.setPID(p, i, d)
	
	"""
	Whether any of the motors are rotating
	"""
	def isMoving(self):
		return self.motorL.isRotating() or self.motorR.isRotating()

	"""
	Moves the given distance forwards or backwards
	"""
	def move(self, distance):
		# distance specified in metres
		wheel = distance * self.movementCoeff
		self.startLogging(['move', distance])
		self.motorL.rotate(self.powerL * wheel)
		self.motorR.rotate(self.powerR * wheel)
		self.stopLogging()
	
	"""
	Rotates the whole robot by the given angle clockwise
	"""
	def rotate(self, angle):
		# angle specified in degrees
		wheel = self.rotatePower * angle * (math.pi/180) * self.botRadius * self.movementCoeff
		self.startLogging(['turn', angle])
		self.motorL.rotate( self.powerL * wheel)
		self.motorR.rotate(-self.powerR * wheel)
		self.stopLogging()

	def drive(self, speedL, speedR):
		self.motorL.setSpeed(speedL)
		self.motorR.setSpeed(speedR)

	def arcPath(self, theta):
		# theta = 0 returns to straight path
		# Positive theta => too far to the right => turn left
		if theta <= 0 :
			self.powerL = 1 
			self.powerR = 1 + theta
		else: 		
			self.powerL = 1 - theta
			self.powerR = 1 
	
	"""
	Blocks the program until the robot stops moving
	"""
	def wait(self):
		while self.isMoving():
			time.sleep(self.deltaTime)

	######################
	### Sensor control ###
	######################

	"""
	Starts the main loop, checking sensors and responding to events until
	`running` is false
	"""
	def mainLoop(self):
		self.running = True
		while self.running:
			self.touchSensorL.check()
			self.touchSensorR.check()
			self.ultraSonic.check()
			self.motorL.check()
			self.motorR.check()
			time.sleep(self.deltaTime)

	def sensorAction(self, params):
		if(params['position'] != 'either' or not params['down']):
			return
		self.wait()
		# function triggered by the event handler when the touchsensor values are changed.
		if (self.touchSensorL.getState() == EventState.SENSOR_TOUCH_DOWN) \
			and (self.touchSensorR.getState() == EventState.SENSOR_TOUCH_DOWN):
			self.move(-20)
			self.wait()
			self.rotate(self, 90)
		elif self.touchSensorL.getState() == EventState.SENSOR_TOUCH_DOWN:
			self.move(-10)
			self.wait()
			self.rotate(self, 45)
		elif self.touchSensorL.getState() == EventState.SENSOR_TOUCH_DOWN:
			self.move(-10)
			self.wait()
			self.rotate(self, -45)
开发者ID:patengelbert,项目名称:robotics-CO333,代码行数:104,代码来源:robot.py


注:本文中的motor.Motor.setSpeed方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。