ServoTimer2.cpp:126: error: expected primary-expression before '?'

Bonjour à tous,

Je suis entrain de tester la library servoTimer2 pour pouvoir commander des servos avec le timer2 (si le timer1 est déjà occupé!)
J’ai téléchargé la lib ici :
http://code.google.com/p/duinos/source/browse/trunk/libraries/ServoTimer2/
J’ai fais la modification pour être compatible avec la version 1.0.5 de l’IDE.

Mais la compil m’envoie le message suivant :

ServoTimer2.cpp: In function 'void writeChan(uint8_t, int)':
ServoTimer2.cpp:126: error: expected primary-expression before '?' token
ServoTimer2.cpp:126: error: expected `:' before ')' token
ServoTimer2.cpp:126: error: expected primary-expression before ')' token
ServoTimer2.cpp:127: error: expected primary-expression before '?' token
ServoTimer2.cpp:127: error: expected `:' before ';' token
ServoTimer2.cpp:127: error: expected primary-expression before ';' token
ServoTimer2.cpp:128: error: expected primary-expression before '?' token
ServoTimer2.cpp:128: error: expected `:' before ')' token
ServoTimer2.cpp:128: error: expected primary-expression before ')' token
ServoTimer2.cpp:129: error: expected primary-expression before '?' token
ServoTimer2.cpp:129: error: expected `:' before ';' token
ServoTimer2.cpp:129: error: expected primary-expression before ';' token

Voici les sources

ServoTimer2.h

// ensure this library description is only included once
#ifndef ServoTimer2_h
#define ServoTimer2_h

//#include <inttypes.h>
//#include <wiring.h>

#define MIN_PULSE_WIDTH?   750    // the shortest pulse sent to a servo  
#define MAX_PULSE_WIDTH?  2250    // the longest pulse sent to a servo
#define DEFAULT_PULSE_WIDTH  1000    // default pulse width when servo is attached
#define FRAME_SYNC_PERIOD   20000    // total frame duration in microseconds
#define NBR_CHANNELS 8   // the maximum number of channels, don't change this

typedef struct  {
uint8_t nbr    :
  5 ;  // a pin number from 0 to 31
uint8_t isActive   :
  1 ;  // false if this channel not enabled, pin only pulsed if true
} 
ServoPin_t   ;  

typedef struct {
  ServoPin_t Pin;
  byte counter;
  byte remainder;
}  
servo_t;

class ServoTimer2
{
public:
  // constructor:
  ServoTimer2();

  uint8_t attach(int);     // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure
  // the attached servo is pulsed with the current pulse width value, (see the write method)
  void detach();
  void write(int);     // store the pulse width in microseconds (between MIN_PULSE_WIDTH and MAX_PULSE_WIDTH)for this channel
  int read();    // returns current pulse width in microseconds for this servo
  boolean attached();  // return true if this servo is attached
private:
  uint8_t chanIndex;  // index into the channel data for this servo

};


// the following ServoArrayT2 class is not implimented in the first version of this library
class ServoArrayT2
{
public:
  // constructor:
  ServoArrayT2();

  uint8_t attach(int);     // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure
  // channels are assigned consecutively starting from 1
  // the attached servo is pulsed with the current pulse width value, (see the write method)
  void detach(int);    // detach the servo on the given channel
  void write(int,int);     // store the pulse width in microseconds (between MIN_PULSE_WIDTH and MAX_PULSE_WIDTH)for the given channel
  int read(int);    // returns current pulse width in microseconds for the given channel
  boolean attached(int);   // return true if the servo on the given channel is attached
private:
  uint8_t chanIndex;  // index into the channel data for this servo

};

#endif

ServoTimer2.cpp

/*
  ServoTimer2.c - Interrupt driven Servo library for Arduino using Timer2- Version 0.1
 Copyright (c) 2008 Michael Margolis.  All right reserved.
  */

#if ARDUINO >= 100
#include "Arduino.h"
#else
extern "C" {
  // AVR LibC Includes
#include <inttypes.h>
#include <avr/interrupt.h>
#include "WProgram.h"
}
#endif

#include "ServoTimer2.h"
static void initISR();  
static void writeChan(uint8_t chan, int pulsewidth);

#define FRAME_SYNC_INDEX   0   // frame sync delay is the first entry in the channel array
//#define FRAME_SYNC_PERIOD  20000     // total frame duration in microseconds
#define FRAME_SYNC_DELAY   ((FRAME_SYNC_PERIOD - ( NBR_CHANNELS * DEFAULT_PULSE_WIDTH))/ 128) // number of iterations of the ISR to get the desired frame rate
#define DELAY_ADJUST   8   // number of microseconds of calculation overhead to be subtracted from pulse timings  

static servo_t servos[NBR_CHANNELS+1];    // static array holding servo data for all channels

static volatile uint8_t Channel;   // counter holding the channel being pulsed
static volatile uint8_t ISRCount;  // iteration counter used in the interrupt routines;
uint8_t ChannelCount = 0;      // counter holding the number of attached channels
static boolean isStarted = false;  // flag to indicate if the ISR has been initialised

