Skip to content

Commit 407dac2

Browse files
authored
Updating Tutorials [AARD-2091] (#1286)
2 parents 5a8a471 + a9fa446 commit 407dac2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+629
-211
lines changed

tutorials/APS.md

Lines changed: 0 additions & 59 deletions
This file was deleted.

tutorials/CodeSimulation.md

Lines changed: 176 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ feedback link: https://github.com/Autodesk/synthesis/issues
99

1010
# Code Simulation in Synthesis
1111

12-
## Setup (Robot Code Side)
12+
## Setup (Robot Code)
1313

14-
The Synthesis simulator comes with code simulation already integrated. However, a development environment for what ever code your are trying to simulate will be required.
15-
Synthesis' code simulation relies on the WPILib HALSim extensions, specifically the websocket-client extension. You'll need to make the following changes to your `build.gradle` in order to properly simulate your code in Synthesis.
14+
The Synthesis simulator comes with code simulation already integrated. However, a development environment for the code you are trying to simulate will be required. Synthesis' code simulation relies on the WPILib HALSim extensions—specifically the WebSocket-client extension. You'll need to make the following changes to your `build.gradle` to connect everything properly.
1615

1716
### C++/Java
1817

@@ -24,25 +23,34 @@ You'll need to enable desktop support for your project in order to run the HALSi
2423
def includeDesktopSupport = true
2524
```
2625

27-
#### 2. Websocket Server Extension
26+
#### 2. WebSocket Server Extension
2827

29-
In order to communicate with your browser, you'll need to enable the websocket server extension with the following:
28+
In order to communicate with your browser, you'll need to enable the WebSocket server extension with the following:
3029

3130
```java
3231
wpi.sim.addWebsocketsServer().defaultEnabled = true
3332
```
3433

3534
#### 3. SyntheSim (Optional)
3635

37-
For CAN-based device support (TalonFX, CANSparkMax, most Gyros), you'll need our own library--SyntheSim. Currently only available for Java, SyntheSim adds additional support for third party devices that don't follow WPILib's web socket specification. It's still in early development, so you'll need to clone and install the library locally in order to use it:
36+
For CAN-based device support (TalonFX, CANSparkMax, most Gyros), you'll need our SyntheSim library. Although currently only available for Java, SyntheSim adds additional support for third-party devices that don't follow WPILib's WebSocket specification. It's still in early development, so you'll need to clone and install the library locally in order to use it:
3837

38+
39+
To clone just the SyntheSym portion of the repository (faster than cloning the whole repository), you can use the following commands
40+
```sh
41+
git clone --no-checkout --depth=1 --filter=tree:0 https://github.com/Autodesk/synthesis.git
42+
cd synthesis
43+
git sparse-checkout set --no-cone /simulation
44+
git checkout
45+
```
46+
47+
Once you've cloned Synthesis, you can build and publish the SyntheSym package to your local maven
3948
```sh
40-
$ git clone https://github.com/Autodesk/synthesis.git
41-
$ cd synthesis/simulation/SyntheSimJava
42-
$ ./gradlew build && ./gradlew publishToMavenLocal
49+
cd simulation/SyntheSimJava
50+
./gradlew build && ./gradlew publishToMavenLocal
4351
```
4452

45-
Next, you'll need to have the local maven repository is added to your project by making sure the following is included in your `build.gradle` file:
53+
Next, you'll need to ensure that the local Maven repository is added to your project by verifying that the following is included in your `build.gradle` file:
4654

4755
```java
4856
repositories {
@@ -61,24 +69,25 @@ dependencies {
6169
}
6270
```
6371

64-
All of these instructions can be found in the [SyntheSim README](https://github.com/Autodesk/synthesis/blob/prod/simulation/SyntheSimJava/README.md).
72+
> [!NOTE]
73+
> All of these instructions can be found in the [SyntheSim README](https://github.com/Autodesk/synthesis/blob/prod/simulation/SyntheSimJava/README.md).
6574
6675
SyntheSim is very much a work in progress. If there is a particular device that isn't compatible, feel free to head to our [GitHub](https://github.com/Autodesk/synthesis) to see about contributing.
6776

6877
#### 4. HALSim GUI
6978

70-
This should be added by default, but in case it isn't, add this to your `build.gradle` to enable the SimGUI extension by default.
79+
Verify that the following is included in your `build.gradle`—it should be added by default.
7180

7281
```java
7382
wpi.sim.addGui().defaultEnabled = true
7483
```
7584

76-
This will allow you to change the state of the robot, as well as hook up any joysticks you'd like to use during teleop. You must use this GUI in order
77-
to bring your robot out of disconnected mode, otherwise we won't be able to change the state of your robot from within the app.
85+
This will allow you to change the state of the robot, as well as connect any joysticks you'd like to use during TeleOp. You must use this GUI in order
86+
to bring your robot out of disconnected mode, or else it won't be possible to change the state of your robot from within the app.
7887

7988
#### 5. Start your code
8089

81-
To start your robot code, you can use the following simulate commands with gradle:
90+
To start your robot code, you can use the following simulate commands with Gradle:
8291

8392
```bash
8493
$ ./gradlew simulateJava
@@ -121,51 +130,183 @@ To start your code, you can run the following:
121130
python -m robotpy sim --ws-server
122131
```
123132

124-
## Setup (Synthesis Web-app Side)
133+
## Working with Hardware Components
134+
135+
Once your simulation is running, you can control various robot components just like you would on a physical robot.
136+
137+
### Motors
138+
139+
#### PWM Motors
140+
141+
Standard servo and speed controller motors work right out of the box with WPILib's built-in simulation support.
142+
143+
```java
144+
private Spark m_leftMotor = new Spark(0);
145+
private Spark m_rightMotor = new Spark(1);
146+
147+
public void teleopPeriodic() {
148+
double forward = -m_Controller.getLeftY();
149+
double turn = m_Controller.getRightX();
150+
151+
m_leftMotor.set(forward + turn);
152+
m_rightMotor.set(forward - turn);
153+
}
154+
```
155+
156+
#### CAN Motors
157+
For advanced motors like TalonFX or CANSparkMax, you'll need to have SyntheSim installed.
158+
159+
```java
160+
// Import from SyntheSim, not vendor libraries
161+
import com.autodesk.synthesis.revrobotics.CANSparkMax;
162+
import com.autodesk.synthesis.ctre.TalonFX;
163+
import com.revrobotics.CANSparkLowLevel.MotorType;
164+
165+
private CANSparkMax m_driveLeft = new CANSparkMax(1, MotorType.kBrushless);
166+
private CANSparkMax m_driveRight = new CANSparkMax(2, MotorType.kBrushless);
167+
private TalonFX m_shooter = new TalonFX(7);
168+
169+
public void autonomousPeriodic() {
170+
m_driveLeft.set(0.5);
171+
m_driveRight.set(-0.5);
172+
173+
double position = m_driveLeft.getEncoder().getPosition();
174+
if (position >= 20) {
175+
m_driveLeft.set(0.0);
176+
}
177+
}
178+
```
179+
180+
### Gyroscopes and Accelerometers
181+
182+
#### NavX and AHRS devices
183+
184+
```java
185+
// Regular NavX library is okay
186+
import com.kauailabs.navx.frc.AHRS;
187+
import edu.wpi.first.wpilibj.SPI;
188+
189+
private AHRS m_gyro = new AHRS();
190+
191+
public void autonomousPeriodic() {
192+
double currentAngle = m_gyro.getAngle();
193+
double pitch = m_gyro.getPitch();
194+
double roll = m_gyro.getRoll();
195+
double yaw = m_gyro.getYaw();
196+
197+
double targetAngle = 90.0;
198+
double error = targetAngle - currentAngle;
199+
double turnSpeed = error * 0.02;
200+
201+
m_driveLeft.set(-turnSpeed);
202+
m_driveRight.set(turnSpeed);
203+
}
204+
```
205+
206+
#### Acceleromoter simulation
207+
For detecting impacts, measuring tilt, or monitoring acceleration:
208+
209+
```java
210+
// Import from SyntheSim
211+
import com.autodesk.synthesis.wpilibj.ADXL362;
212+
213+
private ADXL362 m_accelerometer = new ADXL362(SPI.Port.kMXP, ADXL362.Range.k8G);
214+
215+
public void robotPeriodic() {
216+
double xAccel = m_accelerometer.getX();
217+
double yAccel = m_accelerometer.getY();
218+
double zAccel = m_accelerometer.getZ();
219+
220+
if (Math.abs(xAccel) > 0.5 || Math.abs(yAccel) > 0.5) {
221+
System.out.println("Warning: Robot is tilting!");
222+
}
223+
}
224+
```
225+
226+
### Cameras and Vision
227+
228+
**Camera simulation** in Synthesis uses a WebSocket bridge to stream real-time 3D rendered frames from the simulator to your robot code. The example code allows camera stream in Shuffleboard:
229+
230+
```java
231+
import edu.wpi.first.cameraserver.CameraServer;
232+
import edu.wpi.first.cscore.CvSource;
233+
import com.autodesk.synthesis.Camera;
234+
import com.autodesk.synthesis.CameraFrameHandler;
235+
import com.autodesk.synthesis.WebSocketMessageHandler;
236+
import com.autodesk.synthesis.SynthesisWebSocketServer;
237+
238+
private Camera m_Camera = new Camera("USB Camera 0", 0);
239+
private CvSource m_videoSource;
240+
241+
@Override
242+
public void simulationInit() {
243+
System.out.println("🚀 Starting WebSocket server for Synthesis communication...");
244+
SynthesisWebSocketServer.getInstance().startServer();
245+
246+
m_Camera.setConnected(true);
247+
m_Camera.setWidth(640);
248+
m_Camera.setHeight(480);
249+
m_Camera.setFPS(30);
250+
251+
// Create custom video source. WPILib handles all the streaming!
252+
m_videoSource = CameraServer.putVideo("Synthesis Camera", 640, 480);
253+
254+
// Register camera with frame handler to receive frames from simulation
255+
CameraFrameHandler.getInstance().registerCamera("USB Camera 0", m_videoSource);
256+
}
257+
258+
@Override
259+
public void simulationPeriodic() {
260+
boolean cameraConnected = m_Camera.isConnected();
261+
double cameraWidth = m_Camera.getWidth();
262+
double cameraHeight = m_Camera.getHeight();
263+
double cameraFPS = m_Camera.getFPS();
264+
265+
// Use camera data here
266+
}
267+
```
268+
269+
## Setup (Simulator)
125270

126271
Once started, make sure in the SimGUI that your robot state is set to "Disabled", **not** "Disconnected".
127272

128273
### Spawning in a Robot
129274

130-
Open up [Fission](https://synthesis.autodesk.com/fission/) and spawn in a robot. Once spawned in, place it down and open the config panel. This can be
131-
done by using the left-hand menu and navigating to your robot in the config panel, or by right-clicking on your robot and selecting the "Configure" option.
275+
Head over to [Fission](https://synthesis.autodesk.com/fission/) and load your robot model into the simulation. Once it appears, place it on the field and open the configuration panel, either through the left sidebar menu or by right-clicking your robot and selecting "Configure."
132276

133-
Next, switch the brain currently controlling the robot. In order to give the simulation control over the robot, the brain must be switched from "Synthesis"
134-
to "WPILib". At the moment, only one robot can be controlled by the simulation at a time.
277+
In order to configure your robot to use code simulation, you need to switch the robot's "brain" from "Synthesis" to "WPILib", which tells the simulator to receive inputs from the simulated code rather than from keyboard or controller inputs.
135278

136-
In the top-right, there should be a connection status indicator. If your robot program was running prior to switching to the "WPILib" brain, it should connect
137-
quickly.
279+
The indicator in the top-right corner of the screen will display the status of the connection between Synthesis and the simulator.
138280

139281
### Simulation Configuration
140282

141-
Under your robot in the config panel, there should be a Simulation option now. Here you can find all the settings for the code simulation.
283+
Once you've switched to the WPILib brain, a new "Simulation" section will appear in your robot's config panel.
142284

143-
![image_caption](img/code-sim/config-panel-simulation.png)
285+
![config panel simulation screen](img/code-sim/config-panel-simulation.png)
144286

145287
#### Auto Reconnect
146288

147-
You can enabled auto reconnect incase you are having issues with this. In order for it to take affect, you have to enable the setting, then switch back to the "Synthesis"
148-
brain and then back again to the "WPILib" brain. This setting will be saved.
289+
This setting configures Synthesis to automatically attempt to reconnect when connection is lost, and will stay configured the next time you open Synthesis.
149290

150291
#### Wiring Panel
151292

152-
This panel can be used to "wire up" your robot. It will show you all the inputs and outputs available from both the simulation and robot. The handles (little circles with
153-
labels) are colored to indicate the type of data they represent. Hover over the information icons for more information.
293+
The wiring panel shows all available inputs and outputs from both your running code and the simulated robot hardware. Each connection point (the small labeled circles) is color-coded by data type: analog signals, digital I/O, PWM outputs, etc.
154294

155-
![image_caption](img/code-sim/wiring-panel.png)
295+
![wiring panel](img/code-sim/wiring-panel.png)
156296

157-
The bottom-left controls can be used to zoom in/out, fit your view to the nodes, and add junction nodes for connection many connections to many connections.
297+
The controls in the bottom-left of the panel can be used to zoom, auto-fit the view, and add junction nodes when you need to split one signal to multiple destinations.
158298

159299
#### Auto Testing
160300

161-
The Auto Testing panel allows for iterative testing of an autonomous routine. I'd recommend making sure that the auto reconnect option is enabled.
301+
The Auto Testing panel can be used to easily reset your robot's state for rapid autonomous testing.
302+
303+
![auto testing panel](img/code-sim/auto-testing.png)
162304

163-
![image_caption](img/code-sim/auto-testing.png)
305+
Set your maximum test time, choose your alliance station, and input any game-specific data your autonomous routine needs. Position your robot exactly where you want it to start, then press the start button to begin your autonomous routine.
164306

165-
You can specify a max time, alliance station, and game data. Once you've decided on those and have place the robot where you want, you can start your auto routine.
166-
After the specified amount of time, or when the stop button is pressed, the simulation will freeze and you can either reset to where you started, or close the panel.
307+
When the timer elapses or the test is manually stopped, you're provided the option to reset the robot for another test.
167308

168309
## Need More Help?
169310

170311
If you need help with anything regarding Synthesis or it's related features please reach out through our
171-
[discord server](https://www.discord.gg/hHcF9AVgZA). It's the best way to get in contact with the community and our current developers.
312+
[Discord](https://www.discord.gg/hHcF9AVgZA). It's the best way to get in contact with the community and our current developers.

0 commit comments

Comments
 (0)