Browse Source

Simplified debug logging; no more need for #ifindef

Jason Tarka 4 years ago
parent
commit
61157e7e5b
3 changed files with 45 additions and 35 deletions
  1. 27 0
      02_logging.ino
  2. 17 31
      03_main.ino
  3. 1 4
      rc_receiver_driving.ino

+ 27 - 0
02_logging.ino

@@ -0,0 +1,27 @@
+template <typename T>
+void debug(T val) {
+#ifdef DEBUG
+	Serial.print(val);
+#endif
+}
+
+template <typename T>
+void debugln(T val) {
+#ifdef DEBUG
+	Serial.println(val);
+#endif
+}
+
+template <typename T, typename Y>
+void debugln(T str, Y val) {
+#ifdef DEBUG
+	Serial.print(str);
+	Serial.println(val);
+#endif
+}
+
+void debugln() {
+#ifdef DEBUG
+	Serial.println();
+#endif
+}

+ 17 - 31
03_main.ino

@@ -1,5 +1,5 @@
 char outputStr[100];
-int16_t averages[2][AVG_PERIODS];
+int16_t averages[AVG_PERIODS];
 
 void loop() {
 #ifdef SCAN_MODE
@@ -10,6 +10,9 @@ void loop() {
 	}
 	Serial.println();
 #else
+	debugln();
+	debugln("===================");
+
 	int throttle = getThrottle();
 	throttle = restrictThrottlePower(throttle);
 	throttle = getAverage(throttle);
@@ -29,21 +32,15 @@ const int throtDeadHigh = THROT_NONE + THROT_DEAD_ZONE,
 
 int getThrottle() {
 	int val = channelVals[THROT_CHANNEL];
-#ifndef GRAPH
-	Serial.println();
-	Serial.print("Channel value: ");
-	Serial.print(val);
-	Serial.print("\t");
-#endif
+
+	debug("Channel value: ");
+	debug(val);
+	debug("\t");
 	if(val <= THROT_OFF) {
-#ifndef GRAPH
-		Serial.println("Throttle is off");
-#endif
+		debugln("Throttle is off");
 		return 0;
 	} else if(val < throtDeadHigh && val > throtDeadLow) {
-#ifndef GRAPH
-		Serial.println("Throttle in dead zone");
-#endif
+		debugln("Throttle in dead zone");
 		return 0;
 	}
 	
@@ -53,10 +50,9 @@ int getThrottle() {
 		: map(val, THROT_NONE, THROT_MAX, 0, OUTPUT_RANGE);
 	
 	throttle = constrain(throttle, -OUTPUT_RANGE, OUTPUT_RANGE);
-#ifndef GRAPH
-	Serial.print("Throttle: ");
-	Serial.println(throttle);
-#endif
+
+	debugln("Throttle: ", throttle);
+
 	return throttle;
 }
 
@@ -64,10 +60,7 @@ int restrictThrottlePower(int throttle) {
 	// Reduce the average voltage to something safe for the motor to consume.
 	throttle = (throttle * MAX_POWER_PERCENT) / 100;
 
-#ifndef GRAPH
-	Serial.print("Restricted throttle:\t");
-	Serial.println(throttle);
-#endif
+	debugln("Restricted throttle:\t", throttle);
 
 	// Use the current value of the knob to reduce the speed.
 	int speedKnob = channelVals[SPEED_CHANNEL];
@@ -75,12 +68,8 @@ int restrictThrottlePower(int throttle) {
 	speedKnob = constrain(speedKnob, 0, 100);
 	throttle = (throttle * speedKnob) / 100;
 
-#ifndef GRAPH
-	Serial.print("Speed knob:\t");
-	Serial.println(speedKnob);
-	Serial.print("Speed adjusted throttle:\t");
-	Serial.println(throttle);
-#endif
+	debugln("Speed knob:\t", speedKnob);
+	debugln("Speed adjusted throttle:\t", throttle);
 	
 	return throttle;
 }
@@ -94,10 +83,7 @@ int getAverage(const int throttle) {
 	averages[AVG_PERIODS-1] = throttle;
 	total /= AVG_PERIODS;
 
-#ifndef GRAPH
-	Serial.print("Average adjusted throttle:\t");
-	Serial.println(total);
-#endif
+	debugln("Average adjusted throttle:\t", total);
 
 	return total;
 }

+ 1 - 4
rc_receiver_driving.ino

@@ -1,6 +1,3 @@
-
-//#define SCAN_MODE // If set, scan & output channel values, rather than driving the robot.
-
 /////////////////////////////////////////////////////////////////////////
 // Configuration
 //////////////////////////////////////////////////
@@ -17,7 +14,7 @@
 // Output config
 /////////////////////////////////////////////////////////////////////////
 
-#define GRAPH
+//#define SCAN_MODE // If set, output channel values rather than driving the robot.
 
 /////////////////////////////////////////////////////////////////////////
 // Channel pins