From 5a6322975c1df973544aff84e7d5ef61ebb94865 Mon Sep 17 00:00:00 2001 From: p33a Date: Tue, 26 Apr 2016 04:00:42 +0100 Subject: [PATCH] added a static refresh_flag that is set to 1 at the start of the refresh period and is set to 0 when it ends. This can be used to signal a "calm" period when the servos values can be updated. --- VarSpeedServo.cpp | 8 ++++++-- VarSpeedServo.h | 3 ++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/VarSpeedServo.cpp b/VarSpeedServo.cpp index 713adde..001337d 100644 --- a/VarSpeedServo.cpp +++ b/VarSpeedServo.cpp @@ -92,6 +92,8 @@ servoSequencePoint initSeq[] = {{0,100},{45,100}}; //sequence_t sequences[MAX_SEQUENCE]; +volatile int8_t VarSpeedServo::refresh_flag; + // convenience macros #define SERVO_INDEX_TO_TIMER(_servo_nbr) ((timer16_Sequence_t)(_servo_nbr / SERVOS_PER_TIMER)) // returns the timer controlling this servo #define SERVO_INDEX_TO_CHANNEL(_servo_nbr) (_servo_nbr % SERVOS_PER_TIMER) // returns the index of the servo on this timer @@ -105,9 +107,10 @@ servoSequencePoint initSeq[] = {{0,100},{45,100}}; static inline void handle_interrupts(timer16_Sequence_t timer, volatile uint16_t *TCNTn, volatile uint16_t* OCRnA) { - if( Channel[timer] < 0 ) + if( Channel[timer] < 0 ) { *TCNTn = 0; // channel set to -1 indicated that refresh interval completed so reset the timer - else{ + VarSpeedServo::refresh_flag = 0; + }else{ if( SERVO_INDEX(timer,Channel[timer]) < ServoCount && SERVO(timer,Channel[timer]).Pin.isActive == true ) digitalWrite( SERVO(timer,Channel[timer]).Pin.nbr,LOW); // pulse this channel low if activated } @@ -149,6 +152,7 @@ static inline void handle_interrupts(timer16_Sequence_t timer, volatile uint16_t else *OCRnA = *TCNTn + 4; // at least REFRESH_INTERVAL has elapsed Channel[timer] = -1; // this will get incremented at the end of the refresh period to start again at the first channel + VarSpeedServo::refresh_flag = 1; } } diff --git a/VarSpeedServo.h b/VarSpeedServo.h index 492c4c0..bcd5be8 100644 --- a/VarSpeedServo.h +++ b/VarSpeedServo.h @@ -114,7 +114,7 @@ typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t ; #define VarSpeedServo_VERSION 2 // software version of this library -#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo +#define MIN_PULSE_WIDTH 444 // the shortest pulse sent to a servo #define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo #define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached #define REFRESH_INTERVAL 20000 // minumim time to refresh servos in microseconds @@ -147,6 +147,7 @@ typedef struct { class VarSpeedServo { public: + static volatile int8_t refresh_flag; VarSpeedServo(); uint8_t attach(int pin); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure uint8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes.