Skip to content

Commit

Permalink
added the vel increment: include/graphics.h
Browse files Browse the repository at this point in the history
	wrapped-up task creation:   include/utils.h
	global variable for desired velocity:   include/vehicle.h
	modified:   src/graphics.c
	modified:   src/main.c
	modified:   src/pathplanner.c
	modified:   src/utils.c
  • Loading branch information
ckevar committed Jan 20, 2019
1 parent 080950c commit 6821ee3
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 5 deletions.
3 changes: 3 additions & 0 deletions include/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ void drawNCars(int x);
/* Prints the number of deadline-misses in the interface */
void drawNDmiss();

/* Prints the specified reference velocity of existing cars in the city */
void drawGVel(double vel);

#endif
5 changes: 5 additions & 0 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <types.h>

#include "task.h"
#include "tlmanager.h"

#ifndef UTILS_H
#define UTILS_H
Expand Down Expand Up @@ -71,4 +72,8 @@ void chooseCarDelete(int *r, vehicle_t *c, rt_task_par_t *p);

void checkWhosOut(int *r, vehicle_t *c, rt_task_par_t *p);

void task_createTrafficLight(tl_manager_arg *man_arg, rt_task_par_t *tl_manager_par, int period, int deadline, int prioriti);

void task_createVehicle(vehicle_t *cars, rt_task_par_t *carsPrms, int period, int deadline, int prioriti);

#endif
4 changes: 4 additions & 0 deletions include/vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#define PID_KI 2000.0
#define V_REF 20.0

// VELOCITY BOUNDARIES
#define MAX_VEL_CAR 25
#define MIN_VEL_CAR 0

/***** Real-Time Task *****/
void *vehicle(void *myVehi);
/* drives the car, do the planning
Expand Down
35 changes: 35 additions & 0 deletions src/graphics.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "utils.h"
#include "tlgraphics.h"
#include "vehicle.h"

pthread_mutex_t dmiss_lock;
int deadline_misses;
Expand Down Expand Up @@ -90,18 +91,43 @@ void drawNCars(int x){
50, color, -1);
}

/* Prints the specified reference velocity of existing cars in the city */
void drawGVel(double vel) {
char s[2];
int color;
snprintf(s, sizeof(int), "%d", (int)vel);

if(vel <= (MAX_VEL_CAR / 3))
color = INFO_GREEN;
else if (vel <= (2 * MAX_CARS) / 3)
color = INFO_YEL;
else
color = INFO_RED;

rectfill(screen, (N_BLOCKS_X*BLOCK_W + (N_BLOCKS_X+ 1)*STREET_W) + 50, 248,
(N_BLOCKS_X*BLOCK_W + (N_BLOCKS_X+ 1)*STREET_W) + 70, 260, STAT_BG);
textout_centre_ex(screen, font, s, (N_BLOCKS_X*BLOCK_W + (N_BLOCKS_X+ 1)*STREET_W) + 60,
250, color, -1);

}


