示例#1
0
文件: parrot.py 项目: watrt/mpython
    def get_learn_data(self):
        """
        返回学习后的,红外编码数据。该函数须在 ``learn`` 函数后使用。

        :return: 返回编码后的红外脉冲数据buff
        """
        i2c.writeto(16, b'\x07')
        return i2c.readfrom(16, 118)
示例#2
0
    def move(self, speed, ditance=0xffffffff):
        """ 
        机器人前进、后退

        :param speed: 速度 取值范围: -100 -- 100 正值代表前进
        :param distance: 前时后退时,表示移动距离 0 -- 0xffffffff,单位:mm 为0xffffffff时表示持续行走。 
        """
        tmp = ustruct.pack('<b2I', 0x92, speed, ditance)
        i2c.writeto(17, tmp)
示例#3
0
 def set_motor(self, num, speed):
     """ 
     控制单个马达
     
     :param num: 马达号, 1:左马达 2:右马达
     :param speed: 速度 取值范围: -100 -- 100
     """
     tmp = ustruct.pack('<3b', 0x94, num, speed)
     i2c.writeto(17, tmp)
示例#4
0
    def set_threshold(self, threshold):
        """ 
        设置跌落检测阈值

        :param threshold: 四个角的防跌落检测阈值。 
        """
        tmp = ustruct.pack('<b4H', 0x96, threshold[0], threshold[1],
                           threshold[2], threshold[3])
        i2c.writeto(17, tmp)
示例#5
0
    def get_raw(self):
        """ 
        获取裸数据

        :return: tuple 裸数据元组(r,g,b,c)
        """
        i2c.writeto(17, b'\x03', False)
        raw = ustruct.unpack('HHHH', i2c.readfrom(17, 8))
        return raw
示例#6
0
    def get_rgb(self):
        """ 
        获取RGB值,通过反射方式获取物体颜色时,须开启亮暗平衡调整功能。

        :return: tuple RGB颜色值元组(r,g,b)
        """
        i2c.writeto(17, b'\x04', False)
        rgb = ustruct.unpack('BBB', i2c.readfrom(17, 3))
        return rgb
示例#7
0
    def get_hsv(self):
        """ 
        获取hsv值,通过反射方式获取物体颜色时,须开启亮暗平衡调整功能。

        :return: tuple hsv颜色值元组(h,s,v)
        """
        i2c.writeto(17, b'\x05', False)
        hsv = ustruct.unpack('Iff', i2c.readfrom(17, 12))
        return hsv
示例#8
0
文件: parrot.py 项目: watrt/mpython
def get_battery_level():
    """
    获取拓展板的电压值。

    :return: 返回电量,单位mV。
    """
    i2c.writeto(16, b'\x03')
    tmp = i2c.readfrom(16, 2)
    return struct.unpack('H', tmp)[0]
示例#9
0
    def rotate(self, speed, angle):
        """ 
        机器人旋转

        :param speed: 速度 取值范围: 0 -- 100
        :param angel: 转角,单位:度 取值范围:-360 -- 360  正值代表右转
        """
        tmp = ustruct.pack('<b2H', 0x93, speed, angle)
        i2c.writeto(17, tmp)
示例#10
0
def run(port, direction, speed):
    '''
    port: 0 for M1, 2 for M2
    direction: 0 or 1
    speed: 0~255
    '''
    global i2c
    i2c.writeto(0x10, bytearray([port, direction, speed]))
    return
示例#11
0
 def set_ir(self, on):
     """ 
     防跌落传感器发射管开关,使用防跌落传感器需打开发射管。
     
     :param on: True 打开 False 关闭
     """
     if on:
         i2c.writeto(17, b'\x8E')
     else:
         i2c.writeto(17, b'\x8F')
示例#12
0
 def pid_tracking(self, enable):
     """ 
     使能/禁能内置PID循迹功能
     
     :param enable: True 使能 False 禁止
     """
     if enable:
         i2c.writeto(17, b'\x88')
     else:
         i2c.writeto(17, b'\x89')
示例#13
0
 def set_led(self, on):
     """ 
     循迹传感器led开关,使用循迹传感器前需打开LED。
     
     :param on: True 开 False 关
     """
     if on:
         i2c.writeto(17, b'\x86')
     else:
         i2c.writeto(17, b'\x87')
