Skip to content
Snippets Groups Projects
TerminalControl.cpp 2.95 KiB
Newer Older
//
// Created by philipp on 26.04.18.
//

#include "../include/VeloxProtocolLib/Connection.h"
#include <iostream>
#include <boost/algorithm/string.hpp>
Hoop77's avatar
-  
Hoop77 committed
#include "USS_SRF02.h"
Hoop77's avatar
Hoop77 committed
constexpr float MAX_SPEED = 1.0f;

int main(int argc, char ** argv)
{
    using namespace veloxProtocol;
    networking::Networking net;
Hoop77's avatar
-  
Hoop77 committed
    std::atomic<bool> monitorSTM{false};
    std::atomic<bool> monitorUSS{false};
    std::atomic<bool> running{true};
    std::mutex outputMutex;
    auto conn = Connection::create(net);
    conn->open(
        "/dev/ttySAC0",
        [&]
        {
Hoop77's avatar
-  
Hoop77 committed
            if (!monitorSTM)
Hoop77's avatar
-  
Hoop77 committed
            std::lock_guard<std::mutex> lock{outputMutex};
            std::cout << "[STM Update]\nSpeed=" << conn->getMeasuredSpeed().get()
                      << "\nSteering Angle: " << conn->getMeasuredSteeringAngle().get() << "\n\n";
        },
        []
        { std::cout << "Connection closed!\n"; });

Hoop77's avatar
-  
Hoop77 committed
    std::thread ussThread{
        [&]
        {
Hoop77's avatar
-  
Hoop77 committed
            USS_SRF02 uss{USS_SRF02::DEVICE_ADDRESS1};
Hoop77's avatar
-  
Hoop77 committed

Hoop77's avatar
-  
Hoop77 committed
            while (running)
            {
                int distance = uss.getDistance();;
Hoop77's avatar
-  
Hoop77 committed
                if (distance > 20)
                    conn->setSpeed(0);
Hoop77's avatar
-  
Hoop77 committed

Hoop77's avatar
-  
Hoop77 committed
                if (!monitorUSS)
Hoop77's avatar
-  
Hoop77 committed
                    continue;
Hoop77's avatar
-  
Hoop77 committed

Hoop77's avatar
-  
Hoop77 committed
                std::lock_guard<std::mutex> lock{outputMutex};
                std::cout << "[USS Update]\nDistance=" << distance << "\n\n";
Hoop77's avatar
-  
Hoop77 committed
            }
        }};

    std::cout << "\n\nMANUAL\n"
              << "------\n"
              << "Enter 'stm' to enable/disable monitoring for STM data.\n"
              << "Enter 'uss' to enable/disable monitoring for USS data.\n"
              << "Enter 'q' to quit.\n"
              << "Enter 's <float> t' to set speed\n"
              << "Enter 'a <float>' to set angle\n"
              << "\n\n";

    for (std::string in; in != "q"; std::getline(std::cin, in))
    {
        if (in.empty())
            continue;
Hoop77's avatar
-  
Hoop77 committed

Hoop77's avatar
-  
Hoop77 committed
        if (in == "stm")
        {
            monitorSTM = !monitorSTM;
            continue;
        }
        else if (in == "uss")
Hoop77's avatar
-  
Hoop77 committed
        {
Hoop77's avatar
-  
Hoop77 committed
            monitorUSS = !monitorUSS;
Hoop77's avatar
-  
Hoop77 committed
            continue;
        }

        std::vector<std::string> split;
        boost::split(split, in, [](char c)
        { return c == ' '; });

        std::string cmd = split.at(0);
Hoop77's avatar
-  
Hoop77 committed
        float value = std::stof(split.at(1));

        if (cmd == "a")
        {
            std::cout << "Setting steering angle to: " << value << "\n";
            conn->setSteeringAngle(value);
        }
        else if (cmd == "s")
        {
            std::cout << "Setting speed to: " << value << "\n";
Hoop77's avatar
Hoop77 committed
            if (std::abs(value) > MAX_SPEED)
            {
                value = value >= 0 ? MAX_SPEED : -MAX_SPEED;
                std::cout << "WARNING: Speed set to 1.0 due to risk of collision!\n";
            }
            conn->setSpeed(value);
        }
        else std::cout << "Error: Unknown command!\n";
    }

Hoop77's avatar
-  
Hoop77 committed
    running = false;