Using Driveworks for gps and ipc functions

I am a new hand in driveworks development work,so i try to begin with driveworks samples.I want to implement the function that can read gps information and send it to the client port.These are my codes(almost the combination of sample_gps_logger and sample_ipc_socketclientserver):

#include
#include <signal.h>
#include
#include
#include <stdio.h>
#include <stdlib.h>
#include

#ifdef LINUX
#include <execinfo.h>
#include <unistd.h>
#endif

#include
#include
#include
#include

#include
#include
#include <condition_variable>
#include

#include <framework/ProgramArguments.hpp>
#include <framework/DataPath.hpp>
#include <framework/Log.hpp>
#include <framework/Checks.hpp>
// CORE
#include <dw/core/Logger.h>
#include <dw/core/Context.h>
#include <dw/core/VersionCurrent.h>
#include <dw/ipc/SocketClientServer.h>
// HAL
#include <dw/sensors/Sensors.h>
#include <dw/sensors/gps/GPS.h>
//for json communication
// for zmq:
#include <zmq.h>
//#include <zmq_api.h>
// for json:
#include <jsoncpp/json/json.h>
//------------------------------------------------------------------------------
// Variables
//------------------------------------------------------------------------------
static bool gRun = true;

//------------------------------------------------------------------------------
void sig_int_handler(int)
{
gRun = false;
}
//communication-----------------------------
dwStatus runServer(ProgramArguments const &arguments, dwContextHandle_t ctx)
{
auto status = DW_FAILURE;

auto port = static_cast<uint16_t>(std::stoul(arguments.get("port")));

auto socketServer = dwSocketServerHandle_t{DW_NULL_HANDLE};
CHECK_DW_ERROR(dwSocketServer_initialize(&socketServer, port, 2, ctx));

// accept two connections (use two connections for illustration,
// a single connection can also be used bi-directionally)
auto socketConnectionRead  = dwSocketConnectionHandle_t{DW_NULL_HANDLE},
     socketConnectionWrite = dwSocketConnectionHandle_t{DW_NULL_HANDLE};
do {
    status = dwSocketServer_accept(&socketConnectionRead, 10000, socketServer);
} while (status == DW_TIME_OUT);
do {
    status = dwSocketServer_accept(&socketConnectionWrite, 10000, socketServer);
} while (status == DW_TIME_OUT);

if (status != DW_FAILURE) {
    while (g_run) {
        size_t data;
        auto size = sizeof(decltype(data));

        // receive data
        std::this_thread::sleep_for(std::chrono::milliseconds(std::rand() % 500));
        if ((status = dwSocketConnection_recv(reinterpret_cast<uint8_t *>(&data), &size,
                                              socketConnectionRead)) == DW_END_OF_STREAM)
            break;
        CHECK_DW_ERROR(status);

        if (size != sizeof(decltype(data)))
            break;

        std::cout << "Socket Server received " << data << std::endl;

        // send data back
        std::this_thread::sleep_for(std::chrono::milliseconds(std::rand() % 500));
        if ((status = dwSocketConnection_send(reinterpret_cast<uint8_t *>(&data), &size,
                                              socketConnectionWrite)) == DW_END_OF_STREAM)
            break;
        CHECK_DW_ERROR(status);

        if (size != sizeof(decltype(data)))
            break;

        std::cout << "Socket Server send " << data << std::endl;
    }
}

CHECK_DW_ERROR(dwSocketConnection_release(&socketConnectionWrite));
CHECK_DW_ERROR(dwSocketConnection_release(&socketConnectionRead));
CHECK_DW_ERROR(dwSocketServer_release(&socketServer));

return DW_SUCCESS;

}

