Arduino Program Comand Sequencer

all about robots

Arduino Program Comand Sequencer

Postby chris on Tue Jul 30, 2013 6:30 pm

Simple program comand sequencer for Arduino.

Code: Select all
/*
  simple comand sequnece interpreter
 
*/

// sequencer comands
#define STOP  0             // stop execution
#define ON    1             // ON,pinNumber ==> digital pin on
#define OFF   2             // OFF,pinNumber ==> digital pin off
#define DELAY 3             // delay, milliseconds

// comand sequence
const uint16_t code[]={
  ON,13,
  DELAY,1000,
  OFF,13,
  DELAY,500,
  ON,13,
  DELAY,3000,
  OFF,13,
  STOP
};
void setup() {               
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);     
}

uint16_t ProgramCounter=0;
uint16_t MsTime; // time in ms
uint16_t EndTime=0;

/******************************************************************************

  void execute()
 
  Execute one comand.

******************************************************************************/
void execute()
{
  uint16_t comand=code[ProgramCounter];
  uint16_t value;
 
  switch (comand)
  {
    case ON:
    {
      ProgramCounter++;
      value=code[ProgramCounter++]; // get pin number
      digitalWrite(value,1);
    };break;
   
    case OFF:
    {
      ProgramCounter++;
      value=code[ProgramCounter++]; // get pin number
      digitalWrite(value,0);
    };break;
   
    case DELAY:
    {
      static uint8_t firstEntry=true;
      if(firstEntry) // set timer end
      {
        value=code[ProgramCounter+1]; // get wait time value
        EndTime=MsTime+value;   // set end time
        firstEntry=false;
      }else // check if time elapsed
      {
        if(MsTime>EndTime)
        { 
          firstEntry=true; // prepare for next wait
          ProgramCounter+=2; // set program counter to next comand
        }
      }
    };break;
   
    case STOP: // to nothing   
    {

    };break;
  };
 
}

void loop() {
  execute(); // execute one comand
  delay(1);  // delay 1ms
  MsTime++;  // increase time
}

/***************************************************************************

date      author         version   comment
==========      ================   ===================================
30.July.07   chris         V0.1   First implementation      


Versions:

V0.1
- first version

***************************************************************************/
/***************************************************************************
*   
*   list of authors
*   ===============
*   chris (Nickname) , chris(at)hobby-roboter.de,
*   www.hobby-roboter.de/forum/robots
*   
*
***************************************************************************
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation version 2 of the License,                *
*   If you extend the program please maintain the list of authors.        *
*   ( if there is no real name, the nick name has to be mentioned )
*   If you want to use this software for commercial purposes and you      *
*   don't want to make it open source, please contact the authors for     *
*   licensing.                                                            *
***************************************************************************/
/***************************************************************************
*   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
*   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
*   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
*   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
*   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
*   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
*   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
*   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
*   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
*   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***************************************************************************/




Links:
Kommando Sequenzer in der Raumfahrt: http://www.bernd-leitenberger.de/computer-raumfahrt2.shtml
Tastenprogrammierbare Steuerung: http://www.elektronik-labor.de/Projekte/TPS5.html
Last edited by chris on Sun Aug 04, 2013 4:00 pm, edited 1 time in total.
chris
 
Posts: 289
Joined: Sun May 25, 2008 7:21 am

Re: Arduino Program Comand Sequencer

Postby chris on Tue Jul 30, 2013 6:31 pm

Multi tasking comand sequencer

Code: Select all
/*
  simple multitasking comand sequnece interpreter
 
  This sequencer can run several sequences in parallel
 
*/
#include "sequencer.h"

// sequencer comands
#define ON       1             // ON,pinNumber ==> digital pin on
#define OFF      2             // OFF,pinNumber ==> digital pin off
#define DELAY    3             // delay, milliseconds

// labels
#define SEQUENCE 0xFFFE        // SEQUENCE, number ==> code start for a sequencer
#define STOP     0xFFFF        // stop execution


#define SEQUENCERSTOP 0xFFFF   // if the program counter equals 0xFFFF the sequencer is stopped

//#define DEBUG
// comand sequence
const uint16_t code[]=
{
  // code for sequence 0
  SEQUENCE,0,
 
  ON,13,
  DELAY,1000,
  OFF,13,
  DELAY,500,
  ON,13,
  DELAY,3000,
  OFF,13,
  STOP,
 
  // code for sequence 1
  SEQUENCE,1,
  DELAY,10000,
  ON,13,
  DELAY,500,
  OFF,13,
  STOP,
 
  // end of program memory
  // this line is mandatory
  SEQUENCE, STOP 
};

uint16_t MsTime; // time in ms

SEQUENCER_t Sequencer[NUMBEROFSEQUENCERS]; // array of sequencers

