analoog signaal vertraagd uitsturen.

Arduino specifieke Software
Berichten: 28
Geregistreerd: 30 Nov 2013, 23:49

analoog signaal vertraagd uitsturen.

Berichtdoor Luppie » 31 Dec 2013, 16:47

Ik wil mbv een potmeter een signaal vertraagd uitsturen.

De uitgang moet dus continu het signaal geven dat analoogsignaal door de potmeter gekozen tijd had.

Ik heb wel iets in elkaar geknutseld maar ik kom er niet uit want de uitgang word nu pas ververst wanneer de timer klaar is. De uitgang moet op loopsnelheid ververst worden met de waarde van analoogsignaal X millis geleden)

Code: Alles selecteren
if (analogRead(potPin)>=100 )
{
waitvalue = 100.0/(analogRead(potPin)); // waitvallue is 0,1 to 1
}

currentwaitfactor = millis()
cyclespeed = 100 //cyclespeed word elke cycle  ververst maar is ongeveer 100millis)

if (currentwaitfactor-previouswaitfactor == cyclespeed*waitvalue) //
{
vertaagd signaal==analoogsignaal;
previouswaitfactor=currentwaitfactor
}



servo.write(vertaagd signaal);

Advertisement

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: analoog signaal vertraagd uitsturen.

Berichtdoor nicoverduin » 31 Dec 2013, 17:09

Als ik het goed begrijp dan wil jij dus een signaal naar de servo sturen dat bepaald wordt door de analoge lezing? en als deze analoge waarde wijzigt dan moet dat ook gelijk aangepast worden?
Als dat zo is, dan ben ik ff los :)
a) Een servo gaat in een bepaalde stand staan afhankelijk van de analoge waarde die wordt aangeboden
b) Dus wat ga je dan vertragen?
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 28
Geregistreerd: 30 Nov 2013, 23:49

Re: analoog signaal vertraagd uitsturen.

Berichtdoor Luppie » 31 Dec 2013, 18:30

nee ik heb een bepaalde analoog signaal en die veranderd constant. En inderdaad is dat voor een servo.
Maar de potmeter bepaald alleen de tijd van de vertraging naar de uitgang.
Dus bijvoorbeeld de potmeter zegt 100milli vertraging dan krijgt de uitgang het analoog signaal van 100 milli geleden.

Ik denk dat dit alleen werkt wanneer ik het analoog signaal bewaar (met de bijbehorende tijd) en dat pas ga gebruiken wanneer de pot dat vraagt. Niet echt wat mijn bedoeling is want dan heb ik geheugen nodig?

Het is voor het kalibreren van een servo in een cyclus dat zich herhaald.
1 de cyclus tijd word gemeten.
2 er word berekend wat er moet gebeuren binnen die tijd. (ondertussen word de nieuwe cyclus gemeten)
3 een servo maakt de berekende beweging.

de beweging komt altijd te laat omdat de berekeningen tijd in beslag neemt en de servo heeft tijd nodig om in de positie te komen. Dus wil ik dat signaal bewaren voor de volgende cyclus. Het gevolg is wel dat de servo een beweging maakt die hoort bij twee cyclussen geleden maar dat moet kunnen door maar kleine verschillen in tijd ten opzichte van de vorige cyclus.

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: analoog signaal vertraagd uitsturen.

Berichtdoor nicoverduin » 31 Dec 2013, 19:27

Het ligt er aan hoe vaak je meet. als je constant meet heb je het idee van een emmertjes geheugen nodig of een ringbuffer. En met 2bytes per waarde (of zelfs 1 als je het wat verkleind (delen door 4) heb je bij een vertraging van een seconde 2K resp 1K nodig.
Ring buffertje maken:
- Een pointer naar de huidige gelezen waarden die daarna naar de volgende gaat
- Een pointer die uitleest die loopt een plaats voor op de vorige. Dan is de vertraging de hele buffer
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 28
Geregistreerd: 30 Nov 2013, 23:49

Re: analoog signaal vertraagd uitsturen.

Berichtdoor Luppie » 31 Dec 2013, 21:58

Het gaat dan om 40 integers met een waarde variërend van -500 tot 500. Ik zou daar wel een byte kunnen maken denk ik.

Ik ga eerst eens wat anders proberen ik heb namelijk mijn cyclus in 40 stukje gehakt en bij elk stukje hoort een berekening voor de servo. Dan zou ik dat kunnen verschuiven door een berekening bij stukje 1 neer te zetten die eigenlijk bij stukje 10 hoort.
Alleen even uitvogelen hoe dan lukt met een potmeter :|

hier 4 van de 40 stukjes formule misschien dat jullie er wat mee kunnen
Code: Alles selecteren
 if (cyclecounter>=0 && cyclecounter<=cyclepart)//start of the cycle.This is the first cyclepart
 { rollcycle = rollinneg; //rollinput-negatif has 100% effect
   nickcycle = 0.1*nickinput; // nickinput has 10% effect
 }

 if (cyclecounter>=(int (cyclepart*1))&&cyclecounter<=(int (cyclepart*1))+cyclepart) //the second cyclepart
 { rollcycle = 0.9*rollinneg; //rollinput-negatif has 90% effect
   nickcycle = 0.2*nickinput; //nickinput has 20% effect
 }
 
 if (cyclecounter>=(int (cyclepart*2))&&cyclecounter<=(int (cyclepart*2))+cyclepart) //the 3rd cyclepart
 { rollcycle = 0.8*rollinneg; //rollinput-negatif has 80% effect
   nickcycle = 0,3*nickinput; //nickinput has 30% effect
 }
 
 if (cyclecounter>=(int (cyclepart*3))&&cyclecounter<=(int (cyclepart*3))+cyclepart)//the 4th cyclepart
 { rollcycle = 0.7*rollinneg; //rollinput-negatif has 70% effect
   nickcycle = 0.4*nickinput; //nickinput has 40% effect
 }

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: analoog signaal vertraagd uitsturen.

Berichtdoor nicoverduin » 01 Jan 2014, 13:13

misschien helpt het als je eerst verteld waarvoor dit allemaal is. Tot nu toe zie ik alleen een lineair stukje code waarvan ik mij afvraag of dit niet eenvoudiger is op te lossen.
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 28
Geregistreerd: 30 Nov 2013, 23:49

Re: analoog signaal vertraagd uitsturen.

Berichtdoor Luppie » 01 Jan 2014, 13:40

Dit een code voor een monocopter hier het draadje voor meer uitleg http://www.arduinoforum.nl/viewtopic.php?f=21&t=594

Het is mijn eerste code dus in de ogen van een ervaren iemand misschien nog een zooitje :)