ISR (TIMER2_OVF_vect)
{
  ++ISRCount; // increment the overlflow counter
  if (ISRCount ==  servos[Channel].counter ) // are we on the final iteration for this channel
  {
    TCNT2 =  servos[Channel].remainder;   // yes, set count for overflow after remainder ticks
  }  
  else if(ISRCount >  servos[Channel].counter)  
  {
    // we have finished timing the channel so pulse it low and move on 
    if(servos[Channel].Pin.isActive == true)       // check if activated
      digitalWrite( servos[Channel].Pin.nbr,LOW); // pulse this channel low if active  

    Channel++;    // increment to the next channel
    ISRCount = 0; // reset the isr iteration counter
    TCNT2 = 0;    // reset the clock counter register
    if( (Channel != FRAME_SYNC_INDEX) && (Channel <= NBR_CHANNELS) ){       // check if we need to pulse this channel    
      if(servos[Channel].Pin.isActive == true)     // check if activated
        digitalWrite( servos[Channel].Pin.nbr,HIGH); // its an active channel so pulse it high  
    }
    else if(Channel > NBR_CHANNELS){
      Channel = 0; // all done so start over    
    }
  }  
}

ServoTimer2::ServoTimer2()
{
  if( ChannelCount < NBR_CHANNELS)  
    this->chanIndex = ++ChannelCount;  // assign a channel number to this instance
  else
    this->chanIndex = 0;  // todo?  // too many channels, assigning 0 inhibits this instance from functioning
}

uint8_t ServoTimer2::attach(int pin)
{
  if( isStarted == false)
    initISR();    
  if(this->chanIndex > 0)  
  {
    //debug("attaching chan = ", chanIndex);
    pinMode( pin, OUTPUT) ;  // set servo pin to output
    servos[this->chanIndex].Pin.nbr = pin;  
    servos[this->chanIndex].Pin.isActive = true;  
  }
  return this->chanIndex ;
}

void ServoTimer2::detach()  
{
  servos[this->chanIndex].Pin.isActive = false;  
}

void ServoTimer2::write(int pulsewidth)
{
  writeChan(this->chanIndex, pulsewidth); // call the static function to store the data for this servo?      
}

int ServoTimer2::read()
{
  unsigned int pulsewidth;
  if( this->chanIndex > 0)
    pulsewidth =  servos[this->chanIndex].counter * 128 + ((255 - servos[this->chanIndex].remainder) / 2) + DELAY_ADJUST ;
  else
    pulsewidth  = 0;
  return pulsewidth;  
}

boolean ServoTimer2::attached()
{
  return servos[this->chanIndex].Pin.isActive ;
}


static void writeChan(uint8_t chan, int pulsewidth)
{
  // calculate and store the values for the given channel
  if( (chan > 0) && (chan <= NBR_CHANNELS) )   // ensure channel is valid
  {
    if( pulsewidth < MIN_PULSE_WIDTH )    // ensure pulse width is valid
      pulsewidth = MIN_PULSE_WIDTH;
    else if( pulsewidth > MAX_PULSE_WIDTH )
      pulsewidth = MAX_PULSE_WIDTH;

    pulsewidth -=DELAY_ADJUST;                     // subtract the time it takes to process the start and end pulses (mostly from digitalWrite)
    servos[chan].counter = pulsewidth / 128;    
    servos[chan].remainder = 255 - (2 * (pulsewidth - ( servos[chan].counter * 128)));  // the number of 0.5us ticks for timer overflow      
  }
}


static void initISR()
{  
  for(uint8_t i=1; i <= NBR_CHANNELS; i++) {  // channels start from 1    
    writeChan(i, DEFAULT_PULSE_WIDTH);  // store default values?      
  }
  servos[FRAME_SYNC_INDEX].counter = FRAME_SYNC_DELAY;   // store the frame sync period

  Channel = 0;  // clear the channel index  
  ISRCount = 0;  // clear the value of the ISR counter;

  /* setup for timer 2 */
  TIMSK2 = 0;  // disable interrupts
  TCCR2A = 0;  // normal counting mode
  TCCR2B = _BV(CS21); // set prescaler of 8
  TCNT2 = 0;     // clear the timer2 count
  TIFR2 = _BV(TOV2);  // clear pending interrupts;
  TIMSK2 =  _BV(TOIE2) ; // enable the overflow interrupt?    

  isStarted = true;  // flag to indicate this initialisation code has been executed
}

Votre aide est la bien venue…
Olivier

2 fautes de frappes dans le .h

#define MIN_PULSE_WIDTH?   750    // the shortest pulse sent to a servo  
#define MAX_PULSE_WIDTH?  2250    // the longest pulse sent to a servo

remplacer par

#define MIN_PULSE_WIDTH   750    // the shortest pulse sent to a servo  
#define MAX_PULSE_WIDTH  2250    // the longest pulse sent to a servo

Bien vu!

J'ai quelques difficultés à télécharger la lib : il m'a mis tout un tas de '?'. Je pensais tous les avoir supprimés.

Merci de m'avoir ouvert les yeux! Olivier