示例#14
0
 def balance_en(self, en):
     """ 
     使能/禁能亮暗平衡调整功能,获取反射型物体颜色时,须调用本函数使能亮暗平衡调整功能。
     
     :param en: True:使能 False:禁能
     """
     if en:
         i2c.writeto(17, b'\x97')
     else:
         i2c.writeto(17, b'\x98')
示例#15
0
 def set_balance(self, mode):
     """ 
     设置亮暗平衡调整参数,设置后的参数值,会保存到flash中,所以通常本函数只需执行一次。
     
     :param mode: 1:设置亮平衡 2:设置暗平衡
     """
     if mode == 1:
         i2c.writeto(17, b'\xF2')  # white balance
     elif mode == 2:
         i2c.writeto(17, b'\xF3')  # black balance
示例#16
0
    def set_led(self, on):
        """ 
        打开/关闭补光灯

        :param on: bool True 打开 False 关闭
        """
        if on:
            i2c.writeto(17, b'\x8A')  # color led on
        else:
            i2c.writeto(17, b'\x8B')  # color led off
示例#17
0
    def get_temp(self):
        """ 
        获取色温值

        :return: int 色温值
        """
        self.set_led(False)
        i2c.writeto(17, b'\x06', False)
        color_temp = ustruct.unpack('H', i2c.readfrom(17, 2))[0]
        return color_temp
示例#18
0
    def get_lux(self):
        """ 
        获取亮度值

        :return: int 亮度值,单位lux
        """
        self.set_led(False)
        i2c.writeto(17, b'\x08', False)
        lux = ustruct.unpack('H', i2c.readfrom(17, 2))[0]
        return lux
示例#19
0
文件: parrot.py 项目: watrt/mpython
    def send(self, buff, repeat_en=0):
        """
        发送红外脉冲数据buff

        :param bytes buff: 可以是 ``IR_encode.encode_nec`` 返回的buff。或,学习后的buff。
        :param init repeat_en: 重复码。用于表示按键常按。当repeat_en为1时,会不断发送重复码,直至 ``stop_send`` 停止发送。
        """
        tmp = bytearray([0x04, repeat_en])
        tmp += buff
        # print(tmp)
        i2c.writeto(16, tmp)
示例#20
0
    def get_sensor_val(self):
        """ 
        获取防跌落传感器AD采样值

        :return: tuple 四个防跌落传感器AD采样值
        """
        if self.drop_detct_ir_on == False:
            self.drop_detct_ir_on = True
            i2c.writeto(17, b'\x8E')  #打开防跌红外发射管
        i2c.writeto(17, b'\x01', False)  #发出读ADC数据指令
        tmp = ustruct.unpack('HHHHHHHH', i2c.readfrom(17, 16))
        drop_detect_data = tmp[4:8]
        return drop_detect_data
示例#21
0
    def get_val(self):
        """ 
        获取循迹AD采样值

        :return: 四个循迹传感器AD采样值
        """
        if not self.tracking_led_on:
            self.tracking_led_on = True
            self.set_led(1)
        i2c.writeto(17, b'\x01', False)  # 获取AD值
        tmp = ustruct.unpack('HHHHHHHH', i2c.readfrom(17, 16))
        tracking_data = tmp[0:4]
        return tracking_data
示例#22
0
 def get_params(self):
     """ 
     获取机器人所有配置参数
     
     :return: 示例:(85, 0.002, 0.005, 1.0, 1.0, 1.0,  8, 11, 10, 30, 254, 3411, 3157, 259, 3444, 3185, 254, 3498, 3244, 252, 3305, 3053, 2000, 2000, 2000, 2000)  
     | 85: 标记   
     | 0.002: 行走补偿 
     | 0.005: 旋转补偿  
     | 1.0, 1.0, 1.0: 亮平衡RGB比例值  
     | 8, 11, 10, 30: 暗平衡rgbc值  
     | 254, 3411, 3157, 259: 循迹传感器1白平衡亮、值、差值 有4组  
     """
     i2c.writeto(17, b'\x71', False)
     sys_params = ustruct.unpack('I5f20H', i2c.readfrom(17, 56))
     return sys_params
