Exemplo n.º 1
0
def car_spd_mode_apiece(left_speed=__DEFAULT_SPEED,
                        right_speed=__DEFAULT_SPEED,
                        run_time=0,
                        accel_time=1,
                        decel_time=1,
                        device_index=1):
    if not isinstance(left_speed, (int, float)):
        return
    if not isinstance(right_speed, (int, float)):
        return
    if not isinstance(run_time, (int)):
        return
    if not isinstance(accel_time, (int)):
        return
    if not isinstance(decel_time, (int)):
        return
    left_speed = num_range_scale(left_speed, -__MAX_SPEED, __MAX_SPEED)
    right_speed = num_range_scale(right_speed, -__MAX_SPEED, __MAX_SPEED)
    run_time = num_range_scale(run_time, 0, __MAX_RUN_TIME)
    accel_time = num_range_scale(accel_time, 0, __MAX_ACCEL_TIME)
    decel_time = num_range_scale(decel_time, 0, __MAX_DECEL_TIME)
    neurons_request(
        "m_starter_shield", "car_spd_mode_apiece",
        (left_speed, right_speed, run_time, accel_time, decel_time),
        device_index)
    time.sleep(run_time * __MS_TO_SECOND_FACTOR)
Exemplo n.º 2
0
def straight(distance=50, speed=__DEFAULT_SPEED):
    if not isinstance(distance, (int, float)):
        return
    if not isinstance(speed, (int, float)):
        return
    distance = num_range_scale(distance, -__MAX_DISTANCE, __MAX_DISTANCE)
    wheel_angle = distance * __DISTANCE_TO_ANGLE_FOCTOR
    # distance = int(distance * __CM_TO_MM_FACTOR)
    speed = num_range_scale(speed, -__MAX_SPEED, __MAX_SPEED)
    speed = math.fabs(speed)
    speed = speed * __SPEED_RPM_TO_DPS_FACTOR
    if math.fabs(wheel_angle) <= (speed * speed / __DEFAULT_ACCELERATION):
        accel_time = math.sqrt(math.fabs(wheel_angle / __DEFAULT_ACCELERATION))
        run_time = 2 * accel_time
    else:
        accel_time = speed / __DEFAULT_ACCELERATION
        run_time = math.fabs(wheel_angle) / speed + accel_time
    wheel_angle = int(wheel_angle)
    # run_time = int(run_time * __SECOND_TO_MS_FACTOR)
    # accel_time = int(accel_time * __SECOND_TO_MS_FACTOR)
    run_time = int(round(run_time * __SECOND_TO_MS_FACTOR / 100) * 100)
    accel_time = int(round(accel_time * __SECOND_TO_MS_FACTOR / 100) * 100)
    if accel_time <= 100:
        accel_time = 100
    if run_time <= 2 * accel_time:
        run_time = 2 * accel_time + 100
    # cyberpi.console.println("angle=%s"%(wheel_angle))
    # cyberpi.console.println("speed=%s"%(speed))
    # cyberpi.console.println("r_t=%s"%(run_time))
    # cyberpi.console.println("a_t=%s"%(accel_time))
    starter_shield.car_pos_mode_straight(wheel_angle, run_time, accel_time)
Exemplo n.º 3
0
def led_strip_set_single(strip_index,
                         led_index,
                         red_value,
                         green_value,
                         blue_value,
                         device_index=1):
    if not isinstance(strip_index, (int, float)):
        return
    if not isinstance(led_index, (int, float)):
        return
    if not isinstance(red_value, (int, float)):
        return
    if not isinstance(green_value, (int, float)):
        return
    if not isinstance(blue_value, (int, float)):
        return
    if not isinstance(device_index, (int, float)):
        return
    strip_index = num_range_scale(strip_index, 0, 127)
    led_index = num_range_scale(led_index, 0, 127)
    red_value = num_range_scale(red_value, 0, 255)
    green_value = num_range_scale(green_value, 0, 255)
    blue_value = num_range_scale(blue_value, 0, 255)
    neurons_request(
        "m_standard_shield", "led_strip_set_single",
        (strip_index, led_index, red_value, green_value, blue_value),
        device_index)
