SunFounder PiCar-X Kit
(continued from previous page)
px.set_dir_servo_angle(-30)
px.backward(10)
elif last_state == 'right':
px.set_dir_servo_angle(30)
px.backward(10)
while True:
gm_val_list = px.get_grayscale_data()
gm_state = get_status(gm_val_list)
print("outHandle gm_val_list: %s, %s"%(gm_val_list, gm_state))
currentSta = gm_state
if currentSta != last_state:
break
sleep(0.001)
• get_status Function:
It interprets the grayscale sensor data (val_list) to determine the car’s navigation state.
The car’s state can be ‘forward’, ‘left’, ‘right’, or ‘stop’, based on which sensor detects the line.
def get_status(val_list):
_state = px.get_line_status(val_list) # [bool, bool, bool], 0 means␣
˓→line, 1 means background
if _state == [0, 0, 0]:
return 'stop'
elif _state[1] == 1:
return 'forward'
elif _state[0] == 1:
return 'right'
elif _state[2] == 1:
return 'left'
• Main Loop:
The while True loop continuously checks the grayscale data and adjusts the car’s movement accord-
ingly.
Depending on the gm_state, it sets the steering angle and movement direction.
if __name__=='__main__':
try:
while True:
gm_val_list = px.get_grayscale_data()
gm_state = get_status(gm_val_list)
print("gm_val_list: %s, %s"%(gm_val_list, gm_state))
if gm_state != "stop":
last_state = gm_state
if gm_state == 'forward':
px.set_dir_servo_angle(0)
px.forward(px_power)
elif gm_state == 'left':
px.set_dir_servo_angle(offset)
(continues on next page)
4.7. 5. Line Tracking 63