示例#23
0
文件: parrot.py 项目: watrt/mpython
    def learn(self, wait=True):
        """
        红外学习。默认下wait为True,此时为阻塞函数。learn()开始后,须在5秒内,常按住被学习对象按键。当学习完成后,会返回学习结果,成功True,失败Fail。wait 为 False,则为非阻塞,此时不返回结果。

        :param bool wait: 是否阻塞。
        :return: bool类型,返回结果
        """
        i2c.writeto(16, b'\x06')
        print("Start Learning: the learning object should hold down the button within 5 seconds.")
        if wait:
            time.sleep(5)
            if self.__get_learn_status() != 0:
                # error
                return False
            else:
                return True
示例#24
0
def led_off(no):
    """
    关闭灯。

    :param int no: 控制电机编号,可以使用 ``MOTOR_1``, ``MOTOR_2``,或者直接写入电机编号。
    """
    attempts = 0
    while True:
        try:
            i2c.writeto(0x10, bytearray([no, 0]))
        except Exception as e:
            attempts = attempts + 1
            time.sleep_ms(500)
            if attempts > 2:
                break
        else:
            break
示例#25
0
    def compensation(self, mode, val):
        """ 
        机器人行走参数补偿

            制造上的误差(如齿轮箱,轮子直径、轴距等),会影响行走的精确性。
            本函数提供补偿功能。
        
        :param mode: 1: 移动补偿,步进一步补偿值 2:旋转补偿,旋转一度的补偿值。
        :param val: 补偿值,浮点数,可为正负值。
        """
        if mode == 1:
            tmp = ustruct.pack('<bf', 0xF6, val)
            print(tmp)
            i2c.writeto(17, tmp)
        elif mode == 2:
            tmp = ustruct.pack('<bf', 0xF7, val)
            i2c.writeto(17, tmp)
示例#26
0
def led_on(no, brightness=50):
    """
    打开灯。电机接口,除了可以驱动电机,还能做灯的控制。

    :param int no: 控制电机编号,可以使用 ``MOTOR_1``, ``MOTOR_2``,或者直接写入电机编号。
    :param int brightness: 设置亮度,范围0~100
    """
    brightness = max(min(brightness, 100), 0)
    attempts = 0
    while True:
        try:
            i2c.writeto(0x10, bytearray([no, brightness]))
        except Exception as e:
            attempts = attempts + 1
            time.sleep_ms(500)
            if attempts > 2:
                break
        else:
            break
示例#27
0
 def auto_detect(self, enable):
     """ 
     开启/禁止内置防跌落功能, 开启本功能后,探测到机器人悬空后,机器人停止行走。
     
     :param enable: True 开启 False 关闭
     """
     if enable:
         i2c.writeto(17, b'\x8E')  #打开防跌红外发射管
         i2c.writeto(17, b'\x90')
     else:
         i2c.writeto(17, b'\x8F')  #关闭防跌红外发射管
         i2c.writeto(17, b'\x91')
示例#28
0
def set_speed(motor_no, speed):
    """
    设置电机速度

    :param int motor_no: 控制电机编号,可以使用 ``MOTOR_1``, ``MOTOR_2`` ,或者直接写入电机编号。
    :param int speed: 电机速度,范围-100~100,负值代表反转。

    """
    global _speed_buf
    speed = max(min(speed, 100), -100)
    _speed_buf.update({motor_no: speed})
    attempts = 0
    while True:
        try:
            i2c.writeto(0x10, bytearray([motor_no, speed]))
        except Exception as e:
            attempts = attempts + 1
            time.sleep_ms(500)
            if attempts > 2:
                break
        else:
            break
示例#29
0
    def _power_save(self, mode=3):
        """ 
        stm32及外设进入省电模式,默认进省电模式3(待机模式)

        :param mode: 1: sleep模式 2:stop模式 3:standby模式
        """
        if mode == 1:
            i2c.writeto(17, b'\x81')
        elif mode == 2:
            i2c.writeto(17, b'\x82')
        elif mode == 3:
            i2c.writeto(17, b'\x83')
示例#30
0
def stopall():
    global i2c
    i2c.writeto(0x10, bytearray([0, 0, 0]))
    i2c.writeto(0x10, bytearray([2, 0, 0]))