Login
gpcr/examples/robots_demo.cr
Login

[edit]

examples/robots_demo.cr @ trunk

../src/gpcr/robots_palette

require '../src/gpcr/robots_palette'
scale = 0.75
fontscale = 1.500
box wid 2.954 ht 0.340 with .nw at (0.035, -0.035) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.954 ht 0.340 with .nw at (0.000, 0.000) fill 0x7C83E6 color 0x7C83E6
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -0.022) fill 0xffffff color 0xffffff
line from (0.140, 0.000) to (0.140, -0.340) color white
line from (2.814, 0.000) to (2.814, -0.340) color white
text "require '../src/gpcr/robots_palette'" color white at (1.477, -0.170)

crystal source

require "../src/gpcr/robots_palette"

json block

{
  "kind": "require",
  "name": "../src/gpcr/robots_palette",
  "doc": null,
  "children": []
}

demo_robot

Demo robot — ticket 2b065b36a6. Built entirely from src/gpcr/robots_palette.cr primitives, proving the palette authors crystal-robots-shaped programs that GPCR can render and round-trip: scans ahead, fires if a target is in range, otherwise advances; exercises call / c_call(main) / if-else / while.

main 2 angle = 0 while  angle < 360 3 range = scan(angle, 0) if  range > 0  then 4 cannon  angle   range 5 else drive  angle   50 6 angle += 15
scale = 0.75
fontscale = 1.500
box wid 2.720 ht 5.500 rad 0.035 with .nw at (0.035, -0.455) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.720 ht 5.500 rad 0.035 with .nw at (0.000, -0.420) fill 0xFFAB19 color 0xFFAB19
box wid 2.340 ht 4.830 rad 0.035 with .nw at (0.320, -0.880) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -0.443) fill 0xffffff color 0xffffff
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -5.952) fill 0xFFAB19 color 0xFFAB19
text "main" color white at (1.360, -0.650)
box wid 0.240 ht 0.220 rad 0.110 at (2.570, -0.560) fill 0x334155 color 0x334155
text "2" at (2.570, -0.560) color 0xf8fafc
box wid 1.013 ht 0.340 rad 0.035 with .nw at (0.455, -0.975) fill 0xCBD5E1 color 0xCBD5E1
box wid 1.013 ht 0.340 rad 0.035 with .nw at (0.420, -0.940) fill 0x4C97FF color 0x4C97FF
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -0.963) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -1.313) fill 0x4C97FF color 0x4C97FF
text "angle = 0" color white at (0.927, -1.110)
box wid 2.720 ht 4.210 rad 0.035 with .nw at (0.455, -1.395) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.720 ht 4.210 rad 0.035 with .nw at (0.420, -1.360) fill 0xFFAB19 color 0xFFAB19
box wid 2.340 ht 3.540 rad 0.035 with .nw at (0.740, -1.820) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -1.383) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -5.603) fill 0xFFAB19 color 0xFFAB19
text "while " color white at (1.130, -1.590)
box wid 1.180 ht 0.205 rad 0.102 with .nw at (1.416, -1.488) fill 0xFFFFFF color 0xFFFFFF
text "angle < 360" color 0x263238 at (2.006, -1.590)
box wid 0.240 ht 0.220 rad 0.110 at (2.990, -1.500) fill 0x334155 color 0x334155
text "3" at (2.990, -1.500) color 0xf8fafc
box wid 2.056 ht 0.340 rad 0.035 with .nw at (0.875, -1.915) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.056 ht 0.340 rad 0.035 with .nw at (0.840, -1.880) fill 0x4C97FF color 0x4C97FF
box wid 0.300 ht 0.065 rad 0.018 at (1.210, -1.903) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (1.210, -2.252) fill 0x4C97FF color 0x4C97FF
text "range = scan(angle, 0)" color white at (1.868, -2.050)
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.875, -2.335) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.840, -2.300) fill 0xFFAB19 color 0xFFAB19
box wid 2.340 ht 0.540 rad 0.035 with .nw at (1.160, -2.760) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (1.210, -2.323) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (1.210, -3.542) fill 0xFFAB19 color 0xFFAB19
text "if " color white at (1.419, -2.530)
box wid 1.003 ht 0.205 rad 0.102 with .nw at (1.593, -2.428) fill 0xFFFFFF color 0xFFFFFF
text "range > 0" color 0x263238 at (2.095, -2.530)
text " then" color white at (2.853, -2.530)
box wid 0.240 ht 0.220 rad 0.110 at (3.410, -2.440) fill 0x334155 color 0x334155
text "4" at (3.410, -2.440) color 0xf8fafc
box wid 2.630 ht 0.340 rad 0.035 with .nw at (1.295, -2.855) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.630 ht 0.340 rad 0.035 with .nw at (1.260, -2.820) fill 0x4C97FF color 0x4C97FF
box wid 0.300 ht 0.065 rad 0.018 at (1.630, -2.843) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (1.630, -3.192) fill 0x4C97FF color 0x4C97FF
text "cannon " color white at (1.715, -2.990)
box wid 0.683 ht 0.205 rad 0.102 with .nw at (2.090, -2.888) fill 0xFFFFFF color 0xFFFFFF
text "angle" color 0x263238 at (2.431, -2.990)
text " " color white at (2.857, -2.990)
box wid 0.703 ht 0.205 rad 0.102 with .nw at (2.987, -2.888) fill 0xFFFFFF color 0xFFFFFF
text "range" color 0x263238 at (3.338, -2.990)
box wid 0.240 ht 0.220 rad 0.110 at (3.740, -2.960) fill 0x334155 color 0x334155
text "5" at (3.740, -2.960) color 0xf8fafc
box wid 0.300 ht 0.065 rad 0.018 at (1.630, -3.192) fill 0x4C97FF color 0x4C97FF
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.875, -3.625) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.840, -3.590) fill 0xFFAB19 color 0xFFAB19
box wid 2.340 ht 0.540 rad 0.035 with .nw at (1.160, -4.050) fill 0xFFFDE7 color 0xFFFDE7
text "else" color white at (2.200, -3.820)
box wid 2.177 ht 0.340 rad 0.035 with .nw at (1.295, -4.145) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.177 ht 0.340 rad 0.035 with .nw at (1.260, -4.110) fill 0x4C97FF color 0x4C97FF
box wid 0.300 ht 0.065 rad 0.018 at (1.630, -4.133) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (1.630, -4.482) fill 0x4C97FF color 0x4C97FF
text "drive " color white at (1.616, -4.280)
box wid 0.683 ht 0.205 rad 0.102 with .nw at (1.893, -4.178) fill 0xFFFFFF color 0xFFFFFF
text "angle" color 0x263238 at (2.234, -4.280)
text " " color white at (2.660, -4.280)
box wid 0.447 ht 0.205 rad 0.102 with .nw at (2.790, -4.178) fill 0xFFFFFF color 0xFFFFFF
text "50" color 0x263238 at (3.013, -4.280)
box wid 0.240 ht 0.220 rad 0.110 at (3.287, -4.250) fill 0x334155 color 0x334155
text "6" at (3.287, -4.250) color 0xf8fafc
box wid 0.300 ht 0.065 rad 0.018 at (1.630, -4.482) fill 0x4C97FF color 0x4C97FF
box wid 1.215 ht 0.340 rad 0.035 with .nw at (0.875, -4.915) fill 0xCBD5E1 color 0xCBD5E1
box wid 1.215 ht 0.340 rad 0.035 with .nw at (0.840, -4.880) fill 0x4C97FF color 0x4C97FF
box wid 0.300 ht 0.065 rad 0.018 at (1.210, -4.903) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (1.210, -5.253) fill 0x4C97FF color 0x4C97FF
text "angle += 15" color white at (1.447, -5.050)

