class AvoidEdge(Behavior): def __init__(self, BBCON, priority): super().__init__(BBCON) print("We are going in to calibration mode in 2 sec") time.sleep(2) self.sensob = ReflectanceSensors(auto_calibrate=True) self.priority = priority #Preset value that is set by the user self.sensob.update() self.old = self.sensob.get_value() def printName(self): return "Behavior AvoidEdge " def update(self): super().considerState() if self.active_flag: self.sense_and_act() self.weight = self.priority * self.match_degree def sense_and_act(self): self.sensob.update() self.new = self.sensob.get_value() print(sum(self.old)/len(self.old) - sum(self.new)/len(self.new) > 0.1) if (sum(self.old)/len(self.old) - sum(self.new)/len(self.new) > 0.1 ): self.motor_recommendations = [-1,-1] self.match_degree = 1 else: self.match_degree = 0 self.motor_recommendations = [1,1] self.old = self.new def test(self): return True
class ReflectanceSensob(Sensob): def __init__(self): super(ReflectanceSensob, self).__init__() self.sensor = ReflectanceSensors() self.sensors.append(self.sensor) def get_value(self): ''' returnerer en liste med verdier: [left, midleft, midright right]''' return self.value def update(self): self.sensor.update() self.value = self.sensor.get_value() return self.value
def test4(): sensor = ReflectanceSensors() ZumoButton().wait_for_press() m = Motors() motob = Motob(m) sensob = Sensob() sensob.set_sensors([sensor]) print(sensor.get_value()) behavior = FollowLine(1, [sensob]) #print("Behavior sensob:", behavior.sensobs) count = 0 while True: sensob.update() behavior.update() print("MR:", behavior.motor_recommendations) motob.update(behavior.motor_recommendations[0]) count += 1 #time.sleep(3) if count == 15: break
class FollowLine: def __init__(self): self.reflectanseSensor = ReflectanceSensors() self.values = self.getValueList() self. values = self.isOnLine() def getValueList(self): self.reflectanseSensor.reset() self.reflectanseSensor.update() return self.reflectanseSensor.get_value() def isOnLine(self): nowValue = self.getValueList() offLineList = [0,1,4,5] for i in range(6): if i in offLineList and nowValue[i] < 0.2: return [(1000-(nowValue[i]*1000)),i] return [0,10] def getPriValues(self): return self.isOnLine()
class FallingOut(Sensob): def __init__(self): super(FallingOut, self).__init__() self.sensor = ReflectanceSensors(max_reading=3000, min_reading=1000) self.value = [] def update(self): self.sensor.update() self.value = self.sensor.get_value() def reset(self): pass def calibrate(self): self.sensor = ReflectanceSensors(auto_calibrate=True) def interpret(self): danger = 0 if self.value[0] < 0.65: danger = -1 elif self.value[5] < 0.65: danger = 1 return danger
def tourist(steps=25,shots=5,speed=.25): ZumoButton().wait_for_press() rs = ReflectanceSensors(); m = Motors(); mr = get_recc(rs.get_value()) update_motobs(mr, m)