Spaces:
Sleeping
Sleeping
File size: 7,409 Bytes
6045fb3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | <#
.SYNOPSIS
Drive the live Render FastAPI from PowerShell --- run a simulation,
request a plot, save the PNG to disk, open it in the default viewer.
.DESCRIPTION
The deployed backend at https://cdpr-api.onrender.com exposes:
GET /health -- liveness check
GET /robots -- catalogue
POST /simulate -- forward integration (returns JSON timeseries)
POST /plot -- render a plot (returns {"png_base64": "..."})
POST /workspace -- WCW / WFW grid scan
The free tier sleeps after 15 min idle. The first request can take
~50 s while the container wakes; later requests respond in under a
second. This script reuses the cached connection so the cold start
only happens once per PowerShell session.
.PARAMETER Action
One of: health, robots, simulate, plot, workspace.
.PARAMETER Robot
Reference robot name. Defaults to ipanema_class.
.PARAMETER Kind
Trajectory kind: hold, line, circle, lissajous. Default circle.
.PARAMETER Duration
Simulation length in seconds. Default 1.5.
.PARAMETER Dt
Integration step in seconds. Default 2e-3.
.PARAMETER PlotKind
For Action=plot: which plot to render (position, cable_tensions,
cable_lengths, tracking_error, condition_number,
trajectory_xy / xz / yz).
.PARAMETER OutDir
Where to drop response JSON / PNG. Defaults to ./out/render-<stamp>/.
.PARAMETER OpenPng
For Action=plot: open the saved PNG in the default viewer.
.PARAMETER BaseUrl
Override the API root. Defaults to the live Render URL.
.EXAMPLE
# Liveness check (also a good way to warm the worker before a big call).
.\scripts\call_render.ps1 -Action health
.EXAMPLE
# Run a circle and save the JSON response.
.\scripts\call_render.ps1 -Action simulate -Robot ipanema_class -Kind circle
.EXAMPLE
# Render a tension plot and pop it open.
.\scripts\call_render.ps1 -Action plot -PlotKind cable_tensions -OpenPng
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[ValidateSet("health", "robots", "simulate", "plot", "workspace")]
[string]$Action,
[ValidateSet("point_mass_3d", "planar_translational", "ipanema_class", "cogiro_class")]
[string]$Robot = "ipanema_class",
[ValidateSet("hold", "line", "circle", "lissajous")]
[string]$Kind = "circle",
[double]$Duration = 1.5,
[double]$Dt = 2e-3,
[double]$PayloadMass = 0.0,
[ValidateSet("min_norm", "centered", "preferred")]
[string]$Objective = "centered",
[ValidateSet(
"position", "velocity", "angular_velocity",
"cable_lengths", "cable_tensions",
"tracking_error", "condition_number",
"trajectory_xy", "trajectory_xz", "trajectory_yz"
)]
[string]$PlotKind = "position",
[string]$OutDir = "",
[switch]$OpenPng,
[string]$BaseUrl = "https://cdpr-api.onrender.com"
)
$ErrorActionPreference = "Stop"
# Resolve output directory.
if (-not $OutDir) {
$stamp = Get-Date -Format "yyyyMMdd-HHmmss"
$OutDir = Join-Path -Path (Get-Location) -ChildPath "out\render-$stamp"
}
if (-not (Test-Path $OutDir)) {
New-Item -ItemType Directory -Force -Path $OutDir | Out-Null
}
Write-Host ("[call_render] base = {0}" -f $BaseUrl)
Write-Host ("[call_render] action = {0}" -f $Action)
Write-Host ("[call_render] outdir = {0}" -f $OutDir)
function Write-Json {
param([string]$Path, $Object)
$Object | ConvertTo-Json -Depth 12 | Out-File -FilePath $Path -Encoding utf8
Write-Host ("[call_render] wrote -> {0}" -f $Path)
}
# Build the per-action request body.
$body = $null
switch ($Action) {
"health" { $url = "$BaseUrl/health"; $method = "GET" }
"robots" { $url = "$BaseUrl/robots"; $method = "GET" }
"simulate" {
$url = "$BaseUrl/simulate"; $method = "POST"
$params = switch ($Kind) {
"line" {
@{ start = @(0.0, 0.0, 0.5); end = @(0.3, 0.0, 0.5) }
}
"circle" {
@{
center = @(0.0, 0.0, 0.5)
radius = 0.2
axis = @(0.0, 0.0, 1.0)
angle_span = [math]::PI * 2.0
}
}
"lissajous" {
@{
center = @(0.0, 0.0, 0.5)
amplitudes = @(0.2, 0.2, 0.0)
frequencies = @(1.0, 2.0, 0.0)
phases = @(0.0, [math]::PI / 2.0, 0.0)
}
}
default { @{} }
}
$body = @{
robot = $Robot
payload_mass = $PayloadMass
duration = $Duration
dt = $Dt
tension_objective = $Objective
gravity = @(0.0, 0.0, -9.81)
trajectory = @{ kind = $Kind; duration = $Duration; params = $params }
}
}
"plot" {
$url = "$BaseUrl/plot"; $method = "POST"
$body = @{
kind = $PlotKind
simulate = @{
robot = $Robot
payload_mass = $PayloadMass
duration = $Duration
dt = $Dt
tension_objective = $Objective
gravity = @(0.0, 0.0, -9.81)
trajectory = @{
kind = $Kind
duration = $Duration
params = @{
center = @(0.0, 0.0, 0.5)
radius = 0.2
axis = @(0.0, 0.0, 1.0)
angle_span = [math]::PI * 2.0
}
}
}
}
}
"workspace" {
$url = "$BaseUrl/workspace"; $method = "POST"
$body = @{
robot = $Robot
xlim = @(-0.6, 0.6); ylim = @(-0.4, 0.4); zlim = @(-0.4, 0.4)
resolution = 8
kind = "wcw"
}
}
}
# Invoke. Render's free tier cold-start can exceed PowerShell's default
# 100-s WebRequest timeout the first time you hit /simulate; bump it.
$invokeArgs = @{ Uri = $url; Method = $method; TimeoutSec = 180 }
if ($body) {
$invokeArgs.Body = ($body | ConvertTo-Json -Depth 12)
$invokeArgs.ContentType = "application/json"
}
Write-Host ("[call_render] {0} {1} (timeout 180s --- cold start can take ~50 s)" -f $method, $url)
$t0 = Get-Date
try {
$response = Invoke-RestMethod @invokeArgs
} catch {
Write-Host ("[call_render] HTTP error: {0}" -f $_.Exception.Message) -ForegroundColor Red
if ($_.Exception.Response) {
$stream = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($stream)
$errBody = $reader.ReadToEnd()
Write-Host ("[call_render] response body:`n{0}" -f $errBody) -ForegroundColor Red
}
exit 1
}
$elapsed = (Get-Date) - $t0
Write-Host ("[call_render] {0:N2} s" -f $elapsed.TotalSeconds)
# Persist the response.
$jsonPath = Join-Path $OutDir "$Action.json"
Write-Json -Path $jsonPath -Object $response
# Special handling for /plot: decode the base64 PNG to disk.
if ($Action -eq "plot" -and $response.png_base64) {
$pngPath = Join-Path $OutDir "$PlotKind.png"
$bytes = [Convert]::FromBase64String($response.png_base64)
[IO.File]::WriteAllBytes($pngPath, $bytes)
Write-Host ("[call_render] PNG -> {0}" -f $pngPath)
if ($OpenPng) {
Start-Process $pngPath
}
}
Write-Host "[call_render] done."
|