29-09-2026, 09:54
Witam wszystkich. Próbowałem zbudować quadkopter, w którym wykorzystywałem pętlę 4 ms, ale operacja zapisu analogowego zajmuje dwa razy więcej czasu niż powinna,
a wynik wzrasta do 8 ms bez względu na to, co próbuję – nawet zmiana czasu w pętli „while” nie pomaga.
Wynik wygląda następująco:
M1: ~4000 | M2: ~5 | M3: ~5 | M4: ~5 | TOTAL: ~8000
Próbowałem też zmienić częstotliwość na 500 Hz, ale wtedy pętla trwa dwa razy dłużej, czyli 4 ms.
Kod:
#include <Arduino.h>
#define M1 26
#define M2 25
#define M3 33
#define M4 32
void setup()
{
Serial.begin(115200);
// PWM at 250 Hz, 12-bit
analogWriteFrequency(M1, 250);
analogWriteFrequency(M2, 250);
analogWriteFrequency(M3, 250);
analogWriteFrequency(M4, 250);
analogWriteResolution(12);
delay(1000);
Serial.println("250 Hz PWM timing test");
}
void loop()
{
uint32_t loopStart = micros();
// Measure each PWM update
uint32_t a = micros();
analogWrite(M1, 1500);
uint32_t b = micros();
uint32_t c = micros();
analogWrite(M2, 1500);
uint32_t d = micros();
uint32_t e = micros();
analogWrite(M3, 1500);
uint32_t f = micros();
uint32_t g = micros();
analogWrite(M4, 1500);
uint32_t h = micros();
uint32_t m1Time = b - a;
uint32_t m2Time = d - c;
uint32_t m3Time = f - e;
uint32_t m4Time = h - g;
uint32_t motorTime = h - a;
// 250 Hz = 4000 us period
while (micros() - loopStart < 4000)
{
}
uint32_t totalTime = micros() - loopStart;
static uint32_t count = 0;
if (++count >= 100)
{
Serial.print("M1: ");
Serial.print(m1Time);
Serial.print(" | M2: ");
Serial.print(m2Time);
Serial.print(" | M3: ");
Serial.print(m3Time);
Serial.print(" | M4: ");
Serial.print(m4Time);
Serial.print(" | MOTORS: ");
Serial.print(motorTime);
Serial.print(" | TOTAL: ");
Serial.println(totalTime);
count = 0;
}
}Wynik wygląda następująco:
M1: ~4000 | M2: ~5 | M3: ~5 | M4: ~5 | TOTAL: ~8000
Próbowałem też zmienić częstotliwość na 500 Hz, ale wtedy pętla trwa dwa razy dłużej, czyli 4 ms.
...

