I am having a terrible problem where my encoders gradually loose track of position as if they are dropping ticks or something.
Is it possible that maybie the secret IFI stuff is using High priority interrupts that take a long time and are causing the LP ISR not to execute? I notice that occasionally characters printed to the terminal are dropped as well. Then again the terminal printing thing could be a buffer overrun caused by printing faster than the UART can transmit.
The relevant code is below:
#pragma code
#pragma interruptlow InterruptHandlerLow save=PROD,section(".tmpdata")
//#pragma interruptlow InterruptHandlerLow,section(".tmpdata")
void InterruptHandlerLow ()
{
HandleTurretInterrupts();
...
All you really need to look at here is the HandleTurretInterrupts() function.
/*
* FILE: turret.h
*
* Responsible for turret limits, keeping
* track of it's absolute position and performing the 'homing' sequence.
*/
//==================================================
#include "turret.h"
#include "boolean.h"
#include "hardware.h"
#include <stdio.h>
#include <p18f8722.h>
//--- private variables ---//
boolean pan_homed = false;
boolean tilt_homed = false;
//the current position of the pan and tilt axis
volatile signed int pan_position = 0;
volatile signed int tilt_position = 0;
//used for 'thread' synchronization probably unnecessary after i made the change to diable interrupts during access like kevin did in his code. Currently not used
volatile boolean panTicksBusy = false;
volatile boolean tiltTicksBusy = false;
//--- Private Prototypes --//
void Pan_Routine(void); //
void Tilt_Routine(void);
//-------Functions --------//
//used by all other pars of the code to set the pan axis speed.
void setPanSpeed(signed char speed){
rc_turret_pan = oi_joy_turret_pan;
//make sure we don't override the homin sequence
#ifdef TURRET_AUTO_HOMING
if(!pan_homed) return;
#endif
//limit range of motion
#ifdef USE_TURRET_PAN_LIMIT
panTicksBusy = true;
rc_turret_pan = ((rc_turret_pan<127)&&(pan_position<=PAN_MIN))? 127 : rc_turret_pan;
rc_turret_pan = ((rc_turret_pan>127)&&(pan_position>=PAN_MAX))? 127 : rc_turret_pan;
panTicksBusy = false;
#endif
}
//used by all other parts of the code to set the speed of the tilt axis.
void setTiltSpeed(signed char speed){
rc_turret_tilt = oi_joy_turret_tilt;
//make sure we don't override the homin sequence
#ifdef TURRET_AUTO_HOMING
if(!tilt_homed) return;
#endif
//limit range of motion
#ifdef USE_TURRET_TILT_LIMIT
tiltTicksBusy = true;
rc_turret_tilt = ((rc_turret_tilt<127)&&(tilt_position<=TILT_MIN))? 127 : rc_turret_tilt;
rc_turret_tilt = ((rc_turret_tilt>127)&&(tilt_position>=TILT_MAX))? 127 : rc_turret_tilt;
tiltTicksBusy = false;
#endif
}
//Finds the 0 position of the turret based on the limit switches.
//The axis rotate in the direction of the limit switches until the limit switches are reached. When this happens, movement stops
//and the position cound is set to 0.
void TurretHome(void){
#ifdef TURRET_AUTO_HOMING
//---pan---
if(pan_homed){
//do nothing
}else{
//find our home position
if(rc_sw_pan_home){
pan_homed = true;
panTicksBusy = true;
pan_position = 0;
panTicksBusy = false;
rc_turret_pan = 127;
}else{
rc_turret_pan = PAN_HOME_SPEED;
}
}
//---tilt---
if(tilt_homed){
if(rc_sw_tilt_home){
tilt_position = 0;
}
//do nothing/**/
}else{
//find our home position
if(rc_sw_tilt_home){
tilt_homed = true;
tiltTicksBusy = true;
tilt_position = 0;
tiltTicksBusy = false;
rc_turret_tilt = 127;
}else{
rc_turret_tilt = TILT_HOME_SPEED;
}
}
#endif
}
void TurretRoutine(void){
TurretHome();
#ifdef PRINT_TURRET_STATUS
//printf("TURR pl:%u, tl:%u, ph:%u, th:%u, p:%d, t:%d
\r",rc_sw_pan_home,rc_sw_tilt_home,pan_homed,tilt_homed,pan_position,tilt_position);
//printf("TURR pl:%u, tl:%u, pan:%d, tilt:%d
\r",rc_sw_pan_home,rc_sw_tilt_home,pan_position,tilt_position);
//printf("tilt:%d pan:%d
\r",tilt_position,pan_position);
//printf("Pan:%d Tilt:%d
\r",rc_turret_pan,rc_turret_tilt);
#endif
}
//--- called by the ISR in user_routines_fast.c ---
void HandleTurretInterrupts(void){
if(INTCON3bits.INT2IF) //this is a tilt encoder tick (rising edge of tilt A line)
{
INTCON3bits.INT2IF = 0; //clear the flag
/*
//we have an concurrent access error?
if(tiltTicksBusy)
{
printf("p!!!!!!!");
return;
}
*/
tiltTicksBusy = true;
if(rc_dig_in03) { // tilt B
tilt_position++;
} else {
tilt_position--;
}
tiltTicksBusy = false;
}
if(INTCON3bits.INT3IF) //this is a pan encoder tick (rising edge of pan A line)
{
INTCON3bits.INT3IF = 0; //clear the flag
/*
//we have an acces violation
if(panTicksBusy){
printf("p!!!!!!!");
return;
}
*/
panTicksBusy = true;
if(rc_dig_in04) { // pan B
pan_position--;
} else {
pan_position++;
}
panTicksBusy = false;
}
}
//-- return whether or not the axis' are homed and ready to use --
boolean isPanHomed(){
return pan_homed;
}
boolean isTiltHomed(){
return tilt_homed;
}
//--- configures the turrent encoders ---
void InitTurret(void){
//--- initialize positions to 0 ---
//panTicksBusy = true;
//tiltTicksBusy = true;
pan_position = 0;
tilt_position = 0;
//panTicksBusy = false;
//tiltTicksBusy = false;
/*==== ENCODER PINS ====
*Pan A: INT2/RB2/digital input 01
*Tilt A: INT3/RB3/digital input 02
*Pan B: RB4/digital input 3
*Tilt B: RB5/digital input 4
*/
//--- setup pins as inputs ---
rc_dig_iomode_01 = INPUT;
rc_dig_iomode_02 = INPUT;
rc_dig_iomode_03 = INPUT;
rc_dig_iomode_04 = INPUT;
//--- setup tilt encoder interrupt (INT2/RB2/digital input 01/pan A) ---
INTCON3bits.INT2IF = 0; //make sure flag is clear
INTCON2bits.INTEDG2 = 1; //interrupt on low->high
INTCON3bits.INT2IP = 0; //make it low priority
INTCON3bits.INT2IE = 1; //enable it
//-- setup pan encoder interrupt (INT3/RB3/digital input 02/tilt A) ---
INTCON3bits.INT3IF = 0; //make sure flag is clear
INTCON2bits.INTEDG3 = 1; //interrupt on low->high
INTCON2bits.INT3IP = 0; //make it low priority
INTCON3bits.INT3IE = 1; //enable it
printf("encoders initialized
");
//OpenRB2INT(PORTB_CHANGE_INT_ON & FALLING_EDGE_INT & PORTB_PULLUPS_OFF);
}
//-- Functions to return the position of the axis --
signed int getPanPos(void){
signed int temp;
//panTicksBusy = true;
INTCON3bits.INT3IE = 0; //temporarily diable
temp = pan_position;
INTCON3bits.INT3IE = 1; //enable
//panTicksBusy = false;
return temp;
}
signed int getTiltPos(void){
signed int temp;
//tiltTicksBusy = true;
INTCON3bits.INT2IE = 0;
temp = tilt_position;
INTCON3bits.INT2IE = 1;
//tiltTicksBusy = false;
return temp ;
}