crystal source

@[GPCR::Graph]
def demo_robot
  main("Demo") do
    angle = 0
    while angle < 360
      range = scan(angle, 0)
      if range > 0
        cannon(angle, range)
      else
        drive(angle, 50)
      end
      angle += 15
    end
  end
end

json block

{
  "kind": "graph",
  "name": "demo_robot",
  "doc": "Demo robot — ticket 2b065b36a6. Built entirely from src/gpcr/robots_palette.cr\nprimitives, proving the palette authors crystal-robots-shaped programs that\nGPCR can render and round-trip: scans ahead, fires if a target is in range,\notherwise advances; exercises call / c_call(main) / if-else / while.",
  "children": [
    {
      "kind": "c_call",
      "name": "main",
      "op_index": 2,
      "args": [
        "Demo"
      ],
      "body": [
        {
          "kind": "other",
          "text": "angle = 0"
        },
        {
          "kind": "while",
          "cond": "angle < 360",
          "op_index": 3,
          "body": [
            {
              "kind": "other",
              "text": "range = scan(angle, 0)"
            },
            {
              "kind": "if",
              "cond": "range > 0",
              "op_index": 4,
              "then": [
                {
                  "kind": "call",
                  "name": "cannon",
                  "receiver": null,
                  "op_index": 5,
                  "args": [
                    "angle",
                    "range"
                  ],
                  "label": "cannon [angle] [range]",
                  "children": []
                }
              ],
              "else": [
                {
                  "kind": "call",
                  "name": "drive",
                  "receiver": null,
                  "op_index": 6,
                  "args": [
                    "angle",
                    "50"
                  ],
                  "label": "drive [angle] [50]",
                  "children": []
                }
              ]
            },
            {
              "kind": "other",
              "text": "angle += 15"
            }
          ]
        }
      ]
    }
  ]
}