/*************************************************************************************
 
  uint16_t findSequence(uint8_t sequenceNumber)
 
  Some parts of the code may be run in different sequences. Therefore the sequences
  will run in parallel.
  This routine searces vor the start index of the code for a given sequence number.
 
  input: sequenceNumber
  ouput:
    return the start index in the code for a given sequence number
    returns SEQUNCERSTOP if the number is not found
 
*************************************************************************************/
uint16_t findStartOfSequence(uint8_t sequenceNumber)
{
  uint8_t flag=true;
  uint16_t index=0;
  uint16_t returnValue=SEQUENCERSTOP;
  while(flag)
  {
    if(code[index++]==SEQUENCE)
    {
      if(code[index]==STOP) flag=false; // stop found, return
      if(code[index]==sequenceNumber) // if sequence number found
      {
       
        index++;
        returnValue=index;
        flag=false; // sequence found, return
      }
    }
  }
  Serial.print(" sequence address: ");Serial.print(returnValue);
  return returnValue;
}
/*************************************************************************************

  setup the system
 
*************************************************************************************/
void setup()
{               
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);     
  Serial.begin(9600);
 
  uint8_t n;
 
  // initialize sequencers
  for(n=0;n<NUMBEROFSEQUENCERS;n++)
  {
    Sequencer[n].programCounter=findStartOfSequence(n);
    Sequencer[n].firstEntry=true;
    Sequencer[n].endTime=0;

  }

}

/******************************************************************************

  void execute()
 
  Execute one comand on a sequencer

******************************************************************************/
void execute(SEQUENCER_t *sq)
{
  uint16_t comand;
  uint16_t value;
  if(sq->programCounter!=0xFFFF)
  {
    comand=code[sq->programCounter];
    switch (comand)
    {
      case ON:
      {
        sq->programCounter++;
        value=code[sq->programCounter++]; // get pin number
        digitalWrite(value,1);
      };break;
     
      case OFF:
      {
        sq->programCounter++;
        value=code[sq->programCounter++]; // get pin number
        digitalWrite(value,0);
      };break;
     
      case DELAY:
      {
        if(sq->firstEntry) // set timer end
        {
          value=code[sq->programCounter+1]; // get wait time value
          sq->endTime=MsTime+value;         // set end time
          sq->firstEntry=false;
        }else // check if time elapsed
        {
          if(MsTime>sq->endTime)   
          { 
            sq->firstEntry=true;       // prepare for next wait
            sq->programCounter+=2;     // set program counter to next comand
          }
        }
      };break;
     
      case STOP:   
      {
         sq->programCounter=SEQUENCERSTOP;
      };break;
     
      default:
      {
        #ifdef DEBUG
         Serial.print("comand error");
        #endif
      }
    }
  };
  #ifdef DEBUG
   Serial.print(" pc: "); Serial.println(sq->programCounter);
  #endif
}
/******************************************************************************

  void loop()
 
  main loop

******************************************************************************/
void loop() {
  uint8_t n;
  //execute(&Sequencer[0]); // execute one comand in one sequencer
 
  for(n=0;n<NUMBEROFSEQUENCERS;n++)
  {
    #ifdef DEBUG
     Serial.print("seq: "); Serial.print(n);
    #endif
   
    execute(&Sequencer[n]); // execute one comand in one sequencer
  }
 
  delay(1);  // delay 1ms
  MsTime++;  // increase time
}

/***************************************************************************

date      author         version   comment
==========      ================   ===================================
30.July.07   chris         V0.1   First implementation      


Versions:

V0.1
- first version

***************************************************************************/
/***************************************************************************
*   
*   list of authors
*   ===============
*   chris (Nickname) , chris(at)hobby-roboter.de,
*   www.hobby-roboter.de/forum/robots
*   
*
***************************************************************************
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation version 2 of the License,                *
*   If you extend the program please maintain the list of authors.        *
*   ( if there is no real name, the nick name has to be mentioned )
*   If you want to use this software for commercial purposes and you      *
*   don't want to make it open source, please contact the authors for     *
*   licensing.                                                            *
***************************************************************************/
/***************************************************************************
*   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
*   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
*   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
*   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
*   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
*   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
*   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
*   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
*   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
*   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
***************************************************************************/



You also need the header file sequencer.h:

Code: Select all
#ifndef SEQUENCER_H
#define SEQUENCER_H

  #include <stdint.h>
  typedef struct {
    uint16_t programCounter; // program counter of sequencer
    uint16_t endTime;        // end time used from the wait function
    uint8_t firstEntry;      // flag used from wait function
  } SEQUENCER_t;
 
  extern SEQUENCER_t Sequencer[];
 
  #define NUMBEROFSEQUENCERS 2
 
#endif // SEQUENCER_H
chris
 
Posts: 289
Joined: Sun May 25, 2008 7:21 am


Return to robots

Who is online

Users browsing this forum: No registered users and 1 guest

cron