Race Code Instructions example

Here is a quick example of using our Race Code Functions for control over a 10 lap race.

This example is built around the WaitForWaypoint command to decide when changes need to be made.
Finer control can be achieved by using the WaitForDistance or GetDistance commands to make changes based on a distance around the track instead.

The waypoint numbers used are for track 1, other tracks will use different waypoint values.

Track 1 Race.py example:

#!/usr/bin/env python
# coding: Latin-1

### Settings for this race ###
laps = 10

### Start of the race ###
if TrackFound():
	# We can see the track, start by following the lane we are on
	trackLane = round(CurrentTrackPosition())
	AimForLane(trackLane)
else:
	# Cannot see the track, aim for the center instead (stopgap measure)
	AimForLane(0)
# Save a start-line image
photo = GetLatestImage()
SaveImage(photo, 'Start-line.jpg')
# Start logging what happens
StartUserLog()
StartDetailedLoging()
# Wait for the go signal from the start/stop lights.
WaitForGo()
# Go at max speed
Speed(100)

### During the race ###
# Keep going until we have fished all of the laps
while LapCount() < laps:
	# Full speed to the first corner
	Speed(100)
	WaitForWaypoint(2)
	# Slow down, move to the inside and wait for the apex
	Speed(80)
	AimForLane(-2)
	WaitForWaypoint(3)
	# Speed up and move to the center until the S curve starts
	Speed(100)
	AimForLane(0)
	WaitForWaypoint(4)
	# Move towards the outside until the S curve changes
	AimForLane(+1)
	WaitForWaypoint(5)
	# Move towards the inside until the S curve ends
	AimForLane(-1)
	WaitForWaypoint(6)
	# Slow down and move to the inside around the corner
	Speed(70)
	AimForLane(-2)
	WaitForWaypoint(7)
	# Speed up for the back straight along the center
	Speed(100)
	AimForLane(0)
	WaitForWaypoint(8)
	# High speed for the last corner on the inside
	Speed(90)
	AimForLane(-2)
	WaitForWaypoint(9)
	# Full speed until the start/finish line along the outside
	Speed(100)
	AimForLane(+2)
	WaitForWaypoint(1)

### End of the race ###
# Save a finish-line image
photo = GetLatestImage()
SaveImage(photo, 'Finished.jpg')
# Slow the YetiBorg down gradually from 100% to 0%
for slowing in range(99, -1, -1):
	Speed(slowing)
	WaitForSeconds(0.01)
# Stop the logging
EndUserLog()
EndDetailedLog()
# End the race (will stop the robot and end the program)
FinishRace()

Comments

crashmeplease's picture

On line 20;

StartDetailedLog()

should be

StartDetailedLoging()

?

Arron Churchill's picture

Well spotted, that is a mistake in the above example (now corrected).

Thanks for pointing it out :)

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre>
  • Syntax highlight code surrounded by the <pre class="brush: lang">...</pre> tags, where lang is one of the following language brushes: bash, cpp, perl, python.
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.