예제 #1
0
 def __init__(self,
              car_serial,
              base_speed=100,
              proportional=0.4,
              integral=0,
              diff=0):
     """
         初始化类
     :param car_serial: 串口通信类
     :param base_speed: 车子在直线行走时两个轮子的速度(-255,255)
     :param proportional: PID比例参数
     :param integral: PID的积分参数
     :param diff: PID的微分参数
     """
     self.__serial = car_serial
     self.base_speed = base_speed
     self.proportional = proportional
     self.__offset = 0
     self.task_list = []
     self.__function_timer = CarTimer()  # 提供一个全局的计时器供函数使用
     self.task_list.append(
         CarTask(name="follow_line",
                 activated=True,
                 priority=5,
                 work=self.__follow_line))
예제 #2
0
def do(direction=True, delay=3):
    task_list.append(
        CarTask(name="hello",
                activated=True,
                priority=1,
                timer=CarTimer(interval=delay),
                work=_do_work,
                direction=direction))
예제 #3
0
 def stop(self, delay_time=0):
     """
     如果delay_time = 0 将完全停止,如果delay_time> 0 将会暂停几秒
     :param delay_time: 暂停的时间,0将无限时暂停
     """
     if delay_time == 0:
         self.task_list.append(
             CarTask(name="stop",
                     activated=True,
                     priority=0,
                     work=self.__stop))
     else:
         self.task_list.append(
             CarTask(name="pause",
                     activated=True,
                     priority=1,
                     timer=CarTimer(interval=delay_time),
                     work=self.__stop))
예제 #4
0
 def go_straight(self, delay_time=8):
     """
     直接向前走,不要巡线
     :param delay_time: 延迟时间
     """
     self.task_list.append(
         CarTask(name="go_straight",
                 activated=True,
                 priority=3,
                 timer=CarTimer(interval=delay_time),
                 work=self.__go_straight))
예제 #5
0
 def turn(self, direction=True, delay_time=1):
     """
     转弯
     :param direction: 方向(True为左,False为右)
     :param delay_time: 转弯延迟时间
     """
     self.task_list.append(
         CarTask(name="turn",
                 activated=True,
                 priority=2,
                 timer=CarTimer(interval=delay_time),
                 work=self.__turn,
                 direction=direction))
예제 #6
0
 def servo_move(self, angle, delay_time=1):
     """
     旋转舵机到指定的角度,舵机的旋转需要一定的时间,在这段时间内Arduino将不会响应nano的传输的命令
     delay_time用于指定这一段时间,同时本函数应该避免在这段时间内反复调用,否则会出现Arduino因为无法响应指令而出错。
     :param angle: 转向角度
     :param delay_time: 延迟时间
     """
     self.__serial.drive_servo(
         angle)  # 向发一个指令给Arduino,然后用下面的任务占着时间,不给其他任务执行
     self.task_list.append(
         CarTask(name="servo_move",
                 activated=True,
                 priority=1,
                 timer=CarTimer(interval=delay_time),
                 work=self.__sevo_move))
예제 #7
0
 def bypass_obstacle(self, first_delay_time, second_delay_time):
     """
     避障函数,第一个时间段主要通过右轮的倒转,快速旋转,第二个时间段,通过缓慢的偏转回归到主线上
     :param first_delay_time:偏离主线的运行时间
     :param second_delay_time: 回归主线的运行时间
     """
     self.__function_timer.restart()
     ct = CarTimer(interval=first_delay_time + second_delay_time + 1.5)
     self.task_list.append(
         CarTask(name="bypass",
                 activated=True,
                 priority=2,
                 timer=ct,
                 work=self.__bypass_obstacle,
                 first_delay_time=first_delay_time,
                 second_delay_time=second_delay_time))
예제 #8
0
 def group(self, base_control_list):
     """
     连续执行BaseControl列表的函数
     :param base_control_list: 必须提供一个BaseControl对象的List
     """
     bc_list = base_control_list
     delay_time = 0.0
     for bc in bc_list:
         delay_time += bc.delay_time
     self.__function_timer.restart()
     self.task_list.append(
         CarTask(name="group_control",
                 activated=True,
                 priority=2,
                 timer=CarTimer(interval=delay_time + 0.2),
                 work=self.__group_control,
                 base_control_list=base_control_list))