/* Beta version 0.0 
monocopter code for full control of a monocopter in flight, pitch, nick, roll and yaw. 
based on high speed reading of the Honeywell HMC5883L up to 500Hz.
Reading multi channels with pin change interrupt.*/

#include <PinChangeInt.h>
#include <Wire.h>
#include <Servo.h>

/* define all MAG related stuff */
#define MAG_ADDRESS ((char) 0x1E) //The I2C address of the MAG module

uint8_t mag_buffer[6];
int16_t mag_raw[3];

volatile boolean isDataReady = false;

long start_time = 0;
long previous_time = 0;
long loop_duration = 0;

/*define all pinchange interrupt related stuff*/
#define PITCH_IN_PIN 2 //channel in pins
#define NICK_IN_PIN 3
#define ROLL_IN_PIN 4
#define YAW_IN_PIN 5

#define SERVO_OUT_PIN 9 //channel out pins

// Servo objects generate the signals expected by Electronic Speed Controllers and Servos
// We will use the objects to output the signals we read in
// this example code provides a straight pass through of the signal with no custom processing
Servo servoPITCH;
Servo servoNICK;
Servo servoROLL;
Servo servoYAW;
Servo servo;

// These bit flags are set in bUpdateFlagsShared to indicate which
// channels have new signals
#define PITCH_FLAG 1
#define NICK_FLAG 2
#define ROLL_FLAG 4
#define YAW_FLAG 8

// holds the update flags defined above
volatile uint8_t bUpdateFlagsShared;

// shared variables are updated by the ISR and read by loop.
// In loop we immediatley take local copies so that the ISR can keep ownership of the 
// shared ones. To access these in loop
// we first turn interrupts off with noInterrupts
// we take a copy to use in loop and the turn interrupts back on
// as quickly as possible, this ensures that we are always able to receive new signals
volatile uint16_t unPITCHInShared;
volatile uint16_t unNICKInShared;
volatile uint16_t unROLLInShared;
volatile uint16_t unYAWInShared;

// These are used to record the rising edge of a pulse in the calcInput functions
// They do not need to be volatile as they are only used in the ISR. If we wanted
// to refer to these in loop and the ISR then they would need to be declared volatile
uint32_t ulPITCHStart;
uint32_t ulNICKStart;
uint32_t ulROLLStart;
uint32_t ulYAWStart;

/*difine all stuff related to monocopter calculation */
unsigned long time0= 0; //time for looptime
unsigned int headingDegrees= 0; //vallue in degrees from the MAG
int yawinput= 0; //inputsignal to calculate with
int nickinput= 0; //inputsignal to calculate with
int rollinput= 0; //inputsignal to calculate with
int newyaw= 0; //temporary vallue for yaw
int yaw= 0; // vallue in degrees that is front
int yawmin= 0; //vallue in degrees under yaw
int yawplus= 0; //vallue in degrees above yaw
int azimuth= 0; // heading
int yawverschilMIN= 0; //difference between yaw and yawmin
int yawverschilPLUS= 0; //difference between yaw and yawplus
int pitch= 0; // flapdifflection for lift
unsigned long cyclestart= 0; // startpoint for cycle time
unsigned long cyclecounter= 1001; // endpoint for cycletime (starts with 1001 for start of loop)
float cyclespeed= 1001; // cycletime
float cyclepart= 0; // part of the cycletime
int nickinneg= 0; // negative of nickinput
int rollinneg= 0; // negative of rollinput
int rollcycle= 0; //cycle vallue for servo
int nickcycle= 0; //cycle vallue for servo
int potPin= 1; // pot input for servo delay output
float waitvalue= 0; // value to hold waiting time for roll en nick
int waittime= 0; // millis to wait for roll en nick to output
unsigned long currentwaitfactor= 0; //time
long previouswaitfactor= 0; //store last time roll en nick where updated

void setup()
{
  Serial.begin(115200);
  Wire.begin();
  
  Serial.println("serieele communicatie aan");
  
  pinMode(13, OUTPUT); // uitgang 13 is ledje
  

  /* Initialise the module */
  configMag();

  // attach servo objects, these will generate the correct
  // pulses for driving Electronic speed controllers, servos or other devices
  // designed to interface directly with RC Receivers
  servo.attach(SERVO_OUT_PIN);
  attachInterrupt(12, magDataReady, RISING);

  // using the PinChangeInt library, attach the interrupts
  // used to read the channels
  PCintPort::attachInterrupt(PITCH_IN_PIN, calcPITCH,CHANGE);
  PCintPort::attachInterrupt(NICK_IN_PIN, calcNICK,CHANGE);
  PCintPort::attachInterrupt(ROLL_IN_PIN, calcROLL,CHANGE);
  PCintPort::attachInterrupt(YAW_IN_PIN, calcYAW,CHANGE);
}

//interrupt function when mag DRDY pin is brought LOW
void magDataReady() {
  isDataReady = true;
}