go_to_corner

Corner-picking helper — mirrors crystal-robots' sniper.cr new_corner pattern (case/when/else), exercising the :case node kind in the round-trip.

go to corner  corner 7 match  corner when  1 drive  0   50 when  2 drive  90   50 when  3 drive  180   50 else drive  270   50
scale = 0.75
fontscale = 1.500
box wid 2.145 ht 0.340 rad 0.035 with .nw at (0.035, -0.455) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.145 ht 0.340 rad 0.035 with .nw at (0.000, -0.420) fill 0x4C97FF color 0x4C97FF
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -0.443) fill 0xffffff color 0xffffff
text "go to corner " color white at (0.637, -0.590)
box wid 0.752 ht 0.205 rad 0.102 with .nw at (1.194, -0.488) fill 0xFFFFFF color 0xFFFFFF
text "corner" color 0x263238 at (1.569, -0.590)
box wid 0.240 ht 0.220 rad 0.110 at (1.995, -0.560) fill 0x334155 color 0x334155
text "7" at (1.995, -0.560) color 0xf8fafc
box wid 2.720 ht 0.340 rad 0.035 with .nw at (0.035, -0.875) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.720 ht 0.340 rad 0.035 with .nw at (0.000, -0.840) fill 0xFFAB19 color 0xFFAB19
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -0.862) fill 0xffffff color 0xffffff
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -1.212) fill 0xFFAB19 color 0xFFAB19
text "match " color white at (0.924, -1.010)
box wid 0.752 ht 0.205 rad 0.102 with .nw at (1.250, -0.907) fill 0xFFFFFF color 0xFFFFFF
text "corner" color 0x263238 at (1.626, -1.010)
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.035, -1.295) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.000, -1.260) fill 0xFFAB19 color 0xFFAB19
box wid 2.340 ht 0.540 rad 0.035 with .nw at (0.320, -1.720) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -2.502) fill 0xFFAB19 color 0xFFAB19
text "when " color white at (1.103, -1.490)
box wid 0.348 ht 0.205 rad 0.102 with .nw at (1.444, -1.387) fill 0xFFFFFF color 0xFFFFFF
text "1" color 0x263238 at (1.619, -1.490)
box wid 1.842 ht 0.340 rad 0.035 with .nw at (0.455, -1.815) fill 0xCBD5E1 color 0xCBD5E1
box wid 1.842 ht 0.340 rad 0.035 with .nw at (0.420, -1.780) fill 0x4C97FF color 0x4C97FF
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -1.803) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -2.152) fill 0x4C97FF color 0x4C97FF
text "drive " color white at (0.776, -1.950)
box wid 0.348 ht 0.205 rad 0.102 with .nw at (1.053, -1.847) fill 0xFFFFFF color 0xFFFFFF
text "0" color 0x263238 at (1.227, -1.950)
text " " color white at (1.486, -1.950)
box wid 0.447 ht 0.205 rad 0.102 with .nw at (1.615, -1.847) fill 0xFFFFFF color 0xFFFFFF
text "50" color 0x263238 at (1.839, -1.950)
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -2.152) fill 0x4C97FF color 0x4C97FF
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.035, -2.585) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.000, -2.550) fill 0xFFAB19 color 0xFFAB19
box wid 2.340 ht 0.540 rad 0.035 with .nw at (0.320, -3.010) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -3.792) fill 0xFFAB19 color 0xFFAB19
text "when " color white at (1.103, -2.780)
box wid 0.348 ht 0.205 rad 0.102 with .nw at (1.444, -2.677) fill 0xFFFFFF color 0xFFFFFF
text "2" color 0x263238 at (1.619, -2.780)
box wid 1.941 ht 0.340 rad 0.035 with .nw at (0.455, -3.105) fill 0xCBD5E1 color 0xCBD5E1
box wid 1.941 ht 0.340 rad 0.035 with .nw at (0.420, -3.070) fill 0x4C97FF color 0x4C97FF
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -3.092) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -3.442) fill 0x4C97FF color 0x4C97FF
text "drive " color white at (0.776, -3.240)
box wid 0.447 ht 0.205 rad 0.102 with .nw at (1.053, -3.137) fill 0xFFFFFF color 0xFFFFFF
text "90" color 0x263238 at (1.276, -3.240)
text " " color white at (1.584, -3.240)
box wid 0.447 ht 0.205 rad 0.102 with .nw at (1.714, -3.137) fill 0xFFFFFF color 0xFFFFFF
text "50" color 0x263238 at (1.937, -3.240)
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -3.442) fill 0x4C97FF color 0x4C97FF
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.035, -3.875) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.000, -3.840) fill 0xFFAB19 color 0xFFAB19
box wid 2.340 ht 0.540 rad 0.035 with .nw at (0.320, -4.300) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.370, -5.082) fill 0xFFAB19 color 0xFFAB19
text "when " color white at (1.103, -4.070)
box wid 0.348 ht 0.205 rad 0.102 with .nw at (1.444, -3.968) fill 0xFFFFFF color 0xFFFFFF
text "3" color 0x263238 at (1.619, -4.070)
box wid 2.039 ht 0.340 rad 0.035 with .nw at (0.455, -4.395) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.039 ht 0.340 rad 0.035 with .nw at (0.420, -4.360) fill 0x4C97FF color 0x4C97FF
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -4.382) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -4.732) fill 0x4C97FF color 0x4C97FF
text "drive " color white at (0.776, -4.530)
box wid 0.545 ht 0.205 rad 0.102 with .nw at (1.053, -4.427) fill 0xFFFFFF color 0xFFFFFF
text "180" color 0x263238 at (1.325, -4.530)
text " " color white at (1.683, -4.530)
box wid 0.447 ht 0.205 rad 0.102 with .nw at (1.812, -4.427) fill 0xFFFFFF color 0xFFFFFF
text "50" color 0x263238 at (2.036, -4.530)
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -4.732) fill 0x4C97FF color 0x4C97FF
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.035, -5.165) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.720 ht 1.210 rad 0.035 with .nw at (0.000, -5.130) fill 0xFFAB19 color 0xFFAB19
box wid 2.340 ht 0.540 rad 0.035 with .nw at (0.320, -5.590) fill 0xFFFDE7 color 0xFFFDE7
text "else" color white at (1.360, -5.360)
box wid 2.039 ht 0.340 rad 0.035 with .nw at (0.455, -5.685) fill 0xCBD5E1 color 0xCBD5E1
box wid 2.039 ht 0.340 rad 0.035 with .nw at (0.420, -5.650) fill 0x4C97FF color 0x4C97FF
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -5.672) fill 0xFFFDE7 color 0xFFFDE7
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -6.022) fill 0x4C97FF color 0x4C97FF
text "drive " color white at (0.776, -5.820)
box wid 0.545 ht 0.205 rad 0.102 with .nw at (1.053, -5.717) fill 0xFFFFFF color 0xFFFFFF
text "270" color 0x263238 at (1.325, -5.820)
text " " color white at (1.683, -5.820)
box wid 0.447 ht 0.205 rad 0.102 with .nw at (1.812, -5.717) fill 0xFFFFFF color 0xFFFFFF
text "50" color 0x263238 at (2.036, -5.820)
box wid 0.300 ht 0.065 rad 0.018 at (0.790, -6.022) fill 0x4C97FF color 0x4C97FF

crystal source

@[GPCR::Block(shape: :command, label: "go to corner #{corner}", category: "Robots")]
def go_to_corner(corner : Int32)
  case corner
  when 1
    drive(0, 50)
  when 2
    drive(90, 50)
  when 3
    drive(180, 50)
  else
    drive(270, 50)
  end
end