本文整理汇总了Python中Engine.get_count方法的典型用法代码示例。如果您正苦于以下问题:Python Engine.get_count方法的具体用法?Python Engine.get_count怎么用?Python Engine.get_count使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Engine
的用法示例。
在下文中一共展示了Engine.get_count方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import Engine [as 别名]
# 或者: from Engine import get_count [as 别名]
class Controller:
def __init__(self):
# Storing the engines of this car
self.__leftengine = Engine("A")
self.__rightengine = Engine("B")
self.__engines = [self.__leftengine, self.__rightengine]
# Storing the distance between the centers of the cars
self.__widthcar = 2.6 + 11.1 - 0.2
self.__gearratio = 1.0
# Storing the perimeter of the wheels (2*pi*r)
self.__perimeter = 2 * math.pi * 2.579
# Storing a reference to a brickpi thread
self.__brickpi = BrickPi_Thread(self.__engines)
self.__command_going = False
self.__command_thread = None
# Start a commands
# If there is already a command going, stop that first
# Start thread
# @post self.__command_going = True
# @post self.__command_thread = new thread
def start_command(self, command, arguments=None):
if self.__command_going:
self.stop_command()
self.__brickpi.on()
self.__command_going = True
if arguments != None:
thread = threading.Thread(target=command, args=arguments)
else:
thread = threading.Thread(target=command)
self.__command_thread = thread
self.__command_thread.setDaemon("True")
self.__command_thread.start()
# A method to stop the going command
# if there is a command going stop it
# @post self.__command_going = False
# @post self.__command_thread = None
def stop_command(self):
if self.__command_going:
self.__brickpi.off()
self.__command_going = False
self.__command_thread.join()
self.__command_thread = None
def BrickPi_start(self):
self.__brickpi.on()
# A method to set the speed of the engines
# If the number of speeds don't equal the number of engines an error is raised
# def set_speed_engines(self,speed):
# if len(speed) != len(self.__engines):
# raise Exception
# engines = self.__engines
# for i in range(engies):
# engines[i].set_speed(speed[i])
# A method to flush the counter values of the engines
def flush_engines(self):
for engine in self.__engines:
engine.reset_count()
# return the distance traveled by the engines
def get_engine_distance(self):
result = []
for engine in self.__engines:
result.append(engine.get_count() * self.__perimeter * self.__gearratio)
return result
# Turn the thread back off
def kill_threads(self):
self.__brickpi.off()
## Return the values of the sensor in a dictionary with keys
## Distancesensor1 -> GPIO distance sensor
## Distancesensor2 -> Lego MINDSTORM distance sensor
def get_sensor_data(self):
result = dict()
result["SpeedLeft"] = self.__leftengine.get_speed()
result["SpeedRight"] = self.__rightengine.get_speed()
## result['Top angle'] = (self.__topengine.get_count()%1) *2*math.pi
return result
# A method to drive forward
def forward(self):
pid = PID.PID(15.0, 1 / 2.0, 2.0, 0.1)
self.__leftengine.set_speed(240)
self.__rightengine.set_speed(240)
self.__leftengine.reset_count()
self.__rightengine.reset_count()
while self.__command_going:
distance1 = self.__leftengine.get_count() * self.__perimeter * self.__gearratio
distance2 = self.__rightengine.get_count() * self.__perimeter * self.__gearratio
speeddif = pid.new_value(distance1 - distance2, 0.1)
self.__rightengine.set_speed(240 + speeddif)
time.sleep(0.05)
self.__leftengine.set_speed(0)
self.__rightengine.set_speed(0)
# A mehtod to drive backward
def backward(self):
#.........这里部分代码省略.........
示例2: __init__
# 需要导入模块: import Engine [as 别名]
# 或者: from Engine import get_count [as 别名]
class Interface:
global MINIMUM_SPEED, BATTERY, DEBUG
def __init__(self):
# Storing the engines of this car
self.__leftengine = Engine('A')
self.__rightengine = Engine('B')
self.__topengine = Engine('C')
self.__engines = [self.__leftengine,self.__rightengine,self.__topengine]
# Storing the distance between the centers of the cars
self.__widthcar = 2.6 + 11.1 - .2
# Storing the Lego MINDSTORM distance sensor
self.__distanceLego = MindstormSensor('1','ULTRASONIC_CONT')
# Storing the GPIO distance sensor
self.__distancePi = DistanceSensor(17,4)
# Storing the current gearration
self.__gearratio = 1./1.
# Storing the perimeter of the wheels (2*pi*r)
self.__perimeter = 2*math.pi*2.758
# Storing a reference to a brickpi thread
self.__brickpi = BrickPi_Thread(self.__engines,[self.__distanceLego])
# Storing a reference to a gpio thread
self.__gpio = GPIO_Thread([self.__distancePi])
# Turn the threads on
self.__brickpi.on()
self.__gpio.on()
## Return the values of the sensor in a dictionary with keys
## Distancesensor1 -> GPIO distance sensor
## Distancesensor2 -> Lego MINDSTORM distance sensor
## Coming soon : Top angle -> The angle of the top motor
## Note: Not yet tested
def get_sensor_value(self):
result['Distancesensor1'] = self.__distancePi.get_value()
## result['Top angle'] = (self.__topengine.get_count()%1) *2*math.pi
return result
## Turn the thread back off
def kill_threads(self):
self.__brickpi.off()
self.__gpio.off()
## Set the speeds of the engines
## @param speed = [speed leftengine, speed rightengine , speed topengine]
def set_engines_speed(self,speed):
if len(self.__engines) != len(speed):
raise Error()
else:
for i in range(len(self.__engines)):
self.__engines[i].set_speed(speed[i])
## Method to ride a given time
def ride_time(self,run_time,speed):
start_time = time.time()
self.__leftengine.set_speed(speed)
self.__rightengine.set_speed(speed)
while (time.time() - start_time < run_time):
time.sleep(0.1)
self.__leftengine.set_speed(0)
self.__rightengine.set_speed(0)
def ride_distance(self,distance):
self.__leftengine.reset_count()
self.__rightengine.reset_count()
pid1 = PID.PID(5.,1/20.,1/50.,.5)
pid2 = PID.PID(20.,1/2.,1/5.,.5)
speed = MINIMUM_SPEED
if DEBUG:
print speed
self.__leftengine.set_speed(speed)
self.__rightengine.set_speed(speed)
while speed !=0:
distance1 = self.__leftengine.get_count()*self.__perimeter*self.__gearratio
distance2 = self.__rightengine.get_count()*self.__perimeter*self.__gearratio
if DEBUG:
print 'Distance:', distance1, distance2
speed = pid1.new_value(distance-distance1,0.01)
speed_diff = pid2.new_value(distance1-distance2,0.01)
if DEBUG:
print 'Speed + Speed diff: ', speed, speed_diff
lspeed,rspeed = self.correct_speed(speed,speed_diff)
if DEBUG:
print 'Corrected speed:', lspeed,rspeed
self.__leftengine.set_speed(lspeed)
self.__rightengine.set_speed(rspeed)
time.sleep(0.01)
self.__leftengine.set_speed(0)
self.__rightengine.set_speed(0)
def correct_speed(self,speed,speed_diff):
# If the speed or speed + diff is bigger then 255 a correction must happen
if speed > 255 or speed+speed_diff > 255:
# if speed_diff bigger is bigger then 0 the left engine must slow down
if speed_diff > 0:
return [255-abs(speed_diff),255]
# if speed_diff < 0 the right engine must slow down
else:
return [255,255 - abs(speed_diff)]
# If the speed or speed+ speed_diff is less than 255
elif speed < -255 or speed + speed_diff < -255:
# If speed_diff bigger then 0 right engine must slow down
if speed_diff > 0:
return [-255,-255+abs(speed_diff)]
# If speed diff smaller then 0 the left engine must slow down
#.........这里部分代码省略.........