void loop()
{
  
  time0=micros(); // sla aan het begin van de loop de milliseconden sinds de start van de arduino op voor loopsnelheid
  
   if(isDataReady = true) //read the MAG at highest speed possible
   {  readMag();
       
      //Serial.print(mag_raw[0], DEC); Serial.print(",");
      //Serial.print(mag_raw[1], DEC); Serial.print(",");
      //Serial.print(mag_raw[2], DEC); Serial.print(",");
      isDataReady = false;
    
    }
    else {
      Serial.println("Missed one");
    }
   
  
   // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading = atan2(mag_raw[2], mag_raw[0]);
  //Serial.print(micros()-time0);Serial.println("na MAG");
  
  // for propper north fill in your current declination angle. The declination angle varies seasonally.
  float declinationAngle = 0.0457;
  heading += declinationAngle;
  
    // Correct for when signs are reversed.
  if(heading < 0)
    heading += 2*PI;
    
  // Check for wrap due to addition of declination.
  if(heading > 2*PI)
    heading -= 2*PI;
   
  // Convert radians to degrees for readability. was float,int van gemaakt ivm rekensnelheid
  int headingDegrees = heading * 180/M_PI;
  
    
  // create local variables to hold a local copies of the channel inputs
  // these are declared static so that thier values will be retained
  // between calls to loop.
  static uint16_t unPITCHIn;
  static uint16_t unNICKIn;
  static uint16_t unROLLIn;
  static uint16_t unYAWIn;
  // local copy of update flags
  static uint8_t bUpdateFlags;

  // check shared update flags to see if any channels have a new signal
  if(bUpdateFlagsShared)
  {
    noInterrupts(); // turn interrupts off quickly while we take local copies of the shared variables

    // take a local copy of which channels were updated in case we need to use this in the rest of loop
    bUpdateFlags = bUpdateFlagsShared;
    
    // in the current code, the shared values are always populated
    // so we could copy them without testing the flags
    // however in the future this could change, so lets
    // only copy when the flags tell us we can.
    
    if(bUpdateFlags & PITCH_FLAG)
    {
      unPITCHIn = unPITCHInShared;
    }
    
    if(bUpdateFlags & NICK_FLAG)
    {
      unNICKIn = unNICKInShared;
    }
    
    if(bUpdateFlags & ROLL_FLAG)
    {
      unROLLIn = unROLLInShared;
    }
    
    if(bUpdateFlags & YAW_FLAG)
    {
      unYAWIn = unYAWInShared;
    }
     
    // clear shared copy of updated flags as we have already taken the updates
    // we still have a local copy if we need to use it in bUpdateFlags
    bUpdateFlagsShared = 0;


    interrupts(); // we have local copies of the inputs, so now we can turn interrupts back on
    // as soon as interrupts are back on, we can no longer use the shared copies, the interrupt
    // service routines own these and could update them at any time. During the update, the
    // shared copies may contain junk. Luckily we have our local copies to work with :-)
  }
  
  // do any processing from here onwards
  // only use the local values unROLLIn, unPITCHIn and unNICKIn unYAWIn, the shared
  // variables unROLLInShared, unPITCHInShared, unNICKInShared are always owned by
  // the interrupt routines and should not be used in loop
  
    nickinput = (unNICKIn-1500); // unNICKIn signal stick up 1000, stick down 2000 becomes -500 +500
    rollinput = (unROLLIn-1500); // unROLLIn signal stick left 1000, stick right 2000 becomes -500 +500
    yawinput  = ((unYAWIn-1480)*10); // unYAWIn signal stick left 1000, stick right 2000 becomes -500 +500
   
    yaw = (newyaw + yawinput);     // Flightdirection newyaw + yawinput (-500 tm +500)

    newyaw=yaw;  //newyaw is equal to yaw so its vallue is wil be increased by yawinput in next loopcycle.
    
    yaw = map(newyaw, -32767, 32767, 0, 359); // keep yaw within 0 to 359 becouse int are -32767 to 32767.

    
    /* In order to make the led burn longer (+10° and -10° from yaw) when it passen headingdegrees.
    And to make it work around 0° and 359° the next calculation:
    */
    yawmin = (yaw -10);  //LED burns at yaw -10°
    yawplus = (yaw +10);  // LED burns at yaw +10°
    
    yawverschilPLUS  = (10 - (359 - yaw)); // 10 minus the difference between 359° and yaw (this has to be added to 0°).
    yawverschilMIN  = (10 - yaw );  // 10 minus the difference between 0° and yaw (this has to be subtracted from 359°).


    // clockwise (degrees getting lower vallue)

        if (yawmin > 349) //when yawmin is under 354° (yaw would be 359° at this piont)
    {
    yawmin= 359 - yawverschilMIN;    //yaw becomes 359° - yawverschilMIN.
  }
  
   if (yawplus > 359) //When yawplus becomes lager then 359°
    {
    yawplus = yawverschilPLUS;    //yawplus becomes 0 + yawverschilPLUS.
  }
  
   //counterclockwise (degrees getting higher vallue)

  if (yawmin < 0) //when yawmin is under 0°
    {
    yawmin= 359 - yawverschilMIN;  // yawmin becomes 359 - yawverschilMIN.
  }
  
  if (yawplus < 10) //when yaw is under 0°
    {
    yawplus= yawverschilPLUS;  // yawplus becomes 0 + yawverschilPLUS.
  }
    
    
  if (headingDegrees >= yawmin && headingDegrees <= yawplus) //is heading between yawmin and yawplus??
    
  {digitalWrite (13, HIGH); // Then LED ON
   cyclestart =millis(); //start couting cyclespeed
   cyclespeed = cyclecounter; //cyclespeed needs the vallue cyclecounter has at this piont in loop
  }
  else
  {digitalWrite (13, LOW); //else LED OFF
  }
    
  if ((headingDegrees >= yawmin && headingDegrees <= yawplus)-1) //is heading between yawmin and yawplus MINUS 1°!!
    
    {cyclecounter =(millis()-cyclestart); //stop counting cyclespeed
}  
  
 //if (cyclespeed < 100000) // when the monocopter has takeoffspeed..
  Calc_cycles();  //nick and roll are calculated



//waitvalue is needed to delay the nickcycle and rollcycle to output. 
//with the potentiometer i want to change delay time for field callibration of nick and roll
//not working right now!!

if (analogRead(potPin)>=100 )
{
waitvalue = 100.0/(analogRead(potPin)); // waitvallue is 0,1 to 1
}
Serial.print(analogRead(potPin)); Serial.print("potpin");
Serial.print(waitvalue); Serial.print("waitvalue");
int delayedrollcycle= 0;
int delayednickcycle= 0;
currentwaitfactor = millis();


if (currentwaitfactor-previouswaitfactor == cyclespeed*waitvalue) //
{
delayedrollcycle==rollcycle;
delayednickcycle==nickcycle;
previouswaitfactor=currentwaitfactor;
}
//end of callibration of nick and roll
  
  
// this output needs to be blocked at 1000 to 2000
servo.write(unPITCHIn + delayedrollcycle + delayednickcycle);
  
   // we are checking to see if the channel value has changed, this is indicated
  // by the flags. For the simple pass through we don't really need this check,
  // but for a more complex project where a new signal requires significant processing
  // this allows us to only calculate new values when we have new inputs, rather than
  // on every cycle.
  if(bUpdateFlags & PITCH_FLAG)
  {
    if(servoPITCH.readMicroseconds() != unPITCHIn)
    {
      servoPITCH.writeMicroseconds(unPITCHIn);
    }
  }
  
  if(bUpdateFlags & NICK_FLAG)
  {
    if(servoNICK.readMicroseconds() != unNICKIn)
    {
      servoNICK.writeMicroseconds(unNICKIn);
    }
  }
  
  if(bUpdateFlags & ROLL_FLAG)
  {
    if(servoROLL.readMicroseconds() != unROLLIn)
    {
      servoROLL.writeMicroseconds(unROLLIn);
    }
  }

 if(bUpdateFlags & YAW_FLAG)
  {
    if(servoYAW.readMicroseconds() != unYAWIn)
    {
      servoYAW.writeMicroseconds(unYAWIn);
    }
  }
  
  bUpdateFlags = 0;
  Output();
  
 //Serial.print(micros()-time0);Serial.println("loopend"); // for reading loopspeed
}

