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 EZ-B v4

ARC Pro

Upgrade to ARC Pro

Join the ARC Pro community and gain access to a wealth of resources and support, ensuring your robot's success.

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"
                }
            }
        }
    }
}

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("http://ip.jsontest.com/";); 

var myObj = JSON.parse(resp);

print (myObj.ip);

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
    }
  }

PRO
Synthiam
#8  

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