Beargineers logo

#27628

Beargineers

Simulator

Run the opmodes on a laptop, on a simulated robot, from a Driver Station in the browser, with gamepads, the field view, and the season's balls.

Running it

The robot’s code can run on a laptop, on a simulated robot: before the robot is built, between practices, or to try a change before it goes to the robot.

./gradlew :TeamCode:simulate

In Android Studio, it is in the Gradle tool window, under TeamCode › Tasks › robot › simulate. It builds the module, then opens a Driver Station in the browser at http://localhost:8080/bear/driverstation. It runs until you stop it: Ctrl+C in a terminal, or the stop button in Android Studio.

The opmodes run their own code, unchanged. Every device the robot’s code asks for is there, so the robot and its parts work as on the Control Hub: config, choices in INIT, actions, the path follower, logs.

The Driver Station page

Pick an opmode, then press INIT, START and STOP, as on the Driver Station. Next to the buttons are the Driver Station’s lines and the field view, with the robot on the field.

  • An autonomous stops after 30 seconds, and the TeleOp its preselectTeleOp names is picked next.
  • The robot stays on the field from one opmode to the next, as a real one does: TeleOp starts where autonomous ended. Until it moves, it stands wherever the opmode’s code says it is, like robot.localizer.pose = start, as if the drivers had put it down there.
  • An app with several robots (see Robots and parts) has a list of robot configurations too. With only one, that one is picked.

The graphs page and the settings page are there too, as on the robot: commands start from their buttons, and settings changed on the settings page apply right away. Those changes, and the choices the opmodes remember, are kept in the module’s build/simulator folder, until the project’s config.properties changes.

Gamepads

Gamepads plugged into the laptop are gamepads 1 and 2, in the order they show up: press a button on one with the page open. A Logitech F310 needs the switch underneath on D: on X, a Mac doesn’t see it at all. Switch it back to X for the Driver Hub.

The keyboard is gamepad 1 too, while the page has the focus:

Keys Gamepad
W A S D left stick
Q E right stick left and right: turning
arrow keys dpad, like the INIT menu’s
I J K L Y, X, A, B, laid out as on a gamepad
U O left and right bumper
Backspace, Enter back, start

When the page stops sending, like when its tab is closed, the gamepads let go within a second.

One opmode at a time

./gradlew :TeamCode:simulate --opmode="Auto"

runs one opmode and ends: INIT, START a second later, and STOP after 30 seconds for an autonomous, or when you stop it for a TeleOp. The Driver Station’s lines go to the output every second, with the log. --seconds=10 changes how long it runs, --robot=betaDecode picks the robot configuration, and --port=8090 where the pages are.

What is simulated

Part In the simulator
Mecanum drive drives by the powers its wheels get, up to a top speed, from the power it takes to start moving, with a lag before it reaches a new speed. The walls stop it.
Pinpoint reports where the robot is, exactly
Motors turn at the speed their power gives, or that the hub holds them at, and their encoders count. A flywheel spins up and coasts down.
Servos turn to their position at a servo’s speed; a servo axis’s encoder follows its servo
Battery steady at sim.battery
Sensors distance and color sensors see the balls, where the robot’s simulation puts them (see Balls); others see nothing, and switches aren’t pressed
Balls with the season’s field: see Balls

Not yet: cameras, other robots, and the Pinpoint’s lag and noise.

Making it move like your robot

These settings, in the robot’s config, tell the simulator how the robot moves. Only the simulator reads them; on the robot, they are no problem. The defaults are Beta’s, a 12 kg mecanum robot on 435 rpm motors.

Setting Default What
sim.topSpeed, topStrafeSpeed, topTurnSpeed 150cm/s, 125cm/s, 330deg/s forward, sideways, and turning, at full power on 12 V
sim.frictionPower, frictionStrafePower, frictionTurnPower 0.08, 0.15, 0.07 the power it takes to start moving each way
sim.lag 150ms how long the robot takes to get two thirds of the way to a new speed
sim.battery 12.5 the battery’s voltage
sim.motorSpeed 2800 encoder ticks per second at full power: right for goBILDA and REV HD Hex motors, whatever their gearbox
sim.motorLag, flywheelLag 100ms, 300ms how long motors, and a flywheel’s, take to change speed
sim.servoTravel 600ms how long a servo takes from position 0 to 1

The Tune opmode’s characterizations measure the first ones on your robot: the top speed forward, sideways and turning, and the power that starts it moving each way. The walls stop the robot as big as robotSize.length and robotSize.width say, 18 inches each by default; the field view draws it that big too.

To run the module’s code on a laptop, the plugin sets the module’s unit tests to let Android’s own methods do nothing, which the Android Gradle plugin calls unitTests.returnDefaultValues.

Balls

With the season’s field in the simulator, the field view has its balls and field elements on it: they roll, fly and bounce, the robot pushes them, its intakes take them in and its shooters shoot them, and field elements like the HIVE catch them and tip. The Driver Station page shows what the season says about the field, like the score, and Reset field puts everything back as at the start of a match, with the robot’s preloads in it.