Exemplo n.º 4
0
def play_note(note, beat=None, index=1):
    if not isinstance(index, (int, float)):
        return

    if isinstance(note, (int, float)):
        note = num_range_scale(note, 48, 72)

        freq = MIDI_NOTE_NUM0 * math.pow(NOTE_FREQUENCE_RATIO, note)

    elif isinstance(note, str):
        if note in node_table:
            freq = node_table[note]
        else:
            return

    neurons_request("m_speaker", "stop", (), index)

    if beat == None:
        neurons_request("m_speaker", "play_frequency", freq, index)

    elif isinstance(beat, (int, float)):
        beat = num_range_scale(beat, 0)
        if beat <= 0:
            return

        neurons_request("m_speaker", "play_frequency", freq, index)
        time.sleep(beat)
        neurons_request("m_speaker", "stop", (), index)
Exemplo n.º 5
0
def set_led_rgb(r, g, b, index=1):  #设置RGB灯
    if (not isinstance(r, (int, float))) or (not isinstance(
            g,
        (int, float))) or (not isinstance(b, (int, float))) or (not isinstance(
            index, (int, float))) or (index <= 0):
        return
    r = num_range_scale(r, 0, 255)
    g = num_range_scale(g, 0, 255)
    b = num_range_scale(b, 0, 255)
    neurons_request("m_smart_camera", "set_led_rgb", (r, g, b), index)
Exemplo n.º 6
0
def set_both_led_bri(bri_1,
                     bri_2=0,
                     bri_3=0,
                     bri_4=0,
                     bri_5=0,
                     bri_6=0,
                     bri_7=0,
                     bri_8=0,
                     index=1):
    if not isinstance(index, (int, float)):
        return
    if isinstance(bri_1, list):
        if len(bri_1) < 8:
            bri_1.extend([0] * (8 - len(bri_1)))
        elif len(bri_1) > 8:
            bri_1 = bri_1[0:8]
        for i in range(8):
            bri_1[i] = num_range_scale(bri_1[i], 0, 100)
        neurons_request("m_led_ultrasonic_sensor", "set_led_bri", bri_1, index)
    else:
        bri_1 = num_range_scale(bri_1, 0, 100)
        bri_2 = num_range_scale(bri_2, 0, 100)
        bri_3 = num_range_scale(bri_3, 0, 100)
        bri_4 = num_range_scale(bri_4, 0, 100)
        bri_5 = num_range_scale(bri_5, 0, 100)
        bri_6 = num_range_scale(bri_6, 0, 100)
        bri_7 = num_range_scale(bri_7, 0, 100)
        bri_8 = num_range_scale(bri_8, 0, 100)
        neurons_request(
            "m_led_ultrasonic_sensor", "set_led_bri",
            (bri_1, bri_2, bri_3, bri_4, bri_5, bri_6, bri_7, bri_8), index)
Exemplo n.º 7
0
def encoder_motor_lock_power(motor_index=1, lock_power=30, device_index=1):
    if not isinstance(motor_index, (int, float)):
        return
    if not isinstance(device_index, (int, float)):
        return
    if not isinstance(lock_power, (int, float)):
        return
    motor_index = num_range_scale(motor_index, 0, 127)
    lock_power = num_range_scale(lock_power, 0, 127)
    neurons_request("m_starter_shield", "encoder_motor_lock_power",
                    (motor_index, lock_power), device_index)
Exemplo n.º 8
0
def encoder_motor_lock(motor_index=1, lock_state=0, device_index=1):
    if not isinstance(motor_index, (int, float)):
        return
    if not isinstance(device_index, (int, float)):
        return
    if not isinstance(lock_state, (int, float)):
        return
    motor_index = num_range_scale(motor_index, 0, 127)
    lock_state = num_range_scale(lock_state, 0, 1)
    neurons_request("m_starter_shield", "encoder_motor_lock",
                    (motor_index, lock_state), device_index)
Exemplo n.º 9
0
def encoder_motor_set_speed(motor_index, speed, device_index=1):
    if not isinstance(motor_index, (int, float)):
        return
    if not isinstance(speed, (int, float)):
        return
    if not isinstance(device_index, (int, float)):
        return
    motor_index = num_range_scale(motor_index, 0, 127)
    speed = num_range_scale(speed, -__MAX_SPEED, __MAX_SPEED)
    neurons_request("m_starter_shield", "encoder_motor_set_speed",
                    (motor_index, speed), device_index)
