Я сделал функцию, которая определяет и инициализирует датчики на моем ev3.Код работает, но я беспокоюсь о предупреждении.Какой правильный код я должен использовать?
Я довольно нуб в Python, но, похоже, это работает, и я не пробовал ничего другого, так как я не знаю, что такое правильное решение.
# Sensor declaration
hitechnic_1 = 'null'
hitechnic_2 = 'null'
side_color_sensor = 'null'
colorRear = 'null'
def sensor_declaration():
global hitechnic_1, hitechnic_2, side_color_sensor, colorRear
try:
hitechnic_1 = Sensor('in1:i2c1')
except DeviceNotFound:
print('Sensor 1 not found')
else:
hitechnic_1.mode = 'RGB'
try:
hitechnic_2 = Sensor('in2:i2c1')
except DeviceNotFound:
print('Sensor 2 not found')
else:
hitechnic_2.mode = 'RGB'
try:
side_color_sensor = Sensor('in3:i2c1')
except DeviceNotFound:
print('Sensor 3 not found')
else:
side_color_sensor.mode = 'COLOR'
try:
colorRear = ColorSensor('in4')
except DeviceNotFound:
print('Sensor 4 not found')
# Function declaration --use these as much as possible
sensor_declaration()
# PID Line Follower (1 sensor) --default : Hitechnic sensor in port 1, follows the line on the right side
def pid_line_follower(sensor=hitechnic_1, side=1, speed=40):
global target, error, last_error, integral, derivative, Kp, Ki, Kd,
steer_pair, motor_steering
error = target - (sensor.value(3) / 2)
integral = error + integral
derivative = error - last_error
motor_steering = ((error * Kp) + (integral * Ki) + (derivative * Kd)) *
side
steer_pair.on(motor_steering, -speed)
last_error = error
Когда я использую sensor.value (3) в функции следования строки, она выделяет значение и выдает вышеупомянутую ошибку, но код работает.