from Wheel import Wheel import wiringpi class Tank: # 初始速度为0 __speed=0 # 记录小车状态 __status='stop' # 转向PWM增益 __turningPWMIncrease=1.4 def __init__(self): wiringpi.wiringPiSetup() self.__left=Wheel(pin1=23,pin2=22,pwm=25) self.__right=Wheel(pin1=28,pin2=29,pwm=26) def forward(self): self.__status='forward' self.setSpeed(self.__speed) self.__left.forward() self.__right.forward() def backward(self): self.__status='backward' self.setSpeed(self.__speed) self.__left.backward() self.__right.backward() def left(self): self.__status='left' self.setSpeed(self.__speed) self.__left.backward() self.__right.forward() def right(self): self.__status='right' self.setSpeed(self.__speed) self.__left.forward() self.__right.backward() def stop(self): self.__status='stop' self.__left.stop() self.__right.stop() def speedUp(self): self.__speed=self.__speed+5 if self.__speed>50: self.__speed=50 self.setSpeed(self.__speed) def speedDown(self): self.__speed=self.__speed-5 if self.__speed<15: self.__speed=15 self.__left.setSpeed(self.__speed) def setSpeed(self,speed): self.__speed=speed self.__setRunSpeed(self.__speed) def __setRunSpeed(self,speed): if self.__status=='left' or self.__status=='right': # 如果是转弯,要给加大速度,才能用同样的速度转动 turnSpeed = int(self.__speed*self.__turningPWMIncrease) print("转向速度:"+str(turnSpeed)) self.__left.setSpeed(turnSpeed) self.__right.setSpeed(turnSpeed) else: self.__left.setSpeed(self.__speed) self.__right.setSpeed(self.__speed)