
AI Vision Sensor Object tracking code. (IQ)
What does it do?
Robot drives to an object and when close enough, it stops moving.
Config:
-Set up drivebase.
-Config AI Vision sensor and set up any object you want to follow. (Its called "Tracker" in this example)
How does it work?
Robot Spins while vision sensor looks for any objects and if any are detected, it will go into drive mode. In drive mode, the robot will drive towards the object while attempting to rotate the drivebase to keep the object centred in its view. This is not proportional due to the drive base limitations on VexCode. The robot just checks whether the object is within a column of pixels sensed by the Vision sensor and attempts to rotate the robot to get the object centred it this column. The values for the column are 120-200 in this example. This is in pixels with 160 being in the centre of the screen on the x axis. So, it is ±40 pixels either side. If this value is to small, at longer distances, the robot will oscillate but if it is too large, at close distances, it wont be alined properly. This is a limitation of non-proportional control loops and why PID would in theory be better for this application.
The "broadcast" blocks are just displaying what the robot is doing.
Feel free to change values and modify the code to find any better ways of doing this!
If you need any help, feel free to ask!
Python Version:
def when_started1():
global myVariable, error, turn, my_event, r, Search, Arrived, Driving, ai_vision_5_objects, screen_precision, console_precision, ai_vision_5_index
drivetrain.set_turn_velocity(25, PERCENT)
while True:
ai_vision_5_objects = ai_vision_5.take_snapshot(ai_vision_5__Tracker)
if ai_vision_5_objects and len(ai_vision_5_objects) > 0:
if ai_vision_5_objects[ai_vision_5_index].centerX < 120:
Driving.broadcast()
drivetrain.turn(LEFT)
if ai_vision_5_objects[ai_vision_5_index].centerX > 200:
Driving.broadcast()
drivetrain.turn(RIGHT)
if ai_vision_5_objects[ai_vision_5_index].centerX > 120 and ai_vision_5_objects[ai_vision_5_index].centerX < 200:
if ai_vision_5_objects[ai_vision_5_index].width < 80:
Driving.broadcast()
drivetrain.drive(FORWARD)
else:
Arrived.broadcast()
drivetrain.stop()
else:
drivetrain.set_turn_velocity(30, PERCENT)
Search.broadcast()
drivetrain.turn(RIGHT)
wait(20, MSEC)
def Driving_callback_0():
global myVariable, error, turn, my_event, r, Search, Arrived, Driving, ai_vision_5_objects, screen_precision, console_precision, ai_vision_5_index
brain.screen.set_cursor(1, 1)
brain.screen.clear_screen()
brain.screen.print("Driving...")
wait(0.1, SECONDS)
brain.screen.clear_screen()
def Search_callback_0():
global myVariable, error, turn, my_event, r, Search, Arrived, Driving, ai_vision_5_objects, screen_precision, console_precision, ai_vision_5_index
brain.screen.set_cursor(1, 1)
brain.screen.clear_screen()
brain.screen.print("Searching...")
wait(0.1, SECONDS)
brain.screen.clear_screen()
def Arrived_callback_0():
global myVariable, error, turn, my_event, r, Search, Arrived, Driving, ai_vision_5_objects, screen_precision, console_precision, ai_vision_5_index
brain.screen.set_cursor(1, 1)
brain.screen.clear_screen()
brain.screen.print("Arrived!")
wait(0.1, SECONDS)
brain.screen.clear_screen()