From cbd91d5fb9b94ac00cf13854d7586f14004bf0d9 Mon Sep 17 00:00:00 2001 From: Dominique Luna Date: Wed, 26 Apr 2017 18:05:43 -0700 Subject: [PATCH] remove string -> double conversion, make string processing more robust to websocket junk --- src/main.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6aeea2ecc..f6ce01142 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,11 +18,11 @@ double rad2deg(double x) { return x * 180 / pi(); } std::string hasData(std::string s) { auto found_null = s.find("null"); auto b1 = s.find_first_of("["); - auto b2 = s.find_last_of("]"); + auto b2 = s.rfind("}]"); if (found_null != std::string::npos) { return ""; } else if (b1 != std::string::npos && b2 != std::string::npos) { - return s.substr(b1, b2 - b1 + 1); + return s.substr(b1, b2 - b1 + 2); } return ""; } @@ -45,11 +45,11 @@ int main() { std::string event = j[0].get(); if (event == "telemetry") { // j[1] is the data JSON object - double cte = std::stod(j[1]["cte"].get()); - double epsi = std::stod(j[1]["epsi"].get()); - double speed = std::stod(j[1]["speed"].get()); - double throttle = std::stod(j[1]["throttle"].get()); - double angle = std::stod(j[1]["steering_angle"].get()); + double cte = j[1]["cte"]; + double epsi = j[1]["epsi"]; + double speed = j[1]["speed"]; + double throttle = j[1]["throttle"]; + double angle = j[1]["steering_angle"]; /* * TODO: Calculate steeering angle and throttle here.