Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.18 KB | None | 0 0
  1. /**
  2.  * proxy - Sample application to encapsulate HW/SW interfacing with embedded systems.
  3.  * Copyright (C) 2012 - 2015 Christian Berger
  4.  *
  5.  * This program is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU General Public License
  7.  * as published by the Free Software Foundation; either version 2
  8.  * of the License, or (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  18.  */
  19.  
  20. #include <ctype.h>
  21. #include <cstring>
  22. #include <cmath>
  23.  
  24. #include "core/base/KeyValueConfiguration.h"
  25. #include "core/data/Container.h"
  26. #include "core/data/TimeStamp.h"
  27. #include "OpenCVCamera.h"
  28. #include "core/data/control/VehicleControl.h"
  29. #include "core/data/environment/VehicleData.h"
  30. #include "GeneratedHeaders_Data.h"
  31.  
  32. #include "Proxy.h"
  33.  
  34.  // IMPORT FOR SERIAL
  35. #include <string>
  36. #include <iostream>
  37. #include <cstdio>
  38. #include <unistd.h>
  39. #include "serial/serial.h"
  40.  
  41. // IMPORT FOR NANO
  42. #include <stdio.h>
  43. #include <pb_encode.h>
  44. #include <pb_decode.h>
  45. #include "message.pb.h"
  46.  
  47.  // USING FOR SERIAL
  48.  using std::string;
  49.  using std::exception;
  50.  using std::cout;
  51.  using std::cerr;
  52.  using std::endl;
  53.  using std::vector;
  54.  
  55.  namespace msv {
  56.  
  57.     using namespace std;
  58.     using namespace core::base;
  59.     using namespace core::data;
  60.     using namespace core::data::control;
  61.     using namespace core::data::environment;
  62.     using namespace tools::recorder;
  63.  
  64.     Proxy::Proxy(const int32_t &argc, char **argv) :
  65.     ConferenceClientModule(argc, argv, "proxy"),
  66.     m_recorder(NULL),
  67.     m_camera(NULL)
  68.     {}
  69.  
  70.     Proxy::~Proxy() {
  71.     }
  72.  
  73.     void Proxy::setUp() {
  74.         // This method will be call automatically _before_ running body().
  75.         if (getFrequency() < 20) {
  76.             cerr << endl << endl << "Proxy: WARNING! Running proxy with a LOW frequency (consequence: data updates are too seldom and will influence your algorithms in a negative manner!) --> suggestions: --freq=20 or higher! Current frequency: " << getFrequency() << " Hz." << endl << endl << endl;
  77.         }
  78.  
  79.  
  80.         // Get configuration data.
  81.         KeyValueConfiguration kv = getKeyValueConfiguration();
  82.  
  83.         // Create built-in recorder.
  84.         const bool useRecorder = kv.getValue<uint32_t>("proxy.useRecorder") == 1;
  85.         if (useRecorder) {
  86.             // URL for storing containers.
  87.             stringstream recordingURL;
  88.             recordingURL << "file://" << "proxy_" << TimeStamp().getYYYYMMDD_HHMMSS() << ".rec";
  89.             // Size of memory segments.
  90.             const uint32_t MEMORY_SEGMENT_SIZE = getKeyValueConfiguration().getValue<uint32_t>("global.buffer.memorySegmentSize");
  91.             // Number of memory segments.
  92.             const uint32_t NUMBER_OF_SEGMENTS = getKeyValueConfiguration().getValue<uint32_t>("global.buffer.numberOfMemorySegments");
  93.             // Run recorder in asynchronous mode to allow real-time recording in background.
  94.             const bool THREADING = true;
  95.  
  96.             m_recorder = new Recorder(recordingURL.str(), MEMORY_SEGMENT_SIZE, NUMBER_OF_SEGMENTS, THREADING);
  97.         }
  98.  
  99.         // Create the camera grabber.
  100.         const string NAME = getKeyValueConfiguration().getValue<string>("proxy.camera.name");
  101.         string TYPE = getKeyValueConfiguration().getValue<string>("proxy.camera.type");
  102.         std::transform(TYPE.begin(), TYPE.end(), TYPE.begin(), ::tolower);
  103.         const uint32_t ID = getKeyValueConfiguration().getValue<uint32_t>("proxy.camera.id");
  104.         const uint32_t WIDTH = getKeyValueConfiguration().getValue<uint32_t>("proxy.camera.width");
  105.         const uint32_t HEIGHT = getKeyValueConfiguration().getValue<uint32_t>("proxy.camera.height");
  106.         const uint32_t BPP = getKeyValueConfiguration().getValue<uint32_t>("proxy.camera.bpp");
  107.  
  108.         if (TYPE.compare("opencv") == 0) {
  109.             m_camera = new OpenCVCamera(NAME, ID, WIDTH, HEIGHT, BPP);
  110.         }
  111.  
  112.         if (m_camera == NULL) {
  113.             cerr << "No valid camera type defined." << endl;
  114.         }
  115.     }
  116.  
  117.     void Proxy::tearDown() {
  118.         // This method will be call automatically _after_ return from body().
  119.         OPENDAVINCI_CORE_DELETE_POINTER(m_recorder);
  120.         OPENDAVINCI_CORE_DELETE_POINTER(m_camera);
  121.     }
  122.  
  123.     void Proxy::distribute(Container c) {
  124.         // Store data to recorder.
  125.         if (m_recorder != NULL) {
  126.             // Time stamp data before storing.
  127.             c.setReceivedTimeStamp(TimeStamp());
  128.             m_recorder->store(c);
  129.         }
  130.  
  131.         // Share data.
  132.         getConference().send(c);
  133.     }
  134.  
  135.     double usCentre;
  136.     double usRight;
  137.     double irRight_one;
  138.     double irRight_two;
  139.     double irRear;
  140.     int wheelTicks;
  141.     string port("/dev/ttyACM0");
  142.     unsigned long baud = 38400; //19200; //9600;
  143.     serial::Serial my_serial(port, baud, serial::Timeout::simpleTimeout(100)); //simpleTimeout(1000)
  144.    
  145.     // This method will do the main data processing job.
  146.     ModuleState::MODULE_EXITCODE Proxy::body() {
  147.         uint32_t captureCounter = 0;        
  148.         int numOfErrors = 0;    
  149.         string mark = "=";
  150.         while (getModuleState() == ModuleState::RUNNING) {
  151.             // Capture frame.
  152.             if (m_camera != NULL) {
  153.                 core::data::image::SharedImage si = m_camera->capture();
  154.  
  155.                 Container c(Container::SHARED_IMAGE, si);
  156.                 //distribute(c);
  157.                 captureCounter++;
  158.             }
  159.  
  160.             // TODO: Here, you need to implement the data links to the embedded system
  161.             // to read data from IR/US.
  162.             // DECODE
  163.             //read serial when available for sensor data sent fto Arduino
  164.             if(my_serial.available()){
  165.             ArduinoToOdroid messageIn;
  166.             uint8_t bufferIn[64];
  167.             string result;
  168.             //string result = my_serial.read(27);
  169.             my_serial.readline(result, 28, mark);  
  170.             //cerr << "length: " << result << endl;            
  171.             for(int i = 0; i < 27; i++){
  172.                 bufferIn[i] = result[i];
  173.             }
  174.             pb_istream_t streamIn = pb_istream_from_buffer(bufferIn, 27);
  175.             bool read_status = pb_decode(&streamIn, ArduinoToOdroid_fields, &messageIn);
  176.             //cerr << "Test " << endl;
  177.             if(!read_status){
  178.                 numOfErrors++;
  179.                 cerr << "Error number: " << numOfErrors << endl;
  180.             }
  181.             else{
  182.                 cerr << "Ultrasound centre: " << messageIn.usCentre << endl;
  183.                 cerr << "Ultrasound right: " << messageIn.usRight << endl;
  184.                 cerr << "Infrared right 1: " << messageIn.irRight_one << endl;
  185.                 cerr << "Infrared right 2: " << messageIn.irRight_two << endl;
  186.                 cerr << "Infrared rear: " << messageIn.irRear << endl;
  187.                 cerr << "Wheel ticks: " << messageIn.wheelTicks << endl;
  188.                 cerr << endl << endl;
  189.             }  
  190.             }            
  191.            
  192.             // Encode
  193.             OdroidToArduino messageOut;
  194.             messageOut.spd = 70;
  195.             messageOut.angle = 24;
  196.             uint8_t bufferOut[64];
  197.             pb_ostream_t streamOut = pb_ostream_from_buffer((uint8_t*)bufferOut, sizeof(bufferOut));
  198.             bool sendStatus = pb_encode(&streamOut, OdroidToArduino_fields, &messageOut);
  199.             int length = streamOut.bytes_written;
  200.             if (sendStatus) {              
  201.                 for (int i = 0; i < length; i++) {
  202.                     my_serial.write(&bufferOut[i], 1);
  203.                 }              
  204.                 my_serial.write(mark);
  205.                 //cerr << "length of =:"<<mark.length() << endl;
  206.             }  else {
  207.                 //cerr << "failed" << endl;
  208.             }
  209.            
  210.         }
  211.  
  212.         cout << "Proxy: Captured " << captureCounter << " frames." << endl;
  213.  
  214.         return ModuleState::OKAY;
  215.     }
  216. } // msv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement