Hoe kan ik een geschutstoren 290 graden laten draaien

Discussie in 'Model elektronica' gestart door Herby63, 4 sep 2021.

  1. Herby63

    Herby63

    Lid geworden:
    6 aug 2009
    Berichten:
    4.400
    Locatie:
    Brugge
    Laatst bewerkt: 22 aug 2022
  2. max z

    max z Vriend van modelbouwforum.nl PH-SAM

    Lid geworden:
    4 dec 2009
    Berichten:
    2.421
    Locatie:
    Boskoop
    :thumbsup:
    Doel 1 bereikt! Morgen verder.......
     
    Herby63 vindt dit leuk.
  3. max z

    max z Vriend van modelbouwforum.nl PH-SAM

    Lid geworden:
    4 dec 2009
    Berichten:
    2.421
    Locatie:
    Boskoop
    Toch nog even doorgegaan. Ik heb de 290 graden limieten voor de eerste 4 servo's ingevuld, en de equivalenten van 1000 micros en 2000 micros voor de volgende 4. De slowstep setting geldt voor beide.

    Code:
    volatile unsigned long timer; // all timer variables are unsigned long
    volatile int inpulse =  1500;
    volatile byte sync = 0;
    int mpcount, pulse;
    int Xtarget [8], Xpulse[8];
    const int slowstep = 1;
    const int Xtargetlow [] = {83, 83, 83, 83, 205, 205, 205, 205};
    const int Xtargethigh [] = {467, 467, 467, 467, 410, 410, 410, 410};
    bool start = false, RxchWas = false;
    
    #include <Wire.h>
    #include <Adafruit_PWMServoDriver.h>
    
    // called this way, it uses the default address 0x40
    Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
    
    void setup() {
     
      attachInterrupt(0, read_pwm, CHANGE); // Pin 2 = interrupt 0
      pinMode(2, INPUT);
     
      Serial.begin(9600);
     
      pwm.begin();
    
      pwm.setOscillatorFrequency(27000000);
      pwm.setPWMFreq(50); // 50Hz, frame length 20 millis
    
      for (int i=0; i<8; i++) {
        Xtarget [i] = 308; // equivalent of 1500 micros
        Xpulse [i] = 308;
      }
    
      delay(10);
    }
    
    void loop() {
     
      while (sync == 0); // ISR sets sync to true when reading Rx pulse is complete, loop starts.
      sync = 0; // stops loop after one run.
     
      delay (6); // do an atomic copy, delayed to midframe to reduce jitter
      cli();
      pulse = inpulse;
      sei();
     
      // read next 8 incoming pulses, triggered by a syncpulse, map to extended range and save in array Xtarget []
     
      if (start && pulse >=1000) {
        Xtarget [mpcount] = map (pulse, 1030, 1980, Xtargetlow [mpcount], Xtargethigh [mpcount]); // extending pulse to Xtarget
    
        mpcount += 1;
        if (mpcount >7) {
          start = false; // stop saving subchannels after 8 counts
          delay(500); // only for testing the output by monitor, deactivate for servo operation
        }
      }
     
      if (pulse >= 850 && pulse <950) { // is syncpulse
        start = true;
        mpcount = 0;
      }
    
      // update all servo outputs with or without changed targets, maximum step is slowstep setting
     
      for (uint8_t j=0; j<8; j++) {
        if (Xtarget [j] - Xpulse [j] >= slowstep) Xpulse [j] += slowstep;
        else if (Xtarget [j] - Xpulse [j] <= -slowstep) Xpulse [j] -= slowstep;
        else Xpulse [j] = Xtarget [j];
        pwm.setPWM(j, 0, Xpulse [j]); // LOW>HIGH at 0, HIGH>LOW at pulselen
      }
    
    }
    
    void read_pwm(){
      if (RxchWas == 0) { // if pin 2 was false previously
        timer = micros(); // start timer
        RxchWas = 1;
      }
      else if (RxchWas == 1) { // if pin 2 was already true, read timer
        inpulse = ((volatile int)micros() - timer);
        RxchWas = 0;
        sync = 1; // start loop
     
      }
    }
     
    Laatst bewerkt: 22 aug 2022
  4. max z

    max z Vriend van modelbouwforum.nl PH-SAM

    Lid geworden:
    4 dec 2009
    Berichten:
    2.421
    Locatie:
    Boskoop
    Kleine wijziging in de code gedaan.
     
  5. Herby63

    Herby63

    Lid geworden:
    6 aug 2009
    Berichten:
    4.400
    Locatie:
    Brugge
    :), al net zo verslaafd als ik.
    Er gebeurt niks, geen enkele beweging :(
     
  6. max z

    max z Vriend van modelbouwforum.nl PH-SAM

    Lid geworden:
    4 dec 2009
    Berichten:
    2.421
    Locatie:
    Boskoop
    Ontvanger aangesloten op pin 2?
     
  7. Herby63

    Herby63

    Lid geworden:
    6 aug 2009
    Berichten:
    4.400
    Locatie:
    Brugge
    :) Eerst niet, maar toen ik de code bekeek, zag ik weer al die abracadabra voor het uitlezen van de code, en heb ik dus de ontvanger aangesloten op pin 2, en stroom afgetakt van de UNO, de zender aangezet en even met de potmetertjes gedraaid, geen leven in de servo's.
     
  8. max z

    max z Vriend van modelbouwforum.nl PH-SAM

    Lid geworden:
    4 dec 2009
    Berichten:
    2.421
    Locatie:
    Boskoop
    Oei, dat valt weer tegen. Wordt dan toch morgen ben ik bang (en hoop ik...).
     
    Herby63 vindt dit leuk.
  9. max z

    max z Vriend van modelbouwforum.nl PH-SAM

    Lid geworden:
    4 dec 2009
    Berichten:
    2.421
    Locatie:
    Boskoop
    Ik was een stukje vergeten, en heb nu de code in #143 aangepast. Nieuwe kansen........
     
  10. max z

    max z Vriend van modelbouwforum.nl PH-SAM

    Lid geworden:
    4 dec 2009
    Berichten:
    2.421
    Locatie:
    Boskoop
    Herby, ik heb de code met de Servo_h library die ik in post #108 had staan per ongeluk overschreven met de laatste versie, en ik had 'm nog niet opgeslagen als backup. Heb jij die nog beschikbaar?
    Het was de eerste versie met de input via interrupt.
     
  11. Herby63

    Herby63

    Lid geworden:
    6 aug 2009
    Berichten:
    4.400
    Locatie:
    Brugge
    Waarschijnlijk is het onderstaande, heb ik opgeslagen op de dag van post 108. Hopelijk heb ik er niet te veel aan getweaked. :)

    int subcount, pulse;
    bool start = false;

    #include <Servo.h>
    Servo MPS[8]; // array of Multi Prop Servo objects

    void setup() {

    pinMode(2, INPUT);

    Serial.begin(9600);

    for (int i=0; i<8; i++) {
    MPS.attach (i+5); // servo output attached to pins 5 - 12
    }
    }

    void loop() {

    pulse = pulseIn(2, HIGH); // timeout?

    if (start && pulse >= 950) {
    MPS[subcount-1].writeMicroseconds(pulse); // array starts at MPS[0], ends at MPS[7]
    Serial.print ("MPS"); Serial.print (subcount); Serial.print (" "); Serial.print (pulse); Serial.print (" ");
    subcount += 1;
    if (subcount >8) {
    start = false; // stop saving subchannels after 8 counts
    delay(1); // only for testing the output by monitor, deactivate for servo operation
    }
    }

    if (pulse <= 950) { // syncpulse
    start = true;
    subcount = 1;
    Serial.println ("");
    }
    }

    Hoe doe je dat trouwens om de code in een kadertje in te voegen?
     
  12. max z

    max z Vriend van modelbouwforum.nl PH-SAM

    Lid geworden:
    4 dec 2009
    Berichten:
    2.421
    Locatie:
    Boskoop
    Schermafbeelding 2022-08-23 om 16.22.35.png Schermafbeelding 2022-08-23 om 16.23.43.png Schermafbeelding 2022-08-23 om 16.24.23.png

    en copy/paste in het lege vak.
     
  13. Herby63

    Herby63

    Lid geworden:
    6 aug 2009
    Berichten:
    4.400
    Locatie:
    Brugge
    Nog steeds geen reactie,
    Controletest met de code uit post 138, om de aansluitingen te controleren is nog altijd positief.
     
  14. Herby63

    Herby63

    Lid geworden:
    6 aug 2009
    Berichten:
    4.400
    Locatie:
    Brugge
    Test voor het publiceren van code:
    Code:
    volatile unsigned long timer; // all timer variables are unsigned long
    volatile int inpulse =  1500;
    volatile byte sync = 0;
    int mpcount, pulse;
    int Xtarget [8], Xpulse[8];
    const int slowstep = 1;
    const int Xtargetlow [] = {83, 83, 83, 83, 205, 205, 205, 205};
    const int Xtargethigh [] = {467, 467, 467, 467, 410, 410, 410, 410};
    bool start = false, RxchWas = false;
    
    #include <Wire.h>
    #include <Adafruit_PWMServoDriver.h>
    
    // called this way, it uses the default address 0x40
    Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
    
    void setup() {
     
      attachInterrupt(0, read_pwm, CHANGE); // Pin 2 = interrupt 0
      pinMode(2, INPUT);
     
      Serial.begin(9600);
     
      pwm.begin();
    
      pwm.setOscillatorFrequency(27000000);
      pwm.setPWMFreq(50); // 50Hz, frame length 20 millis
    
      for (int i=0; i<8; i++) {
        Xtarget [i] = 308; // equivalent of 1500 micros
        Xpulse [i] = 308;
      }
    
      delay(10);
    }
    
    void loop() {
     
      while (sync == 0); // ISR sets sync to true when reading Rx pulse is complete, loop starts.
      sync = 0; // stops loop after one run.
     
      delay (6); // do an atomic copy, delayed to midframe to reduce jitter
      cli();
      pulse = inpulse;
      sei();
     
      // read next 8 incoming pulses, triggered by a syncpulse, map to extended range and save in array Xtarget []
     
      if (start && pulse >=1000) {
        Xtarget [mpcount] = map (pulse, 1030, 1980, Xtargetlow [mpcount], Xtargethigh [mpcount]); // extending pulse to Xtarget
    
        mpcount += 1;
        if (mpcount >7) {
          start = false; // stop saving subchannels after 8 counts
          delay(500); // only for testing the output by monitor, deactivate for servo operation
        }
      }
     
      if (pulse >= 850 && pulse <950) { // is syncpulse
        start = true;
        mpcount = 0;
      }
    
      // update all servo outputs with or without changed targets, maximum step is slowstep setting
     
      for (uint8_t j=0; j<8; j++) {
        if (Xtarget [j] - Xpulse [j] >= slowstep) Xpulse [j] += slowstep;
        else if (Xtarget [j] - Xpulse [j] <= -slowstep) Xpulse [j] -= slowstep;
        else Xpulse [j] = Xtarget [j];
        pwm.setPWM(j, 0, Xpulse [j]); // LOW>HIGH at 0, HIGH>LOW at pulselen
      }
    
    }
    
    void read_pwm(){
      if (RxchWas == 0) { // if pin 2 was false previously
        timer = micros(); // start timer
        RxchWas = 1;
      }
      else if (RxchWas == 1) { // if pin 2 was already true, read timer
        inpulse = ((volatile int)micros() - timer);
        RxchWas = 0;
        sync = 1; // start loop
     
      }
    }
    Test geslaagd :)
     
  15. max z

    max z Vriend van modelbouwforum.nl PH-SAM

    Lid geworden:
    4 dec 2009
    Berichten:
    2.421
    Locatie:
    Boskoop
    Pfoe, dat wordt lastig. Ik heb vandaag de code nog een keer nagelopen, maar ik vond geen fouten of ontbrekende zaken.......o_O

    Daar staat de essentie in van wat ik zocht (het gebruik van de Servo library in de multiprop decoder ), maar er zou meer in moeten staan, Xtarget en Xpulse en zo...
     
  16. Herby63

    Herby63

    Lid geworden:
    6 aug 2009
    Berichten:
    4.400
    Locatie:
    Brugge
    Deze misschien? is ook van 14/08

    Code:
    int mpcount, pulse, Xpulse;
    int Xpulselow [ ] = {2000, 2450, 2000, 2450, 650, 650, 650, 650};
    int Xpulsehigh [ ] = {1000, 550, 1000, 550, 2350, 2350, 2350, 2350};
    bool start = false;
    
    #include <Servo.h>
    Servo MPS[8]; // array of Multi Prop Servo objects MPS [0] - MPS [7]
    
    void setup() {
     
      pinMode(4, INPUT);
     
      Serial.begin(9600);
    
      for (int i=0; i<8; i++) {
      MPS[i].attach (i+5); // servo output attached to pins 5 - 12
      }
    }
    
    void loop() {
     
      pulse = pulseIn(4, HIGH); // timeout?
    
      if (start && pulse >=975) {
        Xpulse = map (pulse, 1000, 1980, Xpulselow [mpcount], Xpulsehigh [mpcount]); // extending pulse to Xpulse
      
        MPS[mpcount].writeMicroseconds(Xpulse); // array starts at MPS[0], ends at MPS[7]
        Serial.print ("MPS"); Serial.print (mpcount); Serial.print (" pulse"); Serial.print (pulse); Serial.print (" Xpulse");
        Serial.println (Xpulse);
        mpcount += 1;
        if (mpcount >7) {
          start = false; // stop saving subchannels after 8 counts
         //delay(500); // only for testing the output by monitor, deactivate for servo operation
        }
      }
     
      if (pulse >= 850 && pulse <950) { // syncpulse
        start = true;
        mpcount = 0;
        Serial.println ("");
      }
    }
     
  17. max z

    max z Vriend van modelbouwforum.nl PH-SAM

    Lid geworden:
    4 dec 2009
    Berichten:
    2.421
    Locatie:
    Boskoop
    Ik heb het variabele-type van Xtarget[] en Xpulse[] aangepast en hetzelfde gemaakt als in de test, misschien is dat het. Anders moet je eens kijken met de serial monitor wat er uitkomt voor pulse Xpulse[0] en Xtarget[0], ergens onderaan in de loop.

    Code:
    volatile unsigned long timer; // all timer variables are unsigned long
    volatile int inpulse =  1500;
    volatile byte sync = 0;
    int mpcount, pulse;
    uint16_t Xtarget [8], Xpulse[8];
    const int slowstep = 1;
    const int Xtargetlow [] = {83, 83, 83, 83, 205, 205, 205, 205};
    const int Xtargethigh [] = {467, 467, 467, 467, 410, 410, 410, 410};
    bool start = false, RxchWas = false;
    
    #include <Wire.h>
    #include <Adafruit_PWMServoDriver.h>
    
    // called this way, it uses the default address 0x40
    Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
    
    void setup() {
     
      attachInterrupt(0, read_pwm, CHANGE); // Pin 2 = interrupt 0
      pinMode(2, INPUT);
     
      Serial.begin(9600);
     
      pwm.begin();
    
      pwm.setOscillatorFrequency(27000000);
      pwm.setPWMFreq(50); // 50Hz, frame length 20 millis
    
      for (int i=0; i<8; i++) {
        Xtarget [i] = 308; // equivalent of 1500 micros
        Xpulse [i] = 308;
      }
    
      delay(10);
    }
    
    void loop() {
     
      while (sync == 0); // ISR sets sync to true when reading Rx pulse is complete, loop starts.
      sync = 0; // stops loop after one run.
     
      delay (6); // do an atomic copy, delayed to midframe to reduce jitter
      cli();
      pulse = inpulse;
      sei();
     
      // read next 8 incoming pulses, triggered by a syncpulse, map to extended range and save in array Xtarget []
     
      if (start && pulse >=1000) {
        Xtarget [mpcount] = map (pulse, 1030, 1980, Xtargetlow [mpcount], Xtargethigh [mpcount]); // extending pulse to Xtarget
    
        mpcount += 1;
        if (mpcount >7) {
          start = false; // stop saving subchannels after 8 counts
          delay(500); // only for testing the output by monitor, deactivate for servo operation
        }
      }
     
      if (pulse >= 850 && pulse <950) { // is syncpulse
        start = true;
        mpcount = 0;
      }
    
      // update all servo outputs with or without changed targets, maximum step is slowstep setting
     
      for (uint8_t j=0; j<8; j++) {
        if (Xtarget [j] - Xpulse [j] >= slowstep) Xpulse [j] += slowstep;
        else if (Xtarget [j] - Xpulse [j] <= -slowstep) Xpulse [j] -= slowstep;
        else Xpulse [j] = Xtarget [j];
        pwm.setPWM(j, 0, Xpulse [j]); // LOW>HIGH at 0, HIGH>LOW at pulselen
      }
    
    }
    
    void read_pwm(){
      if (RxchWas == 0) { // if pin 2 was false previously
        timer = micros(); // start timer
        RxchWas = 1;
      }
      else if (RxchWas == 1) { // if pin 2 was already true, read timer
        inpulse = ((volatile int)micros() - timer);
        RxchWas = 0;
        sync = 1; // start loop
     
      }
    }
     
    Herby63 vindt dit leuk.
  18. Herby63

    Herby63

    Lid geworden:
    6 aug 2009
    Berichten:
    4.400
    Locatie:
    Brugge
    Stom van mij, had pulsdraad aangesloten aan 2e pin, en dat is dus pin1.
    Bij aansluiten op pin 2, gaan de 8 servo's bijna naar de neutraalstand, en draaien dan rustig naar de meest linkse positie.
    Hoe ik ook aan de pots draai, ze blijven helemaal naar links gedraaid.

    Serial monitor geeft geen kik.
    Moet ik daarvoor iets toevoegen?
    Baudrate staat goed, en als ik de serial monitor uitzet en weer aan, gaan de servo's in een ruk bijna terug naar neutraal en dan weer rustig naar de meest linkse positie.
     
  19. max z

    max z Vriend van modelbouwforum.nl PH-SAM

    Lid geworden:
    4 dec 2009
    Berichten:
    2.421
    Locatie:
    Boskoop
    Je bedoelt denk ik dat je de usb verbreekt en weer aanzet.

    Code met serial monitor input:
    Code:
    volatile unsigned long timer; // all timer variables are unsigned long
    volatile int inpulse =  1500;
    volatile byte sync = 0;
    int mpcount, pulse;
    uint16_t Xtarget [8], Xpulse[8];
    const int slowstep = 1;
    const int Xtargetlow [] = {83, 83, 83, 83, 205, 205, 205, 205};
    const int Xtargethigh [] = {467, 467, 467, 467, 410, 410, 410, 410};
    bool start = false, RxchWas = false;
    
    #include <Wire.h>
    #include <Adafruit_PWMServoDriver.h>
    
    // called this way, it uses the default address 0x40
    Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
    
    void setup() {
     
      attachInterrupt(0, read_pwm, CHANGE); // Pin 2 = interrupt 0
      pinMode(2, INPUT);
     
      Serial.begin(9600);
     
      pwm.begin();
    
      pwm.setOscillatorFrequency(27000000);
      pwm.setPWMFreq(50); // 50Hz, frame length 20 millis
    
      for (int i=0; i<8; i++) {
        Xtarget [i] = 308; // equivalent of 1500 micros
        Xpulse [i] = 308;
      }
    
      delay(10);
    }
    
    void loop() {
     
      while (sync == 0); // ISR sets sync to true when reading Rx pulse is complete, loop starts.
      sync = 0; // stops loop after one run.
     
      delay (6); // do an atomic copy, delayed to midframe to reduce jitter
      cli();
      pulse = inpulse;
      sei();
     
      // read next 8 incoming pulses, triggered by a syncpulse, map to extended range and save in array Xtarget []
     
      if (start && pulse >=1000) {
        Xtarget [mpcount] = map (pulse, 1030, 1980, Xtargetlow [mpcount], Xtargethigh [mpcount]); // extending pulse to Xtarget
    
        mpcount += 1;
        if (mpcount >7) {
          start = false; // stop saving subchannels after 8 counts
          delay(500); // only for testing the output by monitor, deactivate for servo operation
        }
      }
     
      if (pulse >= 850 && pulse <950) { // is syncpulse
        start = true;
        mpcount = 0;
      }
    
      // update all servo outputs with or without changed targets, maximum step is slowstep setting
     
      for (uint8_t j=0; j<8; j++) {
        if (Xtarget [j] - Xpulse [j] >= slowstep) Xpulse [j] += slowstep;
        else if (Xtarget [j] - Xpulse [j] <= -slowstep) Xpulse [j] -= slowstep;
        else Xpulse [j] = Xtarget [j];
        pwm.setPWM(j, 0, Xpulse [j]); // LOW>HIGH at 0, HIGH>LOW at pulselen
      }
      Serial.print ("pulse = "); Serial.println (pulse);
      Serial.print ("Xpulse[1] = "); Serial.print (Xpulse[1]); Serial.print ("    Xtarget[1] = "), Serial.println (Xtarget[1]);
    }
    
    void read_pwm(){
      if (RxchWas == 0) { // if pin 2 was false previously
        timer = micros(); // start timer
        RxchWas = 1;
      }
      else if (RxchWas == 1) { // if pin 2 was already true, read timer
        inpulse = ((volatile int)micros() - timer);
        RxchWas = 0;
        sync = 1; // start loop
     
      }
    }
    Géén delays invoegen, dan gaat de volgorde niet goed. Gewoon af en toe de autoscroll uitzetten.
     
  20. Herby63

    Herby63

    Lid geworden:
    6 aug 2009
    Berichten:
    4.400
    Locatie:
    Brugge
    nee, nee, op de IDE op de PC op dat icoontje rechtsbovenaan klikken voor de serial monitor aan te zetten.

    En dit komt nu uit de serial monitor
    Code:
    Xpulse[1] = 308    Xtarget[1] = 308
    pulse = 1464
    Xpulse[1] = 307    Xtarget[1] = 308
    pulse = 1508
    Xpulse[1] = 308    Xtarget[1] = 308
    pulse = 1520
    Xpulse[1] = 307    Xtarget[1] = 308
    pulse = 1540
    Xpulse[1] = 308    Xtarget[1] = 308
    pulse = 1476
    Xpulse[1] = 307    Xtarget[1] = 308
    pulse = 1484
    Xpulse[1] = 308    Xtarget[1] = 308
    pulse = 1512
    Xpulse[1] = 307    Xtarget[1] = 308
    pulse = 1524
    Xpulse[1] = 308    Xtarget[1] = 308
    pulse = 1536
    Xpulse[1] = 307    Xtarget[1] = 308
    pulse = 1488
    Xpulse[1] = 308    Xtarget[1] = 308
    pulse = 1496
    Xpulse[1] = 307    Xtarget[1] = 308
    pulse = 1496
    Xpulse[1] = 308    Xtarget[1] = 308
    pulse = 1556
    Xpulse[1] = 307    Xtarget[1] = 308
    pulse = 1540
    Xpulse[1] = 308    Xtarget[1] = 308
    pulse = 928
    Xpulse[1] = 307    Xtarget[1] = 308
    pulse = 1504
    Xpulse[1] = 308    Xtarget[1] = 308
    pulse = 1504
    Xpulse[1] = 309    Xtarget[1] = 274
    pulse = 1544
    Xpulse[1] = 310    Xtarget[1] = 274
    pulse = 920
    Xpulse[1] = 311    Xtarget[1] = 274
    pulse = 1464
    Xpulse[1] = 312    Xtarget[1] = 274
    pulse = 1520
    Xpulse[1] = 313    Xtarget[1] = 281
    pulse = 1536
    Xpulse[1] = 314    Xtarget[1] = 281
    pulse = 1520
    Xpulse[1] = 315    Xtarget[1] = 281
    pulse = 1460
    Xpulse[1] = 316    Xtarget[1] = 281
    pulse = 1512
    Xpulse[1] = 317    Xtarget[1] = 281
    pulse = 1512
    Xpulse[1] = 318    Xtarget[1] = 281
    pulse = 1524
    Xpulse[1] = 319    Xtarget[1] = 281
    pulse = 1484
    Xpulse[1] = 320    Xtarget[1] = 281
    pulse = 1512
    Xpulse[1] = 321    Xtarget[1] = 281
    pulse = 1524
    Xpulse[1] = 322    Xtarget[1] = 281
    pulse = 1464
    Xpulse[1] = 323    Xtarget[1] = 281
    pulse = 1484
    Xpulse[1] = 324    Xtarget[1] = 281
    pulse = 1508
    Xpulse[1] = 325    Xtarget[1] = 281
    pulse = 1524
    Xpulse[1] = 326    Xtarget[1] = 281
    pulse = 1556
    Xpulse[1] = 327    Xtarget[1] = 281
    pulse = 1496
    Xpulse[1] = 328    Xtarget[1] = 281
    pulse = 1516
    Xpulse[1] = 329    Xtarget[1] = 281
    pulse = 1496
    Xpulse[1] = 330    Xtarget[1] = 281
    pulse = 1552
    Xpulse[1] = 331    Xtarget[1] = 281
    pulse = 1544
    Xpulse[1] = 332    Xtarget[1] = 281
    pulse = 924
    Xpulse[1] = 333    Xtarget[1] = 281
    pulse = 1504
    Xpulse[1] = 334    Xtarget[1] = 281
    pulse = 1500
    Xpulse[1] = 335    Xtarget[1] = 272
    pulse = 1528
    Xpulse[1] = 336    Xtarget[1] = 272
    pulse = 920
    Xpulse[1] = 337    Xtarget[1] = 272
    pulse = 1448
    Xpulse[1] = 338    Xtarget[1] = 272
    pulse = 1516
    Xpulse[1] = 339    Xtarget[1] = 279
    pulse = 1532
    Xpulse[1] = 340    Xtarget[1] = 279
    pulse = 1516
    Xpulse[1] = 341    Xtarget[1] = 279
    pulse = 1460
    Xpulse[1] = 342    Xtarget[1] = 279
    pulse = 1500
    Xpulse[1] = 343    Xtarget[1] = 279
    pulse = 1524
    Xpulse[1] = 344    Xtarget[1] = 279
    pulse = 1516
    Xpulse[1] = 345    Xtarget[1] = 279
    pulse = 1480
    Xpulse[1] = 346    Xtarget[1] = 279
    pulse = 1508
    Xpulse[1] = 347    Xtarget[1] = 279
    pulse = 1516
    Xpulse[1] = 348    Xtarget[1] = 279
    pulse = 1464
    Xpulse[1] = 349    Xtarget[1] = 279
    pulse = 1484
    Xpulse[1] = 350    Xtarget[1] = 279
    pulse = 1512
    Xpulse[1] = 351    Xtarget[1] = 279
    pulse = 1504
    Xpulse[1] = 352    Xtarget[1] = 279
    pulse = 1552
    Xpulse[1] = 353    Xtarget[1] = 279
    pulse = 1496
    Xpulse[1] = 354    Xtarget[1] = 279
    pulse = 1508
    Xpulse[1] = 355    Xtarget[1] = 279
    pulse = 1508
    Xpulse[1] = 356    Xtarget[1] = 279
    pulse = 1552
    Xpulse[1] = 357    Xtarget[1] = 279
    pulse = 1536
    Xpulse[1] = 358    Xtarget[1] = 279
    pulse = 920
    Xpulse[1] = 359    Xtarget[1] = 279
    pulse = 1496
    Xpulse[1] = 360    Xtarget[1] = 279
    pulse = 1516
    Xpulse[1] = 361    Xtarget[1] = 279
    pulse = 1540
    Xpulse[1] = 362    Xtarget[1] = 279
    pulse = 932
    Xpulse[1] = 363    Xtarget[1] = 279
    pulse = 1464
    Xpulse[1] = 364    Xtarget[1] = 279
    pulse = 1504
    Xpulse[1] = 365    Xtarget[1] = 274
    pulse = 1540
    Xpulse[1] = 366    Xtarget[1] = 274
    pulse = 1512
    Xpulse[1] = 367    Xtarget[1] = 274
    pulse = 1464
    Xpulse[1] = 368    Xtarget[1] = 274
    pulse = 1496
    Xpulse[1] = 369    Xtarget[1] = 274
    pulse = 1512
    Xpulse[1] = 370    Xtarget[1] = 274
    pulse = 1516
    Xpulse[1] = 371    Xtarget[1] = 274
    pulse = 1496
    Xpulse[1] = 372    Xtarget[1] = 274
    pulse = 1512
    Xpulse[1] = 373    Xtarget[1] = 274
    pulse = 1516
    Xpulse[1] = 374    Xtarget[1] = 274
    pulse = 1460
    Xpulse[1] = 375    Xtarget[1] = 274
    pulse = 1496
    Xpulse[1] = 376    Xtarget[1] = 274
    pulse = 1512
    Xpulse[1] = 377    Xtarget[1] = 274
    pulse = 1524
    Xpulse[1] = 378    Xtarget[1] = 274
    pulse = 1552
    Xpulse[1] = 379    Xtarget[1] = 274
    pulse = 1484
    Xpulse[1] = 380    Xtarget[1] = 274
    pulse = 1508
    Xpulse[1] = 381    Xtarget[1] = 274
    pulse = 1492
    Xpulse[1] = 382    Xtarget[1] = 274
    pulse = 1552
    Xpulse[1] = 383    Xtarget[1] = 274
    pulse = 1540
    Xpulse[1] = 384    Xtarget[1] = 274
    pulse = 920
    Xpulse[1] = 385    Xtarget[1] = 274
    pulse = 1496
    Xpulse[1] = 386    Xtarget[1] = 274
    pulse = 1512
    Xpulse[1] = 387    Xtarget[1] = 277
    pulse = 1548
    Xpulse[1] = 388    Xtarget[1] = 277
    pulse = 928
    Xpulse[1] = 389    Xtarget[1] = 277
    pulse = 1468
    Xpulse[1] = 390    Xtarget[1] = 277
    pulse = 1504
    Xpulse[1] = 391    Xtarget[1] = 274
    pulse = 1540
    Xpulse[1] = 392    Xtarget[1] = 274
    pulse = 1528
    Xpulse[1] = 393    Xtarget[1] = 274
    pulse = 1468
    Xpulse[1] = 394    Xtarget[1] = 274
    pulse = 1508
    Xpulse[1] = 395    Xtarget[1] = 274
    pulse = 1512
    Xpulse[1] = 396    Xtarget[1] = 274
    pulse = 1520
    Xpulse[1] = 397    Xtarget[1] = 274
    pulse = 1500
    Xpulse[1] = 398    Xtarget[1] = 274
    pulse = 1508
    Xpulse[1] = 399    Xtarget[1] = 274
    pulse = 1524
    Xpulse[1] = 400    Xtarget[1] = 274
    pulse = 1460
    Xpulse[1] = 401    Xtarget[1] = 274
    pulse = 1472
    Xpulse[1] = 402    Xtarget[1] = 274
    pulse = 1504
    Xpulse[1] = 403    Xtarget[1] = 274
    pulse = 1520
    Xpulse[1] = 404    Xtarget[1] = 274
    pulse = 1564
    Xpulse[1] = 405    Xtarget[1] = 274
    pulse = 1488
    Xpulse[1] = 406    Xtarget[1] = 274
    pulse = 1520
    Xpulse[1] = 407    Xtarget[1] = 274
    pulse = 1492
    Xpulse[1] = 408    Xtarget[1] = 274
    pulse = 1548
    Xpulse[1] = 409    Xtarget[1] = 274
    pulse = 1548
    Xpulse[1] = 410    Xtarget[1] = 274
    pulse = 916
    Xpulse[1] = 411    Xtarget[1] = 274
    pulse = 1508
    Xpulse[1] = 412    Xtarget[1] = 274
    pulse = 1500
    Xpulse[1] = 413    Xtarget[1] = 272
    pulse = 1532
    Xpulse[1] = 414    Xtarget[1] = 272
    pulse = 920
    Xpulse[1] = 415    Xtarget[1] = 272
    pulse = 1464
    Xpulse[1] = 416    Xtarget[1] = 272
    pulse = 1508
    Xpulse[1] = 417    Xtarget[1] = 276
    pulse = 1540
    Xpulse[1] = 418    Xtarget[1] = 276
    pulse = 1520
    Xpulse[1] = 419    Xtarget[1] = 276
    pulse = 1460
    Xpulse[1] = 420    Xtarget[1] = 276
    pulse = 1496
    Xpulse[1] = 421    Xtarget[1] = 276
    pulse = 1528
    Xpulse[1] = 422    Xtarget[1] = 276
    pulse = 1528
    Xpulse[1] = 423    Xtarget[1] = 276
    pulse = 1480
    Xpulse[1] = 424    Xtarget[1] = 276
    pulse = 1508
    Xpulse[1] = 425    Xtarget[1] = 276
    pulse = 1516
    Xpulse[1] = 426    Xtarget[1] = 276
    pulse = 1460
    Xpulse[1] = 427    Xtarget[1] = 276
    pulse = 1484
    Xpulse[1] = 428    Xtarget[1] = 276
    pulse = 1516
    Xpulse[1] = 429    Xtarget[1] = 276
    pulse = 1516
    Xpulse[1] = 430    Xtarget[1] = 276
    pulse = 1556
    Xpulse[1] = 431    Xtarget[1] = 276
    pulse = 1492
    Xpulse[1] = 432    Xtarget[1] = 276
    pulse = 1512
    Xpulse[1] = 433    Xtarget[1] = 276
    pulse = 1500
    Xpulse[1] = 434    Xtarget[1] = 276
    pulse = 1568
    Xpulse[1] = 435    Xtarget[1] = 276
    pulse = 1532
    Xpulse[1] = 436    Xtarget[1] = 276
    pulse = 924
    Xpulse[1] = 437    Xtarget[1] = 276
    pulse = 1492
    Xpulse[1] = 438    Xtarget[1] = 276
    pulse = 1504
    Xpulse[1] = 439    Xtarget[1] = 274
    pulse = 1552
    Xpulse[1] = 440    Xtarget[1] = 274
    pulse = 924
    Xpulse[1] = 441    Xtarget[1] = 274
    pulse = 1468
    Xpulse[1] = 442    Xtarget[1] = 274
    pulse = 1496
    Xpulse[1] = 443    Xtarget[1] = 271
    pulse = 1544
    Xpulse[1] = 444    Xtarget[1] = 271
    pulse = 1512
    Xpulse[1] = 445    Xtarget[1] = 271
    pulse = 1464
    Xpulse[1] = 446    Xtarget[1] = 271
    pulse = 1500
    Xpulse[1] = 447    Xtarget[1] = 271
    pulse = 1504
    Xpulse[1] = 448    Xtarget[1] = 271
    pulse = 1524
    Xpulse[1] = 449    Xtarget[1] = 271
    pulse = 1500
    Xpulse[1] = 450    Xtarget[1] = 271
    pulse = 1512
    Xpulse[1] = 451    Xtarget[1] = 271
    pulse = 1512
    Xpulse[1] = 452    Xtarget[1] = 271
    pulse = 1464
    Xpulse[1] = 453    Xtarget[1] = 271
    pulse = 1488
    Xpulse[1] = 454    Xtarget[1] = 271
    pulse = 1516
    Xpulse[1] = 455    Xtarget[1] = 271
    pulse = 1524
    Xpulse[1] = 456    Xtarget[1] = 271
    pulse = 1552
    Xpulse[1] = 457    Xtarget[1] = 271
    pulse = 1488
    Xpulse[1] = 458    Xtarget[1] = 271
    pulse = 1512
    Xpulse[1] = 459    Xtarget[1] = 271
    pulse = 1492
    Xpulse[1] = 460    Xtarget[1] = 271
    pulse = 1560
    Xpulse[1] = 461    Xtarget[1] = 271
    pulse = 1536
    Xpulse[1] = 462    Xtarget[1] = 271
    pulse = 920
    Xpulse[1] = 463    Xtarget[1] = 271
    pulse = 1496
    Xpulse[1] = 464    Xtarget[1] = 271
    pulse = 1484
    Xpulse[1] = 465    Xtarget[1] = 266
    pulse = 1548
    Xpulse[1] = 466    Xtarget[1] = 266
    pulse = 916
    Xpulse[1] = 467    Xtarget[1] = 266
    pulse = 1464
    Xpulse[1] = 468    Xtarget[1] = 266
    pulse = 1500
    Xpulse[1] = 469    Xtarget[1] = 272
    pulse = 1536
    
     
  21. max z

    max z Vriend van modelbouwforum.nl PH-SAM

    Lid geworden:
    4 dec 2009
    Berichten:
    2.421
    Locatie:
    Boskoop
    Dat is heel vreemd, het gedrag duidt er op dat de code weer van voren af begint, want in de setup sectie worden Xpulse en Xtarget op neutraal gezet. Dat gebeurt normaal niet door het aan- en uitzetten van de monitor.

    Ik had de serial print commando's niet erg strategisch geplaatst. Ik heb ze nu verplaatst, zodat je de inkomende puls registreert, de daaruit berekende Xtarget en de actuele waarde van Xpulse, die daar langzaam naar toe zou moeten bewegen.

    Code:
    volatile unsigned long timer; // all timer variables are unsigned long
    volatile int inpulse =  1500;
    volatile byte sync = 0;
    int mpcount, pulse;
    uint16_t Xtarget [8], Xpulse[8];
    const int slowstep = 1;
    const int Xtargetlow [] = {83, 83, 83, 83, 205, 205, 205, 205};
    const int Xtargethigh [] = {467, 467, 467, 467, 410, 410, 410, 410};
    bool start = false, RxchWas = false;
    
    #include <Wire.h>
    #include <Adafruit_PWMServoDriver.h>
    
    // called this way, it uses the default address 0x40
    Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
    
    void setup() {
     
      attachInterrupt(0, read_pwm, CHANGE); // Pin 2 = interrupt 0
      pinMode(2, INPUT);
     
      Serial.begin(9600);
     
      pwm.begin();
    
      pwm.setOscillatorFrequency(27000000);
      pwm.setPWMFreq(50); // 50Hz, frame length 20 millis
    
      for (int i=0; i<8; i++) {
        Xtarget [i] = 308; // equivalent of 1500 micros
        Xpulse [i] = 308;
      }
    
      delay(10);
    }
    
    void loop() {
     
      while (sync == 0); // ISR sets sync to true when reading Rx pulse is complete, loop starts.
      sync = 0; // stops loop after one run.
     
      delay (6); // do an atomic copy, delayed to midframe to reduce jitter
      cli();
      pulse = inpulse;
      sei();
     
      Serial.print ("pulse = "); Serial.println (pulse);
     
      // read next 8 incoming pulses, triggered by a syncpulse, map to extended range and save in array Xtarget []
     
      if (start && pulse >=1000) {
        Xtarget [mpcount] = map (pulse, 1030, 1980, Xtargetlow [mpcount], Xtargethigh [mpcount]); // extending pulse to Xtarget
    
        Serial.print ("Xpulse[] = "); Serial.print (Xpulse[mpcount]); Serial.print ("    Xtarget[] = "), Serial.println (Xtarget[mpcount]);
    
        mpcount += 1;
        if (mpcount >7) {
          start = false; // stop saving subchannels after 8 counts
          delay(500); // only for testing the output by monitor, deactivate for servo operation
        }
      }
     
      if (pulse >= 850 && pulse <950) { // is syncpulse
        start = true;
        mpcount = 0;
      }
    
      // update all servo outputs with or without changed targets, maximum step is slowstep setting
     
      for (uint8_t j=0; j<8; j++) {
        if (Xtarget [j] - Xpulse [j] >= slowstep) Xpulse [j] += slowstep;
        else if (Xtarget [j] - Xpulse [j] <= -slowstep) Xpulse [j] -= slowstep;
        else Xpulse [j] = Xtarget [j];
        pwm.setPWM(j, 0, Xpulse [j]); // LOW>HIGH at 0, HIGH>LOW at pulselen
      }
     
    }
    
    void read_pwm(){
      if (RxchWas == 0) { // if pin 2 was false previously
        timer = micros(); // start timer
        RxchWas = 1;
      }
      else if (RxchWas == 1) { // if pin 2 was already true, read timer
        inpulse = ((volatile int)micros() - timer);
        RxchWas = 0;
        sync = 1; // start loop
     
      }
    }
     
    Herby63 vindt dit leuk.

Deel Deze Pagina