|
| 1 | +--- |
| 2 | +title: Radio API |
| 3 | +weight: 6 |
| 4 | +--- |
| 5 | + |
| 6 | +Radio transmitters and receivers are attached to various items in the arena. |
| 7 | +Each transmitter encodes their identity, which means that receivers can identify these objects. |
| 8 | + |
| 9 | +Using the signal strength and bearing of the received radio signals, you're able to |
| 10 | +determine the distance and direction of a transmitter relative to the radio in 2D. |
| 11 | + |
| 12 | +Our library provides this through a `radio` attached to your `Robot`, |
| 13 | +which has a `sweep` function that detects transmitters that are in range: |
| 14 | + |
| 15 | +``` python |
| 16 | +# Get all transmitters that are in range. |
| 17 | +transmitters = r.radio.sweep() |
| 18 | +``` |
| 19 | + |
| 20 | +When called, the `sweep()` function uses the radio receiver to scan for all transmitters in range. |
| 21 | +It returns a list of `Target` objects, each of which describes one of the transmitters that were found within range. |
| 22 | +A detailed description of the attributes of `Target` objects is provided at the bottom of this page. |
| 23 | + |
| 24 | +Here's an example that will repeatedly print out the information, bearing, and signal strength of each transmitter in range: |
| 25 | + |
| 26 | +``` python |
| 27 | +# Continuously prints details of nearby transmitters. |
| 28 | +while True: |
| 29 | + transmitters = r.radio.sweep() |
| 30 | + print("I found", len(transmitters), "transmitter(s):") |
| 31 | + |
| 32 | + for tx in transmitters: |
| 33 | + print("Transmitter ID:", tx.target_info.id) |
| 34 | + print(" Type:", tx.target_info.type) |
| 35 | + print(" Owner:", tx.target_info.owner) |
| 36 | + print(" Bearing:", tx.bearing) |
| 37 | + print(" Signal Strength:", tx.signal_strength) |
| 38 | +``` |
| 39 | + |
| 40 | +## Detecting beacons |
| 41 | + |
| 42 | +There are 4 beacons in the arena, see [the rules](/rules/) for their locations. These beacons continually transmit their identity. Beacons can be detected up to 2.5m away. |
| 43 | + |
| 44 | +The beacons have the `target_info.type` value of `0` (`TargetType.BEACON`), which can be used to detect if it is a beacon. See '[Objects of the Radio System](#objects-of-the-radio-system)' below for all available information. |
| 45 | + |
| 46 | +## Detecting containers |
| 47 | + |
| 48 | +There are many containers in the arena, which are each owned by a specific team. Containers can be detected up to 2.5m away. |
| 49 | + |
| 50 | +The beacons have the `target_info.type` value of `1` (`TargetType.CONTAINER`), which can be used to detect if it is a container. See '[Objects of the Radio System](#objects-of-the-radio-system)' below for all available information. |
| 51 | + |
| 52 | + |
| 53 | +## Objects of the Radio System |
| 54 | + |
| 55 | +### Target |
| 56 | + |
| 57 | +A `Target` object contains information about a _detected_ transmitter. |
| 58 | +It has the following attributes: |
| 59 | + |
| 60 | +`target_info` |
| 61 | +: A [`TargetInfo`](#targetinfo) object containing information about the transmitter that was detected. |
| 62 | + |
| 63 | +`signal_strength` |
| 64 | +: The measured strength of the signal as a float. |
| 65 | + |
| 66 | +`bearing` |
| 67 | +: The angle to the `Target` in radians as a float. |
| 68 | + A bearing of `0` is in front of the robot. Positive bearings are to the robot's right. |
| 69 | + |
| 70 | +### TargetInfo |
| 71 | + |
| 72 | +The `TargetInfo` object contains information about a transmitter. |
| 73 | +It has the following attributes: |
| 74 | + |
| 75 | +`id` |
| 76 | +: The ID of the transmitter, either 1-16 for containers, or 100-103 for beacons. See [the rules](/rules/) for the arena layout. |
| 77 | + |
| 78 | +`type` |
| 79 | +: The type of the transmitter, either `0` (`TargetType.BEACON`) for a beacon, or `1` (`TargetType.CONTAINER`) for a container. |
| 80 | + |
| 81 | +`owner` |
| 82 | +: The zone id of the robot that currently owns the stations territory. This can be `0` (`Owner.ZONE_0`), `1` (`Owner.ZONE_1`), or `-1` (`Owner.NULL`). which indicates it does not have an owner (i.e. it's a beacon). Remember, you can find out which zone your robot is in using [r.zone](/api/game-state) |
0 commit comments