dwStatus runClient(ProgramArguments const &arguments, dwContextHandle_t ctx)
{
auto status = DW_FAILURE;

auto ip   = arguments.get("ip");
auto port = static_cast<uint16_t>(std::stoul(arguments.get("port")));

auto socketClient = dwSocketClientHandle_t{DW_NULL_HANDLE};
CHECK_DW_ERROR(dwSocketClient_initialize(&socketClient, 2, ctx));

// connect two connections (use two connections for illustration,
// a single connection can also be used bi-directionally)
auto socketConnectionWrite = dwSocketConnectionHandle_t{DW_NULL_HANDLE},
     socketConnectionRead  = dwSocketConnectionHandle_t{DW_NULL_HANDLE};
do {
    status = dwSocketClient_connect(&socketConnectionWrite, ip.c_str(), port, 10000, socketClient);
} while (status == DW_TIME_OUT);
do {
    status = dwSocketClient_connect(&socketConnectionRead, ip.c_str(), port, 10000, socketClient);
} while (status == DW_TIME_OUT);

if (status != DW_FAILURE) {
    while (g_run) {
        // send some data
        static size_t dataRef = 0;
        ++dataRef;
        decltype(dataRef) data;
        auto size = sizeof(decltype(data));

        // send data
        std::this_thread::sleep_for(std::chrono::milliseconds(std::frame.latitude));
        if ((status = dwSocketConnection_send(reinterpret_cast<uint8_t *>(&dataRef), &size,
                                              socketConnectionWrite)) == DW_END_OF_STREAM)
            break;
        CHECK_DW_ERROR(status);

        if (size != sizeof(decltype(data)))
            break;

        std::cout << "Socket Client send " << dataRef << std::endl;

        // receive data
        std::this_thread::sleep_for(std::chrono::milliseconds(std::frame.latitude));
        if ((status = dwSocketConnection_recv(reinterpret_cast<uint8_t *>(&data), &size,
                                              socketConnectionRead)) == DW_END_OF_STREAM)
            break;
        CHECK_DW_ERROR(status);

        if (size != sizeof(decltype(data)))
            break;

        std::cout << "Socket Client received " << data << std::endl;
    }
}

CHECK_DW_ERROR(dwSocketConnection_release(&socketConnectionWrite));
CHECK_DW_ERROR(dwSocketConnection_release(&socketConnectionRead));
CHECK_DW_ERROR(dwSocketClient_release(&socketClient));

return DW_SUCCESS;

}
//------------------------------------------------------------------------------
int main(int argc, const char **argv)
{
#ifndef WINDOWS
struct sigaction action = {};
action.sa_handler = sig_int_handler;

sigaction(SIGHUP, &action, NULL);  // controlling terminal closed, Ctrl-D
sigaction(SIGINT, &action, NULL);  // Ctrl-C
sigaction(SIGQUIT, &action, NULL); // Ctrl-\, clean quit with core dump
sigaction(SIGABRT, &action, NULL); // abort() called.
sigaction(SIGTERM, &action, NULL); // kill command

#endif

gRun = true;

ProgramArguments arguments(
    {
        ProgramArguments::Option_t("driver", "gps.virtual"),
        ProgramArguments::Option_t("params", (std::string("file=") +
            DataPath::get() + "/samples/sensors/gps/1.gps").c_str())
    });

if (!arguments.parse(argc, argv) || (!arguments.has("driver") && !arguments.has("params"))) {
    std::cout << "Usage: " << argv[0] << std::endl;
    std::cout << "\t--driver=gps.virtual \t\t\t: one of the available GPS drivers "
              << "(see sample_sensors_info)\n";
    std::cout << "\t--params=file=file.gps,arg2=value \t: comma separated "
              << "key=value parameters for the sensor "
              << "(see sample_sensor_info for a set of supported parameters)\n";

    return -1;
}
ProgramArguments arguments2(
{
     ProgramArguments::Option_t("role", "server", "client or server"),
     ProgramArguments::Option_t("ip", "127.0.0.1", "The server IP the client connects to"),
     ProgramArguments::Option_t("port", "49252","The port the server will listen on / the client will connect to")});

if (!arguments2.parse(argc, argv))
    exit(-1); // Exit if not all require arguments2 are provided
else
    std::cout << "Program Arguments2:\n" << arguments2.printList() << std::endl;

dwContextHandle_t sdk   = DW_NULL_HANDLE;
dwSALHandle_t hal       = DW_NULL_HANDLE;

// create a Logger to log to console
// we keep the ownership of the logger at the application level
dwLogger_initialize(getConsoleLoggerCallback(true));
dwLogger_setLogLevel(DW_LOG_VERBOSE);

// instantiate Driveworks SDK context
dwContextParameters sdkParams = {};

dwInitialize(&sdk, DW_VERSION, &sdkParams);

// create HAL module of the SDK
dwSAL_initialize(&hal, sdk);

// open same GPS sensor twice, to demonstrate capability of sensor data sharing
dwSensorHandle_t gpsSensor[2] = {DW_NULL_HANDLE, DW_NULL_HANDLE};
for (int32_t i=0; i < 2; i++)
{
    dwSensorParams params;
    std::string parameterString = arguments.get("params");
    params.parameters           = parameterString.c_str();
    params.protocol = arguments.get("driver").c_str();
    if (dwSAL_createSensor(&gpsSensor[i], params, hal) != DW_SUCCESS) {
        std::cout << "Cannot create sensor " << params.protocol
                  << " with " << params.parameters << std::endl;

        dwSAL_release(&hal);
        dwRelease(&sdk);
        dwLogger_release();

        return -1;
    }
}

gRun = gRun && dwSensor_start(gpsSensor[0]) == DW_SUCCESS;
gRun = gRun && dwSensor_start(gpsSensor[1]) == DW_SUCCESS;

// Message msg;
bool sensorRun[2] = {gRun, gRun};
while (gRun) {

    if (!sensorRun[0] && !sensorRun[1]) break;

    for (int i=0; i < 2; i++)
    {
        if (!sensorRun[i]) continue;

        dwGPSFrame frame;
        dwStatus status = DW_FAILURE;

        status = dwSensorGPS_readFrame(&frame, 50000, gpsSensor[i]);

        if (status == DW_END_OF_STREAM) {
            std::cout << "GPS[" << i << "] end of stream reached" << std::endl;
            sensorRun[i] = false;
            break;
        } else if (status == DW_TIME_OUT)
            continue;

        // log message
        std::cout << "GPS[" << i <<"] - " << frame.timestamp_us;
        if (status != DW_SUCCESS) // msg.is_error)
        {
            std::cout << " ERROR " << dwGetStatusName(status); // msg.frame.id;
        }else {
            std::cout << std::setprecision(10);

            if(frame.flags & DW_GPS_LAT)
                std::cout << " lat: " << frame.latitude;

            if(frame.flags & DW_GPS_LON)
                std::cout << " lon: " << frame.longitude;

            if(frame.flags & DW_GPS_ALT)
                std::cout << " alt: " << frame.altitude;

            if(frame.flags & DW_GPS_COURSE)
                std::cout << " course: " << frame.course;

            if(frame.flags & DW_GPS_SPEED)
                std::cout << " speed: " << frame.speed;

            if(frame.flags & DW_GPS_CLIMB)
                std::cout << " climb: " << frame.climb;

            if(frame.flags & DW_GPS_HDOP)
                std::cout << " hdop: " << frame.hdop;

            if(frame.flags & DW_GPS_VDOP)
                std::cout << " vdop: " << frame.vdop;
        }
        std::cout << std::endl;
    }
}

dwSensor_stop(gpsSensor[0]);
dwSensor_stop(gpsSensor[1]);
dwSAL_releaseSensor(&gpsSensor[0]);
dwSAL_releaseSensor(&gpsSensor[1]);
// Run client / server
auto const role = arguments2.get("role");

auto status = DW_FAILURE;
if (role == "server")
    status = runServer(arguments2, sdk);
else if (role == "client")
    status = runClient(arguments2, sdk);
else {
    std::cerr << "Invalid role parameter '" << role << "' provided (use either 'client' or 'server')"
              << std::endl;
}
dwSAL_release(&hal);
dwRelease(&sdk);
dwLogger_release();
return status == DW_SUCCESS;

}

