ROME 2 Praktikum 4

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPScriptIMU.cpp Source File

HTTPScriptIMU.cpp

00001 /*
00002  * HTTPScriptIMU.cpp
00003  * Copyright (c) 2020, ZHAW
00004  * All rights reserved.
00005  */
00006 
00007 #include "HTTPScriptIMU.h"
00008 
00009 using namespace std;
00010 
00011 inline string float2String(float f) {
00012     
00013     char buffer[32];
00014     sprintf(buffer, "%.3f", f);
00015     
00016     return string(buffer);
00017 }
00018 
00019 /**
00020  * Create and initialize this http script.
00021  * @param imu a reference to the imu to read data from.
00022  */
00023 HTTPScriptIMU::HTTPScriptIMU(IMU& imu) : imu(imu) {}
00024 
00025 HTTPScriptIMU::~HTTPScriptIMU() {}
00026 
00027 /**
00028  * This method gets called by the http server, when an object of this class is
00029  * registered with the server, and the corresponding script is called
00030  * by an http client.
00031  */
00032 string HTTPScriptIMU::call(vector<string> names, vector<string> values) {
00033     
00034     string response;
00035     
00036     response += "  <imu>\r\n";
00037     response += "    <acceleration>\r\n";
00038     response += "      <x><float>"+float2String(imu.readAccelerationX())+"</float></x>\r\n";
00039     response += "      <y><float>"+float2String(imu.readAccelerationY())+"</float></y>\r\n";
00040     response += "      <z><float>"+float2String(imu.readAccelerationZ())+"</float></z>\r\n";
00041     response += "    </acceleration>\r\n";
00042     response += "    <gyro>\r\n";
00043     response += "      <x><float>"+float2String(imu.readGyroX())+"</float></x>\r\n";
00044     response += "      <y><float>"+float2String(imu.readGyroY())+"</float></y>\r\n";
00045     response += "      <z><float>"+float2String(imu.readGyroZ())+"</float></z>\r\n";
00046     response += "    </gyro>\r\n";
00047     response += "    <magnetometer>\r\n";
00048     response += "      <x><float>"+float2String(imu.readMagnetometerX())+"</float></x>\r\n";
00049     response += "      <y><float>"+float2String(imu.readMagnetometerY())+"</float></y>\r\n";
00050     response += "      <z><float>"+float2String(imu.readMagnetometerZ())+"</float></z>\r\n";
00051     response += "    </magnetometer>\r\n";
00052 
00053     response += "  </imu>\r\n";
00054     
00055     return response;
00056 }
00057