Commit 44727d19 authored by Svetlana Pavlitskaya's avatar Svetlana Pavlitskaya

Controlling torcs car from TorcsClient

parent a29ff880
......@@ -5,8 +5,6 @@
#include <FreeImage.h>
#include <test_basicPortsLoop.h>
constexpr auto IMAGE_WIDTH = 640;
constexpr auto IMAGE_HEIGHT = 480;
constexpr auto IMAGE_CHANNELS = 3;
......@@ -46,6 +44,43 @@ struct shared_use_st
double brakeCmd = 0.0;
};
void writeScreenshot(shared_use_st* shared, unsigned int& num)
{
std::cout << "Read image." << std::endl;
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;
}
bits += 3*IMAGE_WIDTH;
}
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;
shared->control = 0;
shared->written = 0;
}
int main()
{
std::cout << "TORCS Client!" << std::endl;
......@@ -75,7 +110,7 @@ int main()
shared = (struct shared_use_st*)shm;
shared->written = 0;
shared->control = 0;
shared->pause = 1;
shared->pause = 0;
shared->fast = 0.0;
shared->dist_L = 0.0;
......@@ -104,37 +139,28 @@ int main()
unsigned num = 0;
while (1) {
if (shared->control == 1) {
std::cout << "Control == 1!" << std::endl;
shared->control = 1;
shared->accelCmd = 10.0;
shared->steerCmd = 1.0;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
} else
{
std::cout << "Control == 0!" << std::endl;
shared->control = 1;
shared->accelCmd = 10.0;
shared->steerCmd = 1.0;
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
if (shared->written == 1) {
std::cout << "Read image." << std::endl;
FIBITMAP* newShot = FreeImage_Allocate(IMAGE_WIDTH, IMAGE_HEIGHT, 24);
if (!newShot)
{
std::cerr << "Failed to allocate freeimage file." << std::endl;
FreeImage_Unload(newShot);
break;
}
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;
}
bits += 3*IMAGE_WIDTH;
}
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(2s);
//writeScreenshot(shared, num);
std::cout << "Speed: " << shared->speed << std::endl;
std::cout << "Steer: " << shared->angle << std::endl;
std::cout << "ToMiddle: " << shared->toMiddle << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
num++;
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment