torcsclient.cpp 3.06 KB
Newer Older
Svetlana's avatar
Svetlana committed
1 2
#include "torcsclient.h"

3 4 5
#include <iostream>
#include <chrono>
#include <thread>
Svetlana's avatar
Svetlana committed
6
#include <cstring>
7
#include <sys/shm.h>
Svetlana's avatar
Svetlana committed
8
#include <assert.h>
9

Svetlana's avatar
Svetlana committed
10
using namespace torcs;
11

Svetlana's avatar
Svetlana committed
12
TorcsClient::TorcsClient()
13 14 15 16 17 18 19 20 21 22
{
    std::cout << "TORCS Client!" << std::endl;

    void *shm = NULL;
    int shmid;

    shmid = shmget((key_t)4567, sizeof(struct shared_use_st), 0666);
    if(shmid < 0)
    {
        std::cerr << "Failed to get shared memory!" << std::endl;
Svetlana's avatar
Svetlana committed
23
	return;
24 25 26 27 28 29
    }

    shm = shmat(shmid, 0, 0);
    if(shm == (void*)-1)
    {
        std::cerr << "Failed to shmat()!" << std::endl;
Svetlana's avatar
Svetlana committed
30
        return;
31
    }
Svetlana's avatar
Svetlana committed
32 33
    
    connected = true;
34 35 36 37 38 39

    std::cout << "Started shared memory at " << std::hex << shm << std::endl;

    shared = (struct shared_use_st*)shm;
    shared->written = 0;
    shared->control = 0;
40
    shared->pause = 0;
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    shared->fast = 0.0;

    shared->dist_L = 0.0;
    shared->dist_R = 0.0;

    shared->toMarking_L = 0.0;
    shared->toMarking_M = 0.0;
    shared->toMarking_R = 0.0;

    shared->dist_LL = 0.0;
    shared->dist_MM = 0.0;
    shared->dist_RR = 0.0;

    shared->toMarking_LL = 0.0;
    shared->toMarking_ML = 0.0;
    shared->toMarking_MR = 0.0;
    shared->toMarking_RR = 0.0;

    shared->toMiddle = 0.0;
    shared->angle = 0.0;
    shared->speed = 0.0;

    shared->steerCmd = 0.0;
    shared->accelCmd = 0.0;
    shared->brakeCmd = 0.0;
Svetlana's avatar
Svetlana committed
66
}
67

Svetlana's avatar
Svetlana committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
std::unique_ptr<DataContainer> TorcsClient::getScreenshot()
{
    assert(connected);

    while(shared->written == 0)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    auto result = std::make_unique<DataContainer>();
    memcpy(result->data(), shared->data, result->size());
    shared->written = 0;

    return result;
}

void TorcsClient::sendCommand(Command cmd)
{
    assert(connected);

    while (shared->control == 0) {
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    shared->accelCmd = cmd.accelCmd;
    shared->steerCmd = cmd.steerCmd;
    shared->brakeCmd = cmd.brakeCmd;
    shared->control = 0;
}
97

Svetlana's avatar
Svetlana committed
98 99 100
/*void writeScreenshot(shared_use_st* shared, unsigned int& num)
{
    std::cout << "Read image." << std::endl;
101

Svetlana's avatar
Svetlana committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    FIBITMAP* newShot = FreeImage_Allocate(IMAGE_WIDTH, IMAGE_HEIGHT, 24);
    if (!newShot)
    {
        std::cerr << "Failed to allocate freeimage file." << std::endl;
        FreeImage_Unload(newShot);
        return;
    }

    BYTE *bits = (BYTE*)FreeImage_GetBits(newShot);

    for (int h = 0; h < IMAGE_HEIGHT; h++) {
        BYTE *pixel = (BYTE*)bits;

        for (int w = 0; w < IMAGE_WIDTH; w++) {
            pixel[FI_RGBA_RED] = shared->data[((h)*IMAGE_WIDTH+w)*3+0];
            pixel[FI_RGBA_GREEN] = shared->data[((h)*IMAGE_WIDTH+w)*3+1];
            pixel[FI_RGBA_BLUE] = shared->data[((h)*IMAGE_WIDTH+w)*3+2];
            pixel += 3;
120
        }
Svetlana's avatar
Svetlana committed
121
        bits += 3*IMAGE_WIDTH;
122 123
    }

Svetlana's avatar
Svetlana committed
124 125 126 127 128 129 130
    FreeImage_Save(FIF_BMP, newShot, (std::string("shot-")+std::to_string(num)).c_str());
    FreeImage_Unload(newShot);

    shared->control = 0;
    shared->written = 0;
    std::this_thread::sleep_for(std::chrono::seconds(2));std::cout << "Read image." << std::endl;
}*/
131