/* Sets up the display and interface*/
void initStatDisplay(){
// char* desc, insert, delete;
const char *desc = "WELCOME IN CITY, A SIMULATION PROGRAM THAT AIMS TO SIMULATE A URBAN AREA.";
const char *insert = "TO ADD A NEW CAR, PRESS 'n'.";
const char *delete = "TO DELETE AN EXISTING CAR, PRESS 'd'.";
const char *incVel = "TO INCREASE VELOCITY, PRESS UP KEY";
const char *decVel = "TO DECREASE VELOCITY, PRESS DOWN KEY";
const char *nCars1 = "NUMBER OF";
const char *nCars2 = "CARS";
const char *nCars3 = "IN THE CITY:";
const char *nDmiss1 = "NUMBER OF";
const char *nDmiss2 = "DEADLINE";
const char *nDmiss3 = "MISSES:";
const char *nGVel1 = "GLOBAL";
const char *nGVel2 = "VELOCITY";
int dmiss = 0;
char s[4];

Expand All @@ -114,6 +140,8 @@ void initStatDisplay(){
(N_BLOCKS_Y*BLOCK_W + (N_BLOCKS_Y+1)*STREET_W+15), INFO_COL, -1);
textout_ex(screen, font, insert, 20, (N_BLOCKS_Y*BLOCK_W + (N_BLOCKS_Y+1)*STREET_W+35), INFO_GREEN, -1);
textout_ex(screen, font, delete, 20, (N_BLOCKS_Y*BLOCK_W + (N_BLOCKS_Y+1)*STREET_W+45), INFO_RED, -1);
textout_ex(screen, font, incVel, 20, (N_BLOCKS_Y*BLOCK_W + (N_BLOCKS_Y+1)*STREET_W+55), INFO_RED-1, -1);
textout_ex(screen, font, decVel, 20, (N_BLOCKS_Y*BLOCK_W + (N_BLOCKS_Y+1)*STREET_W+65), INFO_RED-2, -1);

textout_centre_ex(screen, font, nCars1, (N_BLOCKS_X*BLOCK_W + (N_BLOCKS_X+ 1)*STREET_W) + 60,
20, INFO_COL, -1);
Expand All @@ -128,7 +156,14 @@ void initStatDisplay(){
textout_centre_ex(screen, font, nDmiss3, (N_BLOCKS_X*BLOCK_W + (N_BLOCKS_X+ 1)*STREET_W) + 60,
150, INFO_COL, -1);

textout_centre_ex(screen, font, nGVel1, (N_BLOCKS_X*BLOCK_W + (N_BLOCKS_X+ 1)*STREET_W) + 60,
230, INFO_COL, -1);
textout_centre_ex(screen, font, nGVel2, (N_BLOCKS_X*BLOCK_W + (N_BLOCKS_X+ 1)*STREET_W) + 60,
240, INFO_COL, -1);

drawNCars(0);
drawGVel(0);

snprintf(s, sizeof(int), "%d", dmiss);
textout_centre_ex(screen, font, s, (N_BLOCKS_X*BLOCK_W + (N_BLOCKS_X+ 1)*STREET_W) + 60,
160, INFO_GREEN, -1);
Expand Down
22 changes: 20 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ extern pthread_mutex_t screen_lock;
extern pthread_mutex_t dmiss_lock;
extern int deadline_misses;

double Vgr;

int main(int argc, char const *argv[])
{
vehicle_t cars[MAX_CARS];
rt_task_par_t carsPrms[MAX_CARS];

Vgr = V_REF;

char scan;
char carCounter = 0;
char isPressed = 1;
Expand All @@ -35,7 +39,8 @@ int main(int argc, char const *argv[])
tl_manager_arg man_arg;
man_arg.tl_matrix = tl_matrix;
rt_task_par_t tl_manager_par;
task_create(&tl_init, &tl_manager, NULL, &man_arg, &tl_manager_par, 4000, 1000, 3);
// task_create(&tl_init, &tl_manager, NULL, &man_arg, &tl_manager_par, 4000, 1000, 3);
task_createTrafficLight(&man_arg, &tl_manager_par, 4000, 1000, 3);
/***************************************/

/******* Under Test Vehicle-Task *******/
Expand All @@ -52,7 +57,8 @@ int main(int argc, char const *argv[])
if (scan == KEY_N && carCounter < MAX_CARS) {
readyCar = 0;
while(carsPrms[readyCar].isActive) readyCar++;
task_create(initVehicle, moveVehicle, termVehicle, &cars[readyCar], &carsPrms[readyCar], 40, 40, 2);
task_createVehicle(&cars[readyCar], &carsPrms[readyCar], 40, 40, 2);
// task_create(initVehicle, moveVehicle, termVehicle, &cars[readyCar], &carsPrms[readyCar], 40, 40, 2);
carCounter++;
}

Expand All @@ -63,6 +69,18 @@ int main(int argc, char const *argv[])
carCounter--;
}

/** if UP key is pressed all the vehicle will increase its velocity **/
if(scan == KEY_UP) {
Vgr = (Vgr >= MAX_VEL_CAR) ? MAX_VEL_CAR : Vgr + 1;
drawGVel(Vgr);
}

/** if DOWN key is pressed all the vehicle will decrease its velocity **/
if (scan == KEY_DOWN) {
Vgr = (Vgr <= MIN_VEL_CAR) ? MIN_VEL_CAR : Vgr - 1;
drawGVel(Vgr);
}

drawNCars(carCounter);
}

Expand Down
8 changes: 5 additions & 3 deletions src/pathplanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "vehicle.h"


extern double Vgr;

void keepCertainDistanceFromBlock(vehicle_t *c, const int safeDistance) {
if (c->ds.dsts[RIGHT_DST] > safeDistance) {
if (c->ds.dsts[RIGHT_DST] < SMAX) {
Expand Down Expand Up @@ -233,7 +235,7 @@ void check2turnAround(vehicle_t *c, imfeatures_t *imf){
(imf->stCorner.y[0]/2 + imf->stCorner.y[1]/2)] == BLOCK_COL)
{
c->planner.w0 = fabs(imf->stCorner.y[0] - imf->stCorner.y[1]); // compute x0 to generate a half circle trajectory
printf("y[0] %d, y[1] %d\n", imf->stCorner.y[0], imf->stCorner.y[1]);
// printf("y[0] %d, y[1] %d\n", imf->stCorner.y[0], imf->stCorner.y[1]);
if (c->planner.w0 > (STREET_W - 5.0)) { // 5 is a tolerance
c->planner.w0 /= 2.0; // compute x0 to generate a half circle trajectory
c->planner.w0 -= (imf->stCorner.y[1] - HRES / 2); // compute x0 to generate a half circle trajectory
Expand Down Expand Up @@ -263,7 +265,7 @@ char pathPlanner(vehicle_t *c) {
}
/***********************************/
} else {
c->Vr = V_REF; // it needs to return going again
c->Vr = Vgr; // it needs to return going again

analyzeCameraFrame(c, &imf); // analyzes the current frame captured

Expand All @@ -277,7 +279,7 @@ char pathPlanner(vehicle_t *c) {
}

else
c->Vr = V_REF;
c->Vr = Vgr;
}
}
/************************************/
Expand Down
8 changes: 8 additions & 0 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "utils.h"
#include "tlgraphics.h"
#include "types.h"
#include "vehicle.h"

/* copy int elements from source to destination */
void cpyPnts(int *source, int *destination, const int N) {
Expand Down Expand Up @@ -47,3 +48,10 @@ void checkWhosOut(int *r, vehicle_t *c, rt_task_par_t *p) {
}


void task_createTrafficLight(tl_manager_arg *man_arg, rt_task_par_t *tl_manager_par, int period, int deadline, int prioriti) {
task_create(&tl_init, &tl_manager, NULL, man_arg, tl_manager_par, period, deadline, prioriti);
}

void task_createVehicle(vehicle_t *cars, rt_task_par_t *carsPrms, int period, int deadline, int prioriti){
task_create(initVehicle, moveVehicle, termVehicle, cars, carsPrms, period, deadline, prioriti);
}

0 comments on commit 6821ee3

Please sign in to comment.