// Output the data down the serial port.
void Output()
{
    //Serial.print("rollpuls:\t");
    //Serial.print (unROLLIn); // geeft de rollpuls weer
    
    //Serial.print("\tcyclestart.\t"); //geeft de tijd weer sinds de eerste omwenteling
    //Serial.print(cyclestart);
  
    //Serial.print("\tcyclecounter.\t"); //geeft weer hoe lang de huidige omwenteling loopt
   //Serial.print(cyclecounter);
 
    //Serial.print("\tcyclespeed.\t"); //geeft weer hoe lang de vorrige omwenteling duurde
    //Serial.println(cyclespeed);
    
    //Serial.print("\tpart.\t"); //geeft weer hoe lang de vorrige omwenteling duurde
    //Serial.println(cyclepart);
   
   //Serial.print("\tkompas.\t"); // geeft de aan waar de vleugel zich bevind
   //Serial.println(headingDegrees);
 
   // Serial.print("\tinput.\t"); //geeft yawinputsignaal weer
   // Serial.print(yawinput);
  
    //Serial.print("\tyaw.\t"); //geeft de voorkant van de monocopter
    //Serial.print(yaw);
  
    //Serial.print("\tyawmin.\t"); // geeft weer waar de vleugel zich bevind -10
    //Serial.print(yawmin);
    
    //Serial.print("\tyawplus.\t"); // geeft weer waar de vleugel zich bevind +10
    //Serial.print(yawplus);
    
    //Serial.print("\tnickinput.\t"); //geeft weer hoe lang de vorrige omwenteling duurde
    //Serial.print(nickinput);
  
    //Serial.print("\trollinput.\t"); //geeft één deel van het rollsignaal weer
    //Serial.print(rollinput);
  
    //Serial.print("\tnickinneg.\t"); //geeft weer hoe lang de vorrige omwenteling duurde
    //Serial.println(nickinneg);
  
    //Serial.print("\trollinneg.\t"); //geeft één deel van het rollsignaal weer
    //Serial.println(rollinneg);
   
}


