spaceshipyard / mars-rover-firmware-lib

Apache License 2.0
1 stars 0 forks source link

Improve firmware messaging mechanism #1

Closed chaos-adept closed 6 years ago

chaos-adept commented 7 years ago

it is required to improve current messaging mechanism for more easy checking state

it means that we need probably start using a library for messaging like

chaos-adept commented 7 years ago

for information

Добрый день. Нашел статью по программированнию управления роботом, аналогично ващей арихетектуре, но там вместо java Node.js. Но в принципе это не главное, там есть пример протокола для управления роботом. Можно взять его за основу, для разработки. https://habrahabr.ru/post/315480/

Best regards, Vladimir.

Hi Vladimir, It is interesting information, I have explored this article, and I have tried library https://github.com/sadr0b0t/babbler_h , it looks nice, but it does not work for me on my robodyn arduino uno analog in case of json usage. I would like to note another important thing that I have found, this library does not allow to communicate from robot to the human or dispatcher, the robot could work only as a “slave”. It is not possible to just send some sensor data or some alarms(like I need new batteries) without input command like it does https://github.com/thijse/Arduino-CmdMessenger.

So far it looks that it makes sense to take some ideas from babbler and CmdMessenger and combine them together. We have a task for it here https://github.com/spaceshipyard/mars-rover-firmware-lib/issues/1

Thanks, Denis

chaos-adept commented 7 years ago

so far it seems that ArudinoJSON could be used as a part of communication , we could replace raw string with an object


// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// https://bblanchon.github.io/ArduinoJson/
// If you like this project, please add a star!

#include <ArduinoJson.h>
String inputString = ""; 
boolean stringComplete = false;

StaticJsonBuffer<200> jsonBuffer;

void setup() {
  Serial.begin(9600);
  while (!Serial) {
    // wait serial port initialization
  }

  // Memory pool for JSON object tree.
  //
  // Inside the brackets, 200 is the size of the pool in bytes,
  // If the JSON object is more complex, you need to increase that value.
  // See https://bblanchon.github.io/ArduinoJson/assistant/

  // StaticJsonBuffer allocates memory on the stack, it can be
  // replaced by DynamicJsonBuffer which allocates in the heap.
  //
  // DynamicJsonBuffer  jsonBuffer(200);

  // JSON input string.
  //
  // It's better to use a char[] as shown here.
  // If you use a const char* or a String, ArduinoJson will
  // have to make a copy of the input in the JsonBuffer.
  char json[] =
      "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
  parseJson(json);

}

void parseJson(const char* json) {
    // Root of the object tree.
  //
  // It's a reference to the JsonObject, the actual bytes are inside the
  // JsonBuffer with all the other nodes of the object tree.
  // Memory is freed when jsonBuffer goes out of scope.
  JsonObject& root = jsonBuffer.parseObject(json);

  // Test if parsing succeeds.
  if (!root.success()) {
    Serial.println("parseObject() failed");
    return;
  }

  // Fetch values.
  //
  // Most of the time, you can rely on the implicit casts.
  // In other case, you can do root["time"].as<long>();
  const char* sensor = root["sensor"];
  long time = root["time"];
  double latitude = root["data"][0];
  double longitude = root["data"][1];

  // Print values.
  Serial.println(sensor);
  Serial.println(time);
  Serial.println(latitude, 6);
  Serial.println(longitude, 6);
}

void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
    Serial.println(inputString);
    parseJson(inputString.c_str());
    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}

/*
  SerialEvent occurs whenever a new data comes in the
 hardware serial RX.  This routine is run between each
 time loop() runs, so using delay inside loop can delay
 response.  Multiple bytes of data may be available.
 */
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }
}```
chaos-adept commented 7 years ago

Communication has been extracted to https://github.com/spaceshipyard/ArduinoJsonRpc library

chaos-adept commented 6 years ago

in the end, the solution is just to use raspberry as a component of communications with http://johnny-five.io/platform-support/#arduino-uno , work has been done in https://github.com/spaceshipyard/mars-rover-node-bridge