eBoard ๐Ÿ‰  โ‘ โ‘งโ‘จ
Written for SIA 2017/2018
Servo Class Reference

#include <eBoard.h>

Public Member Functions

 Servo ()
 
uint8_t attach (int pin)
 
uint8_t attach (int pin, int min, int max)
 
void detach ()
 
void write (int value)
 
void writeMicroseconds (int value)
 
int read ()
 
int readMicroseconds ()
 
bool attached ()
 

Private Attributes

uint8_t servoIndex
 
int8_t min
 
int8_t max
 

Detailed Description

Definition at line 3580 of file eBoard.h.

Constructor & Destructor Documentation

◆ Servo()

Servo::Servo ( )

Definition at line 3685 of file eBoard.h.

References DEFAULT_PULSE_WIDTH, INVALID_SERVO, MAX_SERVOS, ServoCount, servoIndex, servos, servo_t::ticks, and usToTicks.

3685  {
3686  if( ServoCount < MAX_SERVOS) {
3687  this->servoIndex = ServoCount++; // assign a servo index to this instance
3688  servos[this->servoIndex].ticks = usToTicks(DEFAULT_PULSE_WIDTH); // store default values - 12 Aug 2009
3689  }
3690  else
3691  this->servoIndex = INVALID_SERVO ; // too many servos
3692  }
#define MAX_SERVOS
Definition: eBoard.h:3570
uint8_t ServoCount
Definition: eBoard.h:3602
static servo_t servos[(_Nbr_16timers *12)]
Definition: eBoard.h:3600
#define DEFAULT_PULSE_WIDTH
Definition: eBoard.h:3567
volatile unsigned int ticks
Definition: eBoard.h:3578
#define INVALID_SERVO
Definition: eBoard.h:3571
#define usToTicks(_us)
Definition: eBoard.h:3596
uint8_t servoIndex
Definition: eBoard.h:3592

Member Function Documentation

◆ attach() [1/2]

uint8_t Servo::attach ( int  pin)

Definition at line 3693 of file eBoard.h.

References MAX_PULSE_WIDTH, and MIN_PULSE_WIDTH.

Referenced by setup().