Exemplo n.º 10
0
def servo_set_angle_both(angle_1, angle_2, device_index=1):
    if not isinstance(device_index, (int, float)):
        return
    if not isinstance(angle_1, (int, float)):
        return
    if not isinstance(angle_2, (int, float)):
        return
    angle_1 = num_range_scale(angle_1, 0, 180)
    angle_2 = num_range_scale(angle_2, 0, 180)
    neurons_request("m_standard_shield", "servo_set_angle_both",
                    (angle_1, angle_2), device_index)
Exemplo n.º 11
0
def servo_set_pulse_width(servo_index, pulse_width, device_index=1):
    if not isinstance(servo_index, (int, float)):
        return
    if not isinstance(device_index, (int, float)):
        return
    if not isinstance(pulse_width, (int, float)):
        return
    servo_index = num_range_scale(servo_index, 0, 127)
    pulse_width = num_range_scale(pulse_width, 0, 16383)
    neurons_request("m_standard_shield", "servo_set_pulse_width",
                    (servo_index, pulse_width), device_index)
Exemplo n.º 12
0
def dc_motor_set_power_both(power_1, power_2, device_index=1):
    if not isinstance(power_1, (int, float)):
        return
    if not isinstance(power_2, (int, float)):
        return
    if not isinstance(device_index, (int, float)):
        return
    power_1 = num_range_scale(power_1, -100, 100)
    power_2 = num_range_scale(power_2, -100, 100)
    neurons_request("m_standard_shield", "dc_motor_set_power_both",
                    (power_1, power_2), device_index)
Exemplo n.º 13
0
def led_strip_change_brightness(strip_index=1, brightness=30, device_index=1):
    if not isinstance(strip_index, (int, float)):
        return
    if not isinstance(device_index, (int, float)):
        return
    if not isinstance(brightness, (int, float)):
        return
    strip_index = num_range_scale(strip_index, 0, 127)
    brightness = num_range_scale(brightness, -100, 100)
    neurons_request("m_standard_shield", "led_strip_change_brightness",
                    (strip_index, brightness), device_index)
Exemplo n.º 14
0
def multifunction_digital_write(port, value, device_index=1):
    if not isinstance(port, (int, float)):
        return
    if not isinstance(device_index, (int, float)):
        return
    if not isinstance(value, (int, float)):
        return
    port = num_range_scale(port, 0, 127)
    value = num_range_scale(value, 0, 1)
    neurons_request("m_standard_shield", "multifunction_digital_write",
                    (port, value), device_index)
Exemplo n.º 15
0
def servo_set_angle(servo_index, angle, device_index=1):
    if not isinstance(servo_index, (int, float)):
        return
    if not isinstance(device_index, (int, float)):
        return
    if not isinstance(angle, (int, float)):
        return
    servo_index = num_range_scale(servo_index, 0, 127)
    angle = num_range_scale(angle, 0, 180)
    neurons_request("m_standard_shield", "servo_set_angle",
                    (servo_index, angle), device_index)
Exemplo n.º 16
0
def dc_motor_set_power(motor_index, power, device_index=1):
    if not isinstance(motor_index, (int, float)):
        return
    if not isinstance(power, (int, float)):
        return
    if not isinstance(device_index, (int, float)):
        return
    motor_index = num_range_scale(motor_index, 0, 127)
    power = num_range_scale(power, -100, 100)
    neurons_request("m_standard_shield", "dc_motor_set_power",
                    (motor_index, power), device_index)
Exemplo n.º 17
0
def move_to_with_torque(position, speed, strength, index=1):
    if (not isinstance(position, (int))) or (not isinstance(
            speed,
        (int, ))) or (not isinstance(strength, (int, ))) or (not isinstance(
            index, (int))) or (index <= 0):
        return
    if index == "all":
        index = 0xff

    speed = num_range_scale(speed, 1, 50)
    strength = num_range_scale(strength, 1, 255)
    neurons_request("m_smartservo", "move_to_with_torque",
                    [position, speed, strength], index)
Exemplo n.º 18
0
def multifunction_pwm_set(port, duty, frequency, device_index=1):
    if not isinstance(port, (int, float)):
        return
    if not isinstance(device_index, (int, float)):
        return
    if not isinstance(duty, (int, float)):
        return
    if not isinstance(frequency, (int, float)):
        return
    port = num_range_scale(port, 0, 127)
    duty = num_range_scale(duty, 0, 100)
    frequency = num_range_scale(frequency, 0, 2000)
    neurons_request("m_standard_shield", "multifunction_pwm_set",
                    (port, duty, frequency), device_index)
Exemplo n.º 19
0
def set_fills_learn_mode(fills_id, t, index=1):  #设置学习色块模式
    if (not isinstance(fills_id, (int, float))) or (not isinstance(
            t,
        (int, float))) or (not isinstance(index,
                                          (int, float))) or (index <= 0):
        return

    # real index is 0 ~ 7
    fills_id = num_range_scale(fills_id, 1, 8)
    fills_id -= 1

    t = num_range_scale(t, 0, None)
    neurons_request("m_smart_camera", "set_fills_learn_mode", (fills_id, t),
                    index)
Exemplo n.º 20
0
def set_blue(led_index, value, index=1):
    if not isinstance(index, (int, float)):
        return
    if not (isinstance(led_index, (int, float)) or (led_index == "all")):
        return
    if not isinstance(value, (int, float)):
        return

    if led_index == "all":
        led_index = 0
    else:
        led_index = num_range_scale(led_index, 1, 127)

    value = num_range_scale(value, 0, 255)
    neurons_request("m_rgb_led_ring", "set_blue", (led_index, value), index)
Exemplo n.º 21
0
def set_report_mode(mode, timestamp, index=1):
    timestamp = num_range_scale(timestamp, 10, None)
    if mode == 0x00 or mode == 0x01 or mode == 0x02:
        neurons_request("m_soil_moisture", "set_report_mode",
                        (mode, timestamp), index)
    else:
        pass
Exemplo n.º 22
0
def get_fills_diff_speed(sign, tar_x, tar_y, index=1):
    global __kp, __mode
    if (not isinstance(tar_x, (int, float))) or (not isinstance(
            tar_y,
        (int, float))) or (not isinstance(index,
                                          (int, float))) or (index <= 0):
        return 0

    index = int(index)

    if __mode[index - 1] != "color":
        return 0

    data = get_fills_all_data(sign, index)
    if data == 0:
        return 0
    cur_x = data[1]
    cur_y = data[2]

    if cur_x == 0 or cur_y == 0:
        return 0

    error = 0
    if tar_x == -1:
        error = (tar_y - cur_y) * 2
    elif tar_y == -1:
        error = (cur_x - tar_x) * 1
    else:
        return 0

    error = num_range_scale(error, -100, 100)
    error = error * __kp[index - 1]
    return error
Exemplo n.º 23
0
def turn(angle=360, speed=__DEFAULT_SPEED):
    if not isinstance(angle, (int, float)):
        return
    if not isinstance(speed, (int, float)):
        return
    wheel_angle = angle * __ANGLE_CAR_TO_WHEEL_FACTOR
    speed = num_range_scale(speed, -__MAX_SPEED, __MAX_SPEED)
    speed = math.fabs(speed)
    speed = speed * __SPEED_RPM_TO_DPS_FACTOR
    if math.fabs(wheel_angle) <= (speed * speed / __DEFAULT_ACCELERATION):
        accel_time = math.sqrt(math.fabs(wheel_angle / __DEFAULT_ACCELERATION))
        run_time = 2 * accel_time
    else:
        accel_time = speed / __DEFAULT_ACCELERATION
        run_time = math.fabs(wheel_angle) / speed + accel_time
    wheel_angle = int(wheel_angle)
    # run_time = int(run_time * __SECOND_TO_MS_FACTOR)
    # accel_time = int(accel_time * __SECOND_TO_MS_FACTOR)
    run_time = int(round(run_time * __SECOND_TO_MS_FACTOR / 100) * 100)
    accel_time = int(round(accel_time * __SECOND_TO_MS_FACTOR / 100) * 100)
    if accel_time <= 100:
        accel_time = 100
    if run_time <= 2 * accel_time:
        run_time = 2 * accel_time + 100
    # cyberpi.console.println("angle=%s"%(wheel_angle))
    # cyberpi.console.println("speed=%s"%(speed))
    # cyberpi.console.println("r_t=%s"%(run_time))
    # cyberpi.console.println("a_t=%s"%(accel_time))
    starter_shield.car_pos_mode_wheel_clockwise(wheel_angle, run_time,
                                                accel_time)
Exemplo n.º 24
0
def set_camera_sensitometry(level, index=1):  #设置摄像头曝光度
    if (not isinstance(level, (int, float))) or (not isinstance(
            index, (int, float))) or (index <= 0):
        return
    level = num_range_scale(level, 0, 255)
    neurons_request("m_smart_camera", "set_camera_sensitometry", (level),
                    index)
Exemplo n.º 25
0
def set_default_turning_angle(angle, index=1):  #设置默认的转向角度(不开放)
    if (not isinstance(angle, (int, float))) or (not isinstance(
            index, (int, float))) or (index <= 0):
        return
    angle = num_range_scale(angle, -180, 180)
    neurons_request("m_smart_camera", "set_default_turning_angle", (angle),
                    index)
Exemplo n.º 26
0
def get_follow_vector_diff_speed(index=1):
    global __mode
    if (not isinstance(index, (int, float))) or (index <= 0):
        return 0

    index = int(index)

    if __mode[index - 1] != "line":
        return 0

    data = get_line_all_data(index)
    if data == 0:
        return 0
    cur_x0 = data[0]
    cur_y0 = data[1]
    cur_x1 = data[2]
    cur_y1 = data[3]
    # print("end_x start_x:", cur_x0, cur_x1)
    if cur_x0 == 0 or cur_x1 == 0:
        return 0

    error = (cur_x0 - CAMERA_FRAME_WIDTH / 2) * 0.7 + (
        cur_x1 - CAMERA_FRAME_WIDTH / 2) * 0.3
    error = 3 * error
    error = num_range_scale(error, -100, 100)
    error = error * __kp[index - 1]
    return error
Exemplo n.º 27
0
def set_all(red_value, green_value, blue_value, index=1):
    if not isinstance(index, (int, float)):
        return

    if not isinstance(red_value, (int, float)):
        return
    if not isinstance(green_value, (int, float)):
        return
    if not isinstance(blue_value, (int, float)):
        return

    red_value = num_range_scale(red_value, 0, 255)
    green_value = num_range_scale(green_value, 0, 255)
    blue_value = num_range_scale(blue_value, 0, 255)
    neurons_request("m_rgb_led_ring", "set_rgb",
                    (0x00, red_value, green_value, blue_value), index)
Exemplo n.º 28
0
def __set_r_g_b(col_id, value, index):
    global __led_value

    value = num_range_scale(value, 0, 255)
    __led_value[index][col_id] = value

    neurons_request("m_rgb", r_g_b_list[col_id], value, index)
Exemplo n.º 29
0
def set_report_mode(mode, timestamp, index=1):
    timestamp = num_range_scale(timestamp, 10, None)
    if mode == 0x00 or mode == 0x01 or mode == 0x02:
        neurons_request("m_ultrasonic_sensor", "set_report_mode",
                        (mode, timestamp), index)
    else:
        pass
Exemplo n.º 30
0
def get_barcode_diff_speed(label, tar_x, tar_y, index=1):
    global __kp, __mode
    if (not isinstance(tar_x, (int, float))) or (not isinstance(
            tar_y,
        (int, float))) or (not isinstance(index,
                                          (int, float))) or (index <= 0):
        return 0

    index = int(index)

    if __mode[index - 1] != "line":
        return 0

    data = get_barcode_all_data(label, index)
    if data == 0:
        return 0
    cur_x = data[1]
    cur_y = data[2]
    # print("cur x y:", cur_x, cur_y)
    if cur_x == 0 or cur_y == 0:
        return 0

    error = 0
    if tar_x == -1:
        error = (tar_y - cur_y) * 3
    elif tar_y == -1:
        error = (cur_x - tar_x) * 3
    else:
        return 0

    error = num_range_scale(error, -100, 100)
    error = error * __kp[index - 1]
    return error