And this is my CMakelists.txt:

cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(newgpsDriver)
ADD_DEFINITIONS(-std=c++11)
include_directories (/usr/local/driveworks-1.2/sample/src)
set(SOURCES
newgpsDriver.cpp
)

set(LIBRARIES
dw_samples_framework
${Driveworks_LIBRARIES}
)
add_executable(newgpsDriver ${SOURCES})
target_link_libraries(${PROJECT_NAME} PRIVATE ${LIBRARIES})
set_property(TARGET ${PROJECT_NAME} PROPERTY FOLDER “Samples”)

when i make it, it shows an error like this:
/home/nvidia/Downloads/E30/autopilot/gps/test/newgpsDriver.cpp:24:42: fatal error: framework/ProgramArguments.hpp: No such file or directory
compilation terminated.
CMakeFiles/newgpsDriver.dir/build.make:54: recipe for target ‘CMakeFiles/newgpsDriver.dir/newgpsDriver.cpp.o’ failed
make[2]: *** [CMakeFiles/newgpsDriver.dir/newgpsDriver.cpp.o] Error 1
CMakeFiles/Makefile2:60: recipe for target ‘CMakeFiles/newgpsDriver.dir/all’ failed
make[1]: *** [CMakeFiles/newgpsDriver.dir/all] Error 2
Makefile:75: recipe for target ‘all’ failed
make: *** [all] Error 2

I don’t know what happened, so i need your help. Thanks

Hi,

since you are a new developer, please follow those tutorials first subsequently: [url]https://developer.nvidia.com/drive/learn[/url]

From there it becomes clear how to X-compile and get your stuff running.

Thanks.

  • Fabian

Thanks,i’ll try to learn from the website you mentioned

Hi,

that’s great! Let me know where you are struggling and what you expect as next tutorials, so I might focus on these in the next weeks.

  • Fabian

Hello! Is it possible to use the framework (Log.hpp and Checks.hpp for example) in a project structure which I set up through the host compilation tutorial? I want to be able to do #include <framework/Checks.hpp> in my files like you have done in your samples.

Thanks in advance.