Canada
Asked
Resolved Resolved by DJ Sures!

JSON To Mjbots Quad Robot Series

My robot wants to communicate via JSON commands (it is controlled via JSON from a browser) but I need to use ARC to talk to it. It also sends a lot of data back over JSON to provide current servo status that would need to be fed back into ARC.  Any thoughts if this can be adapted to communicate with the robot? https://github.com/mjbots/quad/blob/main/mech/web_control_assets/index.html



Related Hardware (view all EZB hardware)
EZ-B V4 by EZ-Robot
EZ-B v4/2 robot controller: dual Cortex ARM, Wi-Fi, audio/video, 24 servo/digital ports, I2C/UART, camera and Synthiam ARC control for custom robots
Wi-Fi / USB
Servos 24
Camera
Audio
UART 3
I2C
ADC 8
Digital 24

Related Robot Skills (view all robot skills)
Custom Movement Panel maps directional commands to scripts, adjusts per-wheel speed (0-255), and integrates with joysticks for unsupported motors
Websocket Client by Synthiam
WebSocket client for ARC: open/send messages, store server responses in a variable, run a response script, and track connection status.

ARC Pro

Upgrade to ARC Pro

Unleash your robot's full potential with the cutting-edge features and intuitive programming offered by Synthiam ARC Pro.

Author Avatar
PRO
Synthiam
#2  

@Nink - you sent a link to an HTML file. A JSON file looks similar to XML in that it has labelled parameter tags containing values.

i.e.

{
    "glossary": {
        "title": "example glossary",
      "GlossDiv": {
            "title": "S",
         "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
               "SortAs": "SGML",
               "GlossTerm": "Standard Generalized Markup Language",
               "Acronym": "SGML",
               "Abbrev": "ISO 8879:1986",
               "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                  "GlossSeeAlso": ["GML", "XML"]
                    },
               "GlossSee": "markup"
                }
            }
        }
    }
}
Author Avatar
PRO
Synthiam
#3   — Edited

But, you can also parse the JSON in javascript right in arc..

Here's an example parsing JSON from a string...


var resp = '{ "glossary": { "title": "example glossary" } }';

var myObj = JSON.parse(resp);

print (myObj.glossary.title);

And here's an example of getting JSON from an HTTP get request...


var resp = Net.hTTPGet("https://ip.jsontest.com/";); 

var myObj = JSON.parse(resp);

print (myObj.ip);
Author Avatar
PRO
Synthiam
#7  

The json that you would need to send is defined as a class struct in the app.js. You could edit the app.js and print the json to the console. But this is essentially the part that is creating the json - it is stored in a "command" variable and built throughout those lines by extracting data from the webpage objects and joystick.


let command = {
      "command" : {
        "mode" : (() => {
          if (this._mode == "off") { return "stopped"; }
          if (this._mode == "stop") { return "zero_velocity" ;}
          if (this._mode == "idle") { return "rest"; }

          if (!movement_commanded && !force_step &&
              (this._mode == "walk" ||
               this._mode == "pronk")) {
            return "rest";
          }
          if (this._mode == "walk") { return "walk"; }
          if (this._mode == "pronk") { return "jump"; }
          return "zero_velocity";
        })(),
      },
    };

    const record_data = getElement("record_data").checked;
    // Unset leaves the current value alone, but we can just send it
    // every time.
    command["command"]["log"] = record_data ? "enable" : "disable";

    command["command"]["v_R"] = v_R;
    command["command"]["w_R"] = w_R;

    if (pose_RB) {
      command["command"]["rest"] = {
        offset_RB : pose_RB,
      };
    }

    if (command["command"]["mode"] == "jump") {
      command["command"]["jump"] = {
        "acceleration" : this.getJumpAcceleration(),
        "repeat" : getElement("jump_repeat").checked,
      };
    } else if (command["command"]["mode"] == "walk") {
      const height_engaged = (
        this._joystick.down(Joystick.BUTTON_SHOULDER_RB) ||
          this._keys['h']);
      const maximize_flight = getElement("maximize_flight").checked;

      command["command"]["walk"] = {
        "step_height" : height_engaged ?
          Number(getElement("height_scale").value) : 1.0,
        "maximize_flight" : maximize_flight,
      };
    }

    const command_string = JSON.stringify(command);
    getElement('current_json_command').innerHTML = command_string;

    if (this._websocket.readyState == WebSocket.OPEN) {
      this._websocket.send(command_string)   <--------------- SENDS THE JSON
    }
  }
Author Avatar
PRO
Synthiam
#8  

*Moved into own thread because it is out of scope for the original thread