Each shot says where it went, in the output and on the Driver Station page: into a HIVE cell, against its walls, or onto the tiles, and by how much it missed the opening it passed, like pollen shooter: pollen landed at (-46, 30), 9 cm under the red audience cell's opening.

What the field looks like, and what the robot’s mechanisms do with balls, is code only the simulator runs. It goes in the module’s src/sim/kotlin folder, which the plugin builds with the unit tests: it stays out of the robot’s app, and the simulator finds it there.

The season’s field is an object of SimulatedSeason, setting up what is on the field at the start of a match. Positions are on the season’s field, in its coordinates:

object Biobuzz : SimulatedSeason(BiobuzzField) {
    val pollen = BallKind("pollen", 7.1.cm, grams = 25.0, color = "#fcc419")

    override fun Arena.setUp() {
        // A GARDEN's 4 pollen, along the audience wall
        repeat(4) { i -> ball(pollen, Point((-179.3).cm, (179.3 - 7.1 * i).cm)) }
        // A FLOWER: its 4 pollen stacked, the bottom one there for an intake to take
        stack(Point((-61).cm, 175.9.cm), 6.cm, List(4) { pollen })
    }
}
In Arena.setUp What
ball a ball on the tiles, or in the air
obstacle a circle robots and balls bump into, like a FLOWER
wall a straight wall robots and balls bump into, like the HIVE’s frame from leg to leg
stack balls stacked over a spot, like a FLOWER’s: an intake takes the bottom one, and the next drops
container holds balls, like a HIVE cell: balls get in through its Opening, and it tells the season. With the opening’s depth, it is a box behind the opening, and balls that miss bounce off it
after does something later, like the HIVE tipping over
draw, drawing shapes on the field view, as they are at the moment, like which HIVE cell is up
status what the Driver Station page says about the field, like the score

What stays put all match, like the zones and the field elements’ footprints, goes in the field’s layout instead: the field view draws it on the robot too. The shapes are the same, FieldShapes from nl.beargineers.platform.geometry.

The robot’s mechanisms are an object of RobotSimulation for its class, described from its parts. Points and angles are the robot’s: from its center, x forward and y to its left. Its body, as big as robotSize says, pushes balls out of the way:

object A2Simulation : RobotSimulation<A2Robot>(A2Robot::class.java) {
    override fun Mechanisms.describe(robot: A2Robot) {
        val table = turntable("table", Point((-0.7).cm, 7.4.cm), radius = 11.cm, pockets = 4, height = 6.1.cm) {
            angleOf(robot.table)
        }
        val pollen = shooter("pollen shooter", robot.shooters.pollen, at = Point(7.6.cm, (-20).cm), height = 22.cm,
            facing = (-90).degrees, elevation = 60.degrees, launches = 3500.rpm to 6.3.meters.perSecond)
        table.exit(285.degrees, pollen) { it.kind == Biobuzz.pollen }
        intake("front intake", robot.frontIntake, mouth = Point(22.65.cm, (-15.85).cm) to Point(22.65.cm, 15.85.cm)) {
            table.emptyFacing(0.degrees)
        }
        distanceSensor("frontBall", at = Point(17.3.cm, 10.4.cm), facing = 180.degrees)
        preload(Biobuzz.pollen, table.pockets)
    }
}
In Mechanisms.describe What
intake takes in balls at its mouth, along the robot’s side, while its motor’s power is positive; holds one until the place it goes to is free, and pushes it back out running backward
place a spot in the robot that holds one ball
turntable pockets on a turning table, like a sorter or a spindexer, at the angle its servo really is
turntable.exit where balls leave the table turning counterclockwise, for a shooter, if the ball and the mechanism let them
shooter launches balls at a speed in proportion to its flywheel’s, each shot slowing the flywheel
distanceSensor a distance or color sensor of the robot configuration, seeing the nearest ball in its line of sight
preload balls in the robot at the start of a match
drawing shapes drawn on the robot, in its frame, as they are at the moment, like a door that is shut
angleOf, positionOf, speedOf where a servo axis or a servo really is, and how fast a flywheel spins

The field view draws the mechanisms on the robot where it really is:

  • a turntable’s pockets as rings, which their balls cover, with a spoke to the first, so you see it turn, and a mark at each exit;
  • an intake’s mouth, green while it takes balls in, red while it pushes them out;
  • a shooter’s barrel, pointing where it shoots: grey while its flywheel stands, orange on its way up to speed, green at speed;
  • a distance sensor’s dot, and a line to the ball it sees.

Anything else goes in a drawing, with the same shapes as the field’s, in the robot’s frame. A2’s door, across the pollen exit while it is shut:

val door = FieldShape.line(Point(9.5.cm, (-7.5).cm), Point((-2.1).cm, (-10.6).cm), color = "#212529", width = 4.0)
drawing { if (doorOpen()) emptyList() else listOf(door) }

On the tiles, a ball slows down as it rolls: from 1 m/s, it rolls about 1.2 m. In the air, gravity and the air slow it. It bounces off the walls, the obstacles, containers’ walls, the robot and other balls. Not simulated: balls bouncing off other parts of field elements, like the HIVE’s frame above its legs; jams; balls flying over the walls.