From 6519d941e35482d9b20cbabf21681b0d5e87df67 Mon Sep 17 00:00:00 2001
From: Jake Drahos <j@kedrahos.com>
Date: Mon, 28 Nov 2016 12:54:52 -0600
Subject: [PATCH] Monitor of trackerdata finally works

Added nanosleep to make rate work right, also
switched from -t for total time to -c for count
---
 groundStation/src/backend/backend.c |  5 ++-
 groundStation/src/cli/cli_monitor.c | 60 +++++++++++++++--------------
 2 files changed, 36 insertions(+), 29 deletions(-)

diff --git a/groundStation/src/backend/backend.c b/groundStation/src/backend/backend.c
index 0803aa983..7bc7969ab 100644
--- a/groundStation/src/backend/backend.c
+++ b/groundStation/src/backend/backend.c
@@ -635,7 +635,10 @@ static void client_recv(int fd) {
 			if (strncmp(buffer, TD_MAGIC, strlen(TD_MAGIC)) == 0) {
 				/* Request for tracker data */
 				struct ucart_vrpn_TrackerData td;
-				if (ucart_vrpn_tracker_getData(tracker, &td)) {
+				if (tracker == NULL) {
+					char * dummy = TD_MAGIC " 1.0 2.0 3.0 4.0 5.0 6.0\n";
+					write(fd, dummy, strlen(dummy));
+				}else if (ucart_vrpn_tracker_getData(tracker, &td)) {
 					write(fd, TD_MAGIC " ERROR\n", strlen(TD_MAGIC " ERROR\n"));
 				} else { 
 					/* more than sufficient buffer */
diff --git a/groundStation/src/cli/cli_monitor.c b/groundStation/src/cli/cli_monitor.c
index aef07810b..ea5a7b8c7 100644
--- a/groundStation/src/cli/cli_monitor.c
+++ b/groundStation/src/cli/cli_monitor.c
@@ -14,20 +14,17 @@ static void timespec_diff(struct timespec *start,  struct timespec *result);
 
 int cli_monitor(struct backend_conn * conn,	int argc, char **argv) {
 	int c, result;
-	int timeFlag = 0;
+	int countFlag = 0;
 
-	static struct timespec startTime;
-	static struct timespec elapsedTime;
-	static struct timespec lastChecked;
-	static int nsecs, rate = 10;
-	static int forever = 0;
+	int count, rate = 10;
+	int forever = 0;
 
 
- 	while ((c = getopt(argc, argv, "ft:r:")) != -1) {
+ 	while ((c = getopt(argc, argv, "fc:r:")) != -1) {
 		switch(c) {
-			case 't' :
-				nsecs = atoi(optarg);
-				timeFlag = 1;
+			case 'c' :
+				count = atoi(optarg);
+				countFlag = 1;
 				break;
 			case 'r' :
 				rate = atoi(optarg) + 1;
@@ -42,23 +39,30 @@ int cli_monitor(struct backend_conn * conn,	int argc, char **argv) {
 
 	if (forever) {
 		for (;;) {
+			struct timespec req;
+			if (rate == 1) {
+				req.tv_sec = 1;
+				req.tv_nsec = 0;
+			} else { 
+				req.tv_sec = 0;
+				req.tv_nsec = 1000000000 / rate;
+			}
+			nanosleep(&req, NULL);
 			monitor(conn);
 		}
-	} else if (timeFlag) {
-		clock_gettime(CLOCK_REALTIME, &startTime);
-		clock_gettime(CLOCK_REALTIME, &lastChecked);
-		while((result = monitor(conn)) == 0) {
-			timespec_diff(&startTime, &elapsedTime);
-			if(elapsedTime.tv_sec > nsecs) {
-				break;
-			}
-			while(1) {
-				timespec_diff(&lastChecked, &elapsedTime);
-				if(elapsedTime.tv_nsec >= (long) ((SECOND_IN_NANO * 2) / rate)) {
-					clock_gettime(CLOCK_REALTIME, &lastChecked);
-					break;
-				}
+	} else if (countFlag) {
+		for (int i = 0; i < count; i++) {
+			result = monitor(conn);
+
+			struct timespec req;
+			if (rate == 1) {
+				req.tv_sec = 1;
+				req.tv_nsec = 0;
+			} else { 
+				req.tv_sec = 0;
+				req.tv_nsec = 1000000000 / rate;
 			}
+			nanosleep(&req, NULL);
 		}
 	} else {
 		return monitor(conn);
@@ -81,14 +85,14 @@ int monitor(struct backend_conn * conn) {
 	printf("\033[2J");
 	printf("STATUS: NA\n");
 	printf("CTRLR :\tP\tR\tY\tP_V\tR_V\tY_V\tH\tLAT\tLON\n");
-	printf("  P   :\t%7lf\t%7lf\t%7lf\t%7lf\t%7lf\t%7lf\t%7lf\t%7lf\t%7lf\n",
+	printf("  P   :\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\n",
 			0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
-	printf("  I   :\t%7lf\t%7lf\t%7lf\t%7lf\t%7lf\t%7lf\t%7lf\t%7lf\t%7lf\n",
+	printf("  I   :\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\n",
 			0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
-	printf("  D   :\t%7lf\t%7lf\t%7lf\t%7lf\t%7lf\t%7lf\t%7lf\t%7lf\t%7lf\n",
+	printf("  D   :\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\n",
 			0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
 	printf("PosAtt:\tH\tLAT\tLON\tP\tR\tY\n");
-	printf("      :\t%7lf\t%7lf\t%7lf\t%7lf\t%7lf\t%7lf\n",
+	printf("      :\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\t%6.3lf\n",
 			td.height, td.lateral, td.longitudinal,
 			td.pitch, td.roll, td.yaw);
 
-- 
GitLab