/* This function will calculate the signal that is needed to steer the clockwise rotating monocopter. 
It wil run after the monocopter has reached takeoff speed. 
maximum lift needs to take place 270° before commanded flightdirection ie, maximum lift for command nick in direction of yaw at 270° before yaw.
Command Nick backwards maximun lift at 90° before yaw. Command roll right maximun lift at 180°from yaw. Command roll left maximun lift at yaw*/
void Calc_cycles (void)
  { 
  cyclepart = (cyclespeed/40);

    nickinneg = map(nickinput, -500, 500, 500, -500); // th+cycleparte inverse of rollinput needed for rollcycle front side
    rollinneg = map(rollinput, -500, 500, 500, -500); // the inverse of nickinput needed for nickcycle left side
   
//first kwart 
if (cyclecounter >= 0 && cyclecounter <= ((int (cyclepart*9))+cyclepart)) // if not skip the first 10 calculations
{  Serial.println("1");

 if (cyclecounter>=0 && cyclecounter<=cyclepart)//start of the cycle.This is the first cyclepart
 { rollcycle = rollinneg; //rollinput-negatif has 100% effect
   nickcycle = 0.1*nickinput; // nickinput has 10% effect
 }

 if (cyclecounter>=(int (cyclepart*1))&&cyclecounter<=(int (cyclepart*1))+cyclepart) //the second cyclepart
 { rollcycle = 0.9*rollinneg; //rollinput-negatif has 90% effect
   nickcycle = 0.2*nickinput; //nickinput has 20% effect
 }
 
 if (cyclecounter>=(int (cyclepart*2))&&cyclecounter<=(int (cyclepart*2))+cyclepart) //the 3rd cyclepart
 { rollcycle = 0.8*rollinneg; //rollinput-negatif has 80% effect
   nickcycle = 0,3*nickinput; //nickinput has 30% effect
 }
 
 if (cyclecounter>=(int (cyclepart*3))&&cyclecounter<=(int (cyclepart*3))+cyclepart)//the 4th cyclepart
 { rollcycle = 0.7*rollinneg; //rollinput-negatif has 70% effect
   nickcycle = 0.4*nickinput; //nickinput has 40% effect
 }
 
 if (cyclecounter>=(int (cyclepart*4))&&cyclecounter<=(int (cyclepart*4))+cyclepart)//the 5th cyclepart
 { rollcycle = 0.6*rollinneg; //rollinput-negatif has 60% effect
   nickcycle = 0.5*nickinput; //nickinput has 50% effect
 }
 
 if (cyclecounter>=(int (cyclepart*5))&&cyclecounter<=(int (cyclepart*5))+cyclepart)//the 6th cyclepart
 { rollcycle = 0.5*rollinneg; //rollinput-negatif has 50% effect
   nickcycle = 0.6*nickinput; //nickinput has 60% effect
 }

 if (cyclecounter>=(int (cyclepart*6))&&cyclecounter<=(int (cyclepart*6))+cyclepart)//the 7th cyclepart
 { rollcycle = 0.4*rollinneg; //rollinput-negatif has 40% effect
   nickcycle = 0.7*nickinput; //nickinput has 70% effect
 }
 
 if (cyclecounter>=(int (cyclepart*7))&&cyclecounter<=(int (cyclepart*7))+cyclepart)//the 8th cyclepart
 { rollcycle = 0.3*rollinneg; //rollinput-negatif has 30% effect
   nickcycle = 0.8*nickinput; //nickinput has 80% effect
 }
 
 if (cyclecounter>=(int (cyclepart*8))&&cyclecounter<=(int (cyclepart*8))+cyclepart)//the 9th cyclepart
 { rollcycle = 0.2*rollinneg; //rollinput-negatif has 20% effect
   nickcycle = 0.9*nickinput; //nickinput has 90% effect
 }
 
  if (cyclecounter>=(int (cyclepart*9))&&cyclecounter<=(int (cyclepart*9))+cyclepart)//the 10th cyclepart
 { rollcycle = 0.1*rollinneg; //rollinput-negatif has 10% effect
   nickcycle = nickinput; //nickinput has 100% effect
 }
}

 // second kwart starts here
 if (cyclecounter >= (int (cyclepart*10)) && cyclecounter <= ((int (cyclepart*19))+cyclepart)) // if not skip the next 10 calculations
{   Serial.println("2");

  if (cyclecounter>=(int (cyclepart*10))&&cyclecounter<=(int (cyclepart*10))+cyclepart)//the 11th cyclepart
 { rollcycle = 0.1*rollinput; //rollinput has 10% effect
   nickcycle = nickinput; //nickinput has 100% effect
 }
 
  if (cyclecounter>=(int (cyclepart*11))&&cyclecounter<=(int (cyclepart*11))+cyclepart)//the 12th cyclepart
 { rollcycle = 0.2*rollinput; //rollinput has 20% effect
   nickcycle = 0.9*nickinput; //nickinput has 90% effect
 }
  
  if (cyclecounter>=(int (cyclepart*12))&&cyclecounter<=(int (cyclepart*12))+cyclepart)//the 13th cyclepart
 { rollcycle = 0.3*rollinput; //rollinput has 30% effect
   nickcycle = 0.8*nickinput; //nickinput has 80% effect
 }
  
  if (cyclecounter>=(int (cyclepart*13))&&cyclecounter<=(int (cyclepart*13))+cyclepart)//the 14th cyclepart
 { rollcycle = 0.4*rollinput; //rollinput has 40% effect
   nickcycle = 0.7*nickinput; //nickinput has 70% effect
 }
 
  if (cyclecounter>=(int (cyclepart*14))&&cyclecounter<=(int (cyclepart*14))+cyclepart)//the 15th cyclepart
 { rollcycle = 0.5*rollinput; //rollinput has 50% effect
   nickcycle = 0.6*nickinput; //nickinput-negatif has 60% effect
 }
 
  if (cyclecounter>=(int (cyclepart*15))&&cyclecounter<=(int (cyclepart*15))+cyclepart)//the 16th cyclepart
 { rollcycle = 0.6*rollinput; //rollinput has 60% effect
   nickcycle = 0.5*nickinput; //nickinput has 50% effect
 }
 
  if (cyclecounter>=(int (cyclepart*16))&&cyclecounter<=(int (cyclepart*16))+cyclepart)//the 17th cyclepart
 { rollcycle = 0.7*rollinput; //rollinput has 70% effect
   nickcycle = 0.4*nickinput; //nickinput has 40% effect
 }
 
  if (cyclecounter>=(int (cyclepart*17))&&cyclecounter<=(int (cyclepart*17))+cyclepart)//the 18th cyclepart
 { rollcycle = 0.8*rollinput; //rollinput has 80% effect
   nickcycle = 0.3*nickinput; //nickinput has 30% effect
 }
 
  if (cyclecounter>=(int (cyclepart*18))&&cyclecounter<=(int (cyclepart*18))+cyclepart)//the 19th cyclepart
 { rollcycle = 0.9*rollinput; //rollinput has 90% effect
   nickcycle = 0.2*nickinput; //nickinput has 20% effect
 }
 
  if (cyclecounter>=(int (cyclepart*19))&&cyclecounter<=(int (cyclepart*19))+cyclepart)//the 20th cyclepart
 { rollcycle = rollinput; //rollinput has 100% effect
   nickcycle = 0.1*nickinput; //nickinput has 10% effect
 }
 }
 
// half cycle done, last half starts here
if (cyclecounter >= (int (cyclepart*20)) && cyclecounter <= ((int (cyclepart*29))+cyclepart)) // if not skip 10 calculations
{  Serial.println("3");

  if (cyclecounter>=(int (cyclepart*20))&&cyclecounter<=(int (cyclepart*20))+cyclepart)//the 21th cyclepart
 { rollcycle = rollinput; //rollinput has 100% effect
   nickcycle = 0.1*nickinneg; //nickinput-negatif has 10% effect
 }
 
  if (cyclecounter>=(int (cyclepart*21))&&cyclecounter<=(int (cyclepart*21))+cyclepart)//the 22th cyclepart
 { rollcycle = 0.9*rollinput; //rollinput has 90% effect
   nickcycle = 0.2*nickinneg; //nickinput-negatif has 20% effect
 }
 
  if (cyclecounter>=(int (cyclepart*22))&&cyclecounter<=(int (cyclepart*22))+cyclepart)//the 23th cyclepart
 { rollcycle = 0.8*rollinput; //rollinput has 80% effect
   nickcycle = 0.3*nickinneg; //nickinput-negatif has 30% effect
 }
 
  if (cyclecounter>=(int (cyclepart*23))&&cyclecounter<=(int (cyclepart*23))+cyclepart)//the 24th cyclepart
 { rollcycle = 0.7*rollinput; //rollinput has 70% effect
   nickcycle = 0.4*nickinneg; //nickinput-negatif has 40% effect
 }
 
  if (cyclecounter>=(int (cyclepart*24))&&cyclecounter<=(int (cyclepart*24))+cyclepart)//the 25th cyclepart
 { rollcycle = 0.6*rollinput; //rollinput has 60% effect
   nickcycle = 0.5*nickinneg; //nickinput-negatif has 50% effect
 }
 
  if (cyclecounter>=(int (cyclepart*25))&&cyclecounter<=(int (cyclepart*25))+cyclepart)//the 26th cyclepart
 { rollcycle = 0.5*rollinput; //rollinput has 50% effect
   nickcycle = 0.6*nickinneg; //nickinput-negatif has 60% effect
 }
 
  if (cyclecounter>=(int (cyclepart*26))&&cyclecounter<=(int (cyclepart*26))+cyclepart)//the 27th cyclepart
 { rollcycle = 0.4*rollinput; //rollinput has 40% effect
   nickcycle = 0.7*nickinneg; //nickinput-negatif has 70% effect
 }
 
  if (cyclecounter>=(int (cyclepart*27))&&cyclecounter<=(int (cyclepart*27))+cyclepart)//the 28th cyclepart
 { rollcycle = 0.3*rollinput; //rollinput has 30% effect
   nickcycle = 0.8*nickinneg; //nickinput-negatif has 80% effect
 }
 
  if (cyclecounter>=(int (cyclepart*28))&&cyclecounter<=(int (cyclepart*28))+cyclepart)//the 29th cyclepart
 { rollcycle = 0.2*rollinput; //rollinput has 20% effect
   nickcycle = 0.9*nickinneg; //nickinput-negatif has 90% effect
 }

  if (cyclecounter>=(int (cyclepart*29))&&cyclecounter<=(int (cyclepart*29))+cyclepart)//the 30th cyclepart
 { rollcycle = 0.1*rollinput; //rollinput has 10% effect
   nickcycle = nickinneg; //nickinput-negatif has 100% effect
 }
}

  //last kwart start here
if (cyclecounter >= (int (cyclepart*30)) && cyclecounter <= ((int (cyclepart*39))+cyclepart)) // if not skip the next 10 calculations
{  Serial.println("4");

  if (cyclecounter>=(int (cyclepart*30))&&cyclecounter<=(int (cyclepart*30))+cyclepart)//the 31th cyclepart
 { rollcycle = 0.1*rollinneg; //rollinput-negatif has 10% effect
   nickcycle = nickinneg; //nickinput-negatif has 100% effect
 }
 
  if (cyclecounter>=(int (cyclepart*31))&&cyclecounter<=(int (cyclepart*31))+cyclepart)//the 32th cyclepart
 { rollcycle = 0.2*rollinneg; //rollinput-negatif has 20% effect
   nickcycle = 0.9*nickinneg; //nickinput-negatif has 90% effect
 }
 
  if (cyclecounter>=(int (cyclepart*32))&&cyclecounter<=(int (cyclepart*32))+cyclepart)//the 33th cyclepart
 { rollcycle = 0.3*rollinneg; //rollinput-negatif has 30% effect
   nickcycle = 0.8*nickinneg; //nickinput-negatif has 80% effect
 }
 
  if (cyclecounter>=(int (cyclepart*33))&&cyclecounter<=(int (cyclepart*33))+cyclepart)//the 34th cyclepart
 { rollcycle = 0.4*rollinneg; //rollinput-negatif has 40% effect
   nickcycle = 0.7*nickinneg; //nickinput-negatif has 70% effect
 }
 
  if (cyclecounter>=(int (cyclepart*34))&&cyclecounter<=(int (cyclepart*34))+cyclepart)//the 35th cyclepart
 { rollcycle = 0.5*rollinneg; //rollinput-negatif has 50% effect
   nickcycle = 0.6*nickinneg; //nickinput-negatif has 60% effect
 }
 
  if (cyclecounter>=(int (cyclepart*35))&&cyclecounter<=(int (cyclepart*35))+cyclepart)//the 36th cyclepart
 { rollcycle = 0.6*rollinneg; //rollinput-negatif has 60% effect
   nickcycle = 0.5*nickinneg; //nickinput-negatif has 50% effect
 }
 
  if (cyclecounter>=(int (cyclepart*36))&&cyclecounter<=(int (cyclepart*36))+cyclepart)//the 37th cyclepart
 { rollcycle = 0.7*rollinput; //rollinput-negatif has 70% effect
   nickcycle = 0.4*nickinneg; //nickinput-negatif has 40% effect
 }
 
  if (cyclecounter>=(int (cyclepart*37))&&cyclecounter<=(int (cyclepart*37))+cyclepart)//the 38th cyclepart
 { rollcycle = 0.8*rollinneg; //rollinput-negatif has 80% effect
   nickcycle = 0.3*nickinneg; //nickinput-negatif has 30% effect
 }
 
  if (cyclecounter>=(int (cyclepart*38))&&cyclecounter<=(int (cyclepart*38))+cyclepart)//the 39th cyclepart
 { rollcycle = 0.9*rollinneg; //rollinput-negatif has 90% effect
   nickcycle = 0.2*nickinneg; //nickinput-negatif has 20% effect
 }
 
  if (cyclecounter>=(int (cyclepart*39))+cyclepart)//the last cycle part
 { rollcycle = rollinneg; //rollinput-negatif has 100% effect
   nickcycle = 0.1*nickinneg; //nickinput-negatif has 10% effect
 } 

 
  /* Serial.print(int (cyclecounter)); Serial.print(",");
   Serial.print(int (cyclepart*1)); Serial.print(",");
   Serial.print(int (cyclepart*2)); Serial.print(",");
   Serial.print(int (cyclepart*3)); Serial.print(",");
   Serial.print(int (cyclepart*4)); Serial.print(",");
   Serial.print(int (cyclepart*5)); Serial.print(",");
   Serial.print(int (cyclepart*6)); Serial.print(",");
   Serial.print(int (cyclepart*7)); Serial.print(",");
   Serial.print(int (cyclepart*8)); Serial.print(","); 
   Serial.print(int (cyclepart*9)); Serial.print(",");
   Serial.print(int (cyclepart*10)); Serial.print(",");
   Serial.print(int (cyclepart*11)); Serial.print(",");
   Serial.print(int (cyclepart*12)); Serial.print(",");
   Serial.print(int (cyclepart*13)); Serial.print(",");
   Serial.print(int (cyclepart*14)); Serial.print(",");
   Serial.print(int (cyclepart*15)); Serial.print(",");
   Serial.print(int (cyclepart*16)); Serial.print(",");
   Serial.print(int (cyclepart*17)); Serial.print(",");
   Serial.print(int (cyclepart*18)); Serial.print(",");
   Serial.print(int (cyclepart*19)); Serial.print(",");
   Serial.print(int (cyclepart*20)); Serial.print(",");
   Serial.print(int (cyclepart*21)); Serial.print(",");
   Serial.print(int (cyclepart*22)); Serial.print(",");
   Serial.print(int (cyclepart*23)); Serial.print(",");
   Serial.print(int (cyclepart*24)); Serial.print(",");
   Serial.print(int (cyclepart*25)); Serial.print(",");
   Serial.print(int (cyclepart*26)); Serial.print(",");
   Serial.print(int (cyclepart*27)); Serial.print(",");
   Serial.print(int (cyclepart*28)); Serial.print(","); 
   Serial.print(int (cyclepart*29)); Serial.print(",");
   Serial.print(int (cyclepart*30)); Serial.print(",");
   Serial.print(int (cyclepart*31)); Serial.print(",");
   Serial.print(int (cyclepart*32)); Serial.print(",");
   Serial.print(int (cyclepart*33)); Serial.print(",");
   Serial.print(int (cyclepart*34)); Serial.print(",");
   Serial.print(int (cyclepart*35)); Serial.print(",");
   Serial.print(int (cyclepart*36)); Serial.print(",");
   Serial.print(int (cyclepart*37)); Serial.print(",");
   Serial.print(int (cyclepart*38)); Serial.print(",");
   Serial.print(int (cyclepart*39)); Serial.println(",");
    //Serial.println(cyclepart);
    */
    
    //Serial.print("Nick"); Serial.print(nickcycle); // nicksignaal naar servo
    //Serial.print("");
    //Serial.print("Roll"); Serial.println(rollcycle); // nicksignaal naar servo
    //Serial.print(micros()-time0);Serial.println("na calc");

  }   
   
   
/* This function will initialise the module and only needs to be run once
   after the module is first powered up or reset */
void configMag() {
  uint8_t mag_name;
  
  // make sure that the device is connected
  Wire.beginTransmission(MAG_ADDRESS);
  Wire.write((byte) 0x0A); // Identification Register A
  Wire.endTransmission();
  
  Wire.beginTransmission(MAG_ADDRESS);
  Wire.requestFrom(MAG_ADDRESS, 1);
  mag_name = Wire.read();
  Wire.endTransmission();
  
  if(mag_name != 0x48) {
    Serial.println("HMC5883L not found!");
    Serial.print(mag_name, HEX); Serial.println(" found, should be 0x48");
    delay(1000);
  }
  
  // Register 0x00: CONFIG_A
  // normal measurement mode (0x00) and 75 Hz ODR (0x18)
  Wire.beginTransmission(MAG_ADDRESS);
  Wire.write((byte) 0x00);
  Wire.write((byte) 0x18);
  Wire.endTransmission();
  delay(5);
  
  // Register 0x01: CONFIG_B
  // default range of +/- 130 uT (0x20)
  Wire.beginTransmission(MAG_ADDRESS);
  Wire.write((byte) 0x01);
  Wire.write((byte) 0x20);
  Wire.endTransmission();
  delay(5);
  
  // Register 0x02: MODE
  // continuous measurement mode at configured ODR (0x00)
  // possible to achieve 160 Hz by using single measurement mode (0x01) and DRDY
  Wire.beginTransmission(MAG_ADDRESS);
  Wire.write((byte) 0x02);
  Wire.write((byte) 0x01);
  Wire.endTransmission();
  
  delay(200);
  
}

// read 6 bytes (x,y,z magnetic field measurements) from the magnetometer
void readMag() {
  
  // multibyte burst read of data registers (from 0x03 to 0x08)
  Wire.beginTransmission(MAG_ADDRESS);
  Wire.write((byte) 0x03); // the address of the first data byte
  Wire.endTransmission();
  
  Wire.beginTransmission(MAG_ADDRESS);
  Wire.requestFrom(MAG_ADDRESS, 6); // Request 6 bytes
  int i = 0;
  while(Wire.available() >0 && i < 6)
  { 
    mag_buffer[i] = Wire.read(); // Read one byte
    i++;
  }
  Wire.read();
  Wire.endTransmission();
  
  // combine the raw data into full integers (HMC588L sends MSB first)
  // ________ MSB _______ _____ LSB ____
  mag_raw[0] = (mag_buffer[0] << 8) | mag_buffer[1]; //x
  mag_raw[1] = (mag_buffer[2] << 8) | mag_buffer[3]; //z
  mag_raw[2] = (mag_buffer[4] << 8) | mag_buffer[5]; //y
  
  // put the device back into single measurement mode
  Wire.beginTransmission(MAG_ADDRESS);
  Wire.write((byte) 0x02);
  Wire.write((byte) 0x01);
  Wire.endTransmission();
  
}

// simple interrupt service routine
void calcPITCH()
{
  // if the pin is high, its a rising edge of the signal pulse, so lets record its value
  if(digitalRead(PITCH_IN_PIN) == HIGH)
  { 
    ulPITCHStart = micros();
  }
  else
  {
    // else it must be a falling edge, so lets get the time and subtract the time of the rising edge
    // this gives use the time between the rising and falling edges i.e. the pulse duration.
    unPITCHInShared = (uint16_t)(micros() - ulPITCHStart);
    // use set the PITCH flag to indicate that a new PITCH signal has been received
    bUpdateFlagsShared |= PITCH_FLAG;
  }
}

void calcNICK()
{
  if(digitalRead(NICK_IN_PIN) == HIGH)
  { 
    ulNICKStart = micros();
  }
  else
  {
    unNICKInShared = (uint16_t)(micros() - ulNICKStart);
    bUpdateFlagsShared |= NICK_FLAG;
  }
}

void calcROLL()
{
  if(digitalRead(ROLL_IN_PIN) == HIGH)
  { 
    ulROLLStart = micros();
  }
  else
  {
    unROLLInShared = (uint16_t)(micros() - ulROLLStart);
    bUpdateFlagsShared |= ROLL_FLAG;
  }
}
  
void calcYAW()
{
  if(digitalRead(YAW_IN_PIN) == HIGH)
  { 
    ulYAWStart = micros();
  }
  else
  {
    unYAWInShared = (uint16_t)(micros() - ulYAWStart);
    bUpdateFlagsShared |= YAW_FLAG;
  }
}


Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: analoog signaal vertraagd uitsturen.

Berichtdoor nicoverduin » 02 Jan 2014, 00:07

indrukwekkend. Maar mocht je denken dat ik nu je hele code ga doorspitten :)
Neen. Daar heb ik de tijd niet voor.
Dus als je in eenvoudige tekst kan toelichten wat je wilt doen met die 40 stappen wil ik best meedenken. Anders veel succes.
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Berichten: 28
Geregistreerd: 30 Nov 2013, 23:49

Re: analoog signaal vertraagd uitsturen.

Berichtdoor Luppie » 02 Jan 2014, 18:24

Ik heb voor de code een tekeningetje gemaakt dat het één en ander moet verklaren want het is niet eenvoudig geloof ik :roll:

Ik heb alleen een fout gemaakt in de tekening want cyclepart 0 begint precies op yaw dus alle cycleparts draaien mee met yaw. Yaw is dus de voorkant.

het ledje brand tussen yawmin en yawplus.

Die 40 stappen zie je in de tekening weer als 40 stukjes van de omwenteling. Bij elk stukje vertel ik hoeveel invloed in % een bepaalde stuurbeweging (nick en roll) mag hebben.

De beweging van de servo zal echter enkele cycleparts later een feit zijn door vertraging in loopsnelheid en de servo. Ik denk dat ik de oplossing moet zoeken in het vertragen van het ledje want dat is mijn enige referentie met de voorkant.

Afbeelding

zo ziet dat er dan uit:
Afbeelding

en op film
http://youtu.be/ABg48tzxPac

Gebruikers-avatar
Berichten: 5043
Geregistreerd: 13 Mei 2013, 20:57
Woonplaats: Heemskerk

Re: analoog signaal vertraagd uitsturen.

Berichtdoor nicoverduin » 02 Jan 2014, 19:55

Nou ik heb het filmpje bekeken in de hoop dat hij ook werkelijk de lucht in gaat !! en Jawel tegen het einde inderdaad :)
Maar wat wil je dan met dit hele gebeuren? Maw wat is dan precies de bedoeling van die schijf?
Docent HBO Technische Informatica, Embedded ontwikkelaar & elektronicus
http://www.verelec.nl

Volgende

Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: lfauher en 102 gasten