3693  {
3694  return this->attach(pin, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
3695  }
#define MIN_PULSE_WIDTH
Definition: eBoard.h:3565
#define MAX_PULSE_WIDTH
Definition: eBoard.h:3566
uint8_t attach(int pin)
Definition: eBoard.h:3693
+ Here is the caller graph for this function:

◆ attach() [2/2]

uint8_t Servo::attach ( int  pin,
int  min,
int  max 
)

Definition at line 3696 of file eBoard.h.

References initISR(), ServoPin_t::isActive, isTimerActive(), max, MAX_PULSE_WIDTH, MAX_SERVOS, min, MIN_PULSE_WIDTH, ServoPin_t::nbr, servo_t::Pin, SERVO_INDEX_TO_TIMER, servoIndex, and servos.

3696  {
3697  if(this->servoIndex < MAX_SERVOS ) {
3698  pinMode( pin, OUTPUT) ; // set servo pin to output
3699  servos[this->servoIndex].Pin.nbr = pin;
3700  // todo min/max check: abs(min - MIN_PULSE_WIDTH) /4 < 128
3701  this->min = (MIN_PULSE_WIDTH - min)/4; //resolution of min/max is 4 uS
3702  this->max = (MAX_PULSE_WIDTH - max)/4;
3703  // initialize the timer if it has not already been initialized
3705  if(isTimerActive(timer) == false)
3706  initISR(timer);
3707  servos[this->servoIndex].Pin.isActive = true; // this must be set after the check for isTimerActive
3708  }
3709  return this->servoIndex ;
3710  }
#define MAX_SERVOS
Definition: eBoard.h:3570
static servo_t servos[(_Nbr_16timers *12)]
Definition: eBoard.h:3600
int8_t max
Definition: eBoard.h:3594
timer16_Sequence_t
Definition: eBoard.h:3564
#define MIN_PULSE_WIDTH
Definition: eBoard.h:3565
#define SERVO_INDEX_TO_TIMER(_servo_nbr)
Definition: eBoard.h:3604
#define MAX_PULSE_WIDTH
Definition: eBoard.h:3566
ServoPin_t Pin
Definition: eBoard.h:3577
uint8_t isActive
Definition: eBoard.h:3574
uint8_t servoIndex
Definition: eBoard.h:3592
uint8_t nbr
Definition: eBoard.h:3573
static void initISR(timer16_Sequence_t timer)
Definition: eBoard.h:3650
static boolean isTimerActive(timer16_Sequence_t timer)
Definition: eBoard.h:3677
int8_t min
Definition: eBoard.h:3593
+ Here is the call graph for this function:

◆ attached()

bool Servo::attached ( )
inline

Definition at line 3756 of file eBoard.h.

References ServoPin_t::isActive, servo_t::Pin, servoIndex, and servos.

3756  {
3757  return servos[this->servoIndex].Pin.isActive ;
3758  }
static servo_t servos[(_Nbr_16timers *12)]
Definition: eBoard.h:3600
ServoPin_t Pin
Definition: eBoard.h:3577
uint8_t isActive
Definition: eBoard.h:3574
uint8_t servoIndex
Definition: eBoard.h:3592

◆ detach()

void Servo::detach ( )

Definition at line 3711 of file eBoard.h.

References finISR(), ServoPin_t::isActive, isTimerActive(), servo_t::Pin, SERVO_INDEX_TO_TIMER, and servoIndex.

3711  {
3712  servos[this->servoIndex].Pin.isActive = false;
3714  if(isTimerActive(timer) == false) {
3715  finISR(timer);
3716  }
3717  }
static servo_t servos[(_Nbr_16timers *12)]
Definition: eBoard.h:3600
static void finISR(timer16_Sequence_t timer)
Definition: eBoard.h:3662
timer16_Sequence_t
Definition: eBoard.h:3564
#define SERVO_INDEX_TO_TIMER(_servo_nbr)
Definition: eBoard.h:3604
ServoPin_t Pin
Definition: eBoard.h:3577
uint8_t isActive
Definition: eBoard.h:3574
uint8_t servoIndex
Definition: eBoard.h:3592
static boolean isTimerActive(timer16_Sequence_t timer)
Definition: eBoard.h:3677
+ Here is the call graph for this function:

◆ read()

int Servo::read ( void  )
inline

Definition at line 3744 of file eBoard.h.

References readMicroseconds(), SERVO_MAX, and SERVO_MIN.

3745  {
3746  return map( this->readMicroseconds()+1, SERVO_MIN(), SERVO_MAX(), 0, 180);
3747  }
#define SERVO_MIN()
Definition: eBoard.h:3608
#define SERVO_MAX()
Definition: eBoard.h:3609
int readMicroseconds()
Definition: eBoard.h:3748
+ Here is the call graph for this function:

◆ readMicroseconds()

int Servo::readMicroseconds ( )

Definition at line 3748 of file eBoard.h.

References INVALID_SERVO, servoIndex, servos, ticksToUs, and TRIM_DURATION.

Referenced by read().

3748  {
3749  unsigned int pulsewidth;
3750  if( this->servoIndex != INVALID_SERVO )
3751  pulsewidth = ticksToUs(servos[this->servoIndex].ticks) + TRIM_DURATION ; // 12 aug 2009
3752  else
3753  pulsewidth = 0;
3754  return pulsewidth;
3755  }
#define TRIM_DURATION
Definition: eBoard.h:3598
static servo_t servos[(_Nbr_16timers *12)]
Definition: eBoard.h:3600
#define ticksToUs(_ticks)
Definition: eBoard.h:3597
#define INVALID_SERVO
Definition: eBoard.h:3571
uint8_t servoIndex
Definition: eBoard.h:3592
+ Here is the caller graph for this function:

◆ write()

void Servo::write ( int  value)

Definition at line 3718 of file eBoard.h.

References MIN_PULSE_WIDTH, SERVO_MAX, SERVO_MIN, and writeMicroseconds().

Referenced by set_motor_speed(), set_steer_angle(), and setup().

3718  {
3719  if(value < MIN_PULSE_WIDTH)
3720  { // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
3721  if(value < 0) value = 0;
3722  if(value > 180) value = 180;
3723  value = map(value, 0, 180, SERVO_MIN(), SERVO_MAX());
3724  }
3725  this->writeMicroseconds(value);
3726  }
#define SERVO_MIN()
Definition: eBoard.h:3608
#define SERVO_MAX()
Definition: eBoard.h:3609
#define MIN_PULSE_WIDTH
Definition: eBoard.h:3565
void writeMicroseconds(int value)
Definition: eBoard.h:3727
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ writeMicroseconds()

void Servo::writeMicroseconds ( int  value)

Definition at line 3727 of file eBoard.h.

References MAX_SERVOS, SERVO_MAX, SERVO_MIN, servoIndex, servos, servo_t::ticks, TRIM_DURATION, and usToTicks.

Referenced by write().

3727  {
3728  // calculate and store the values for the given channel
3729  byte channel = this->servoIndex;
3730  if( (channel < MAX_SERVOS) ) // ensure channel is valid
3731  {
3732  if( value < SERVO_MIN() ) // ensure pulse width is valid
3733  value = SERVO_MIN();
3734  else if( value > SERVO_MAX() )
3735  value = SERVO_MAX();
3736  value = value - TRIM_DURATION;
3737  value = usToTicks(value); // convert to ticks after compensating for interrupt overhead - 12 Aug 2009
3738  uint8_t oldSREG = SREG;
3739  cli();
3740  servos[channel].ticks = value;
3741  SREG = oldSREG;
3742  }
3743  }
#define SERVO_MIN()
Definition: eBoard.h:3608
#define MAX_SERVOS
Definition: eBoard.h:3570
#define TRIM_DURATION
Definition: eBoard.h:3598
static servo_t servos[(_Nbr_16timers *12)]
Definition: eBoard.h:3600
#define SERVO_MAX()
Definition: eBoard.h:3609
volatile unsigned int ticks
Definition: eBoard.h:3578
#define usToTicks(_us)
Definition: eBoard.h:3596
uint8_t servoIndex
Definition: eBoard.h:3592
+ Here is the caller graph for this function:

Member Data Documentation

◆ max

int8_t Servo::max
private

Definition at line 3594 of file eBoard.h.

Referenced by attach().

◆ min

int8_t Servo::min
private

Definition at line 3593 of file eBoard.h.

Referenced by attach().

◆ servoIndex

uint8_t Servo::servoIndex
private

Definition at line 3592 of file eBoard.h.

Referenced by attach(), attached(), detach(), readMicroseconds(), Servo(), and writeMicroseconds().


The documentation for this class was generated from the following file: