Parcourir la source

Increase averaging time + keep a consistent update timing

- Check the time before sleeping to make sure it keeps on a consistent
schedule.
- Increase to a 2000ms averaging so the motor has time to spin down
without breaking the gearing.
Jason Tarka il y a 4 ans
Parent
commit
e4d5c416c5
2 fichiers modifiés avec 9 ajouts et 6 suppressions
  1. 8 5
      03_main.ino
  2. 1 1
      rc_receiver_driving.ino

+ 8 - 5
03_main.ino

@@ -1,7 +1,12 @@
 char outputStr[100];
 int16_t averages[AVG_PERIODS];
 
+const int throtDeadHigh = THROT_NONE + THROT_DEAD_ZONE,
+	throtDeadLow = THROT_NONE - THROT_DEAD_ZONE;
+
 void loop() {
+	const long startTime = millis();
+	
 #ifdef SCAN_MODE
 	for(byte ch = LOW_CHANNEL; ch <= NUM_CHANNELS; ch++) {
 		int val = channelVals[ch];
@@ -24,12 +29,10 @@ void loop() {
 	analogWrite(LEFT_MOTOR_PIN, throttle > 0 ? throttle : -throttle);
 #endif
 
-	delay(SCAN_DELAY);
+	const long now = millis();
+	delay(SCAN_DELAY + (startTime - now));
 }
 
-const int throtDeadHigh = THROT_NONE + THROT_DEAD_ZONE,
-	throtDeadLow = THROT_NONE - THROT_DEAD_ZONE;
-
 int getThrottle() {
 	int val = channelVals[THROT_CHANNEL];
 
@@ -75,7 +78,7 @@ int restrictThrottlePower(int throttle) {
 }
 
 int getAverage(const int throttle) {
-	int total = throttle;
+	long long total = throttle;
 	for(byte i = 0; i < AVG_PERIODS-1; i++) {
 		total += averages[i];
 		averages[i] = averages[i+1];

+ 1 - 1
rc_receiver_driving.ino

@@ -7,7 +7,7 @@
 ///////////////////////
 
 #define SCAN_DELAY 10 // Delay between updates in ms
-#define AVG_OVER_MILLIS 200 // Number of milliseconds to average the speed over
+#define AVG_OVER_MILLIS 2000 // Number of milliseconds to average the speed over
 #define AVG_PERIODS AVG_OVER_MILLIS/SCAN_DELAY // Number of periods to average the speed over (reduces random variances)
 
 /////////////////////////////////////////////////////////////////////////