Skip to content

Commit

Permalink
Merge branch 'master' into biomimetics-master
Browse files Browse the repository at this point in the history
Conflicts:
	ams-enc.c
  • Loading branch information
apullin committed Oct 27, 2015
2 parents c331ed5 + 998b0a5 commit b012786
Show file tree
Hide file tree
Showing 11 changed files with 1,735 additions and 20 deletions.
4 changes: 2 additions & 2 deletions adc_pid.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ static void initDma0(void) {
void __attribute__((interrupt, no_auto_psv)) _DMA0Interrupt(void) {
//This should really be done in an elegant way by selecting a pointer, and
// not repeating code below.
LED_3 = 1;
//LED_3 = 1;
if (DmaBuffer == 0) {
adc_AN0 = BufferA[0][0]; //AN0
adc_AN8 = BufferA[1][0]; //AN8
Expand All @@ -274,7 +274,7 @@ void __attribute__((interrupt, no_auto_psv)) _DMA0Interrupt(void) {
adc_MotorD = adc_AN11;

DmaBuffer ^= 1; //Toggle between buffers
LED_3 = 0;
//LED_3 = 0;
IFS0bits.DMA0IF = 0; //Clear the DMA0 Interrupt Flag
}
// End DMA section
64 changes: 55 additions & 9 deletions ams-enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
#include "utils.h"
#include "settings.h"


#define LSB2ENCDEG 0.0219
#define ENC_I2C_CHAN 1 //Encoder is on I2C channel 1

Expand All @@ -74,13 +73,7 @@ EncObj encPos[NUM_ENC];

#define AMS_ENC_ANGLE_REG 0xFE

#ifndef AMS_ENC_OFFSET_0
#define AMS_ENC_OFFSET_0 0
#endif

#ifndef AMS_ENC_OFFSET_1
#define AMS_ENC_OFFSET_1 0
#endif

volatile unsigned char state = AMS_ENC_IDLE;
volatile unsigned char encoder_number = 0;
Expand Down Expand Up @@ -132,10 +125,36 @@ void amsEncoderResetPos(void) {
for(i = 0; i< NUM_ENC; i++) {
//amsEncoderBlockingRead(i); // get initial values w/o setting oticks
//encPos[i].offset = encPos[i].pos; // initialize encoder
encPos[i].calibPos = 0;
encPos[i].oticks = 0; // set revolution counter to 0
}

//Set up offset
//TODO: maybe move these to NVM somewhere in the flash, rather than project defines
#ifdef AMS_ENC_OFFSET_0
encPos[0].offset = AMS_ENC_OFFSET_0;
#else
encPos[0].offset = 0;
#endif

#ifdef AMS_ENC_OFFSET_1
encPos[1].offset = AMS_ENC_OFFSET_1;
#else
encPos[1].offset = 0;
#endif

#ifdef AMS_ENC_OFFSET_2
encPos[2].offset = AMS_ENC_OFFSET_2;
#else
encPos[2].offset = 0;
#endif

#ifdef AMS_ENC_OFFSET_3
encPos[3].offset = AMS_ENC_OFFSET_3;
#else
encPos[3].offset = 0;
#endif

}

/*****************************************************************************
Expand Down Expand Up @@ -196,7 +215,7 @@ unsigned char amsEncoderStartAsyncRead(void) {
}

void __attribute__((interrupt, no_auto_psv)) _MI2C1Interrupt(void) {
LED_3 = 1;
//LED_3 = 1;

switch(state) {
case AMS_ENC_WRITE_START:
Expand Down Expand Up @@ -253,7 +272,7 @@ void __attribute__((interrupt, no_auto_psv)) _MI2C1Interrupt(void) {
state = AMS_ENC_IDLE;
break;
}
LED_3 = 0;
//LED_3 = 0;
_MI2C1IF = 0;
}

Expand All @@ -276,3 +295,30 @@ float amsEncoderGetFloatPos(unsigned char num) {
pos = encPos[num].pos* LSB2ENCDEG; //calculate Float
return pos;
}

int amsEncoderGetPos(unsigned char num) {
if(num < NUM_ENC){
return encPos[num].pos;
}
else{
return 0;
}
}

long amsEncoderGetOticks(unsigned char num) {
if(num < NUM_ENC){
return encPos[num].oticks;
}
else{
return 0;
}
}

unsigned int amsEncoderGetOffset(unsigned char num){
if(num < NUM_ENC){
return encPos[num].offset;
}
else{
return 0;
}
}
31 changes: 27 additions & 4 deletions ams-enc.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@
typedef struct {
unsigned int pos; // raw reading from sensor 14 bits
long oticks; // revolution counter
unsigned int calibPos; // 0 to 2pi, converted to 16 bits
unsigned int offset; // initial reading on setup - relative zero position
} EncObj;

extern EncObj encPos[NUM_ENC];

/*****************************************************************************
* Function Name : amsHallSetup
* Description : initialize I2C, and for initial calibration, set offset
Expand Down Expand Up @@ -105,11 +104,35 @@ void amsEncoderBlockingRead(unsigned char num);
unsigned char amsEncoderStartAsyncRead(void);

/*****************************************************************************
* Function Name : encGetFloatPos
* Function Name : amsEncoderGetFloatPos
* Description : Return the angular position of encoder[num] return as float
* Parameters : None
* Return Value : None
* Return Value : float value for a single encoder, in degrees
*****************************************************************************/
float amsEncoderGetFloatPos(unsigned char num);

/*****************************************************************************
* Function Name : amsEncoderGetPos
* Description : Return the angular position of encoder[num] return as int
* Parameters : None
* Return Value : int value for a single encoder
*****************************************************************************/
int amsEncoderGetPos(unsigned char num);

/*****************************************************************************
* Function Name : amsEncoderGetOticks
* Description : Return the count of total revolutions
* Parameters : None
* Return Value : int value, for a single encoder
*****************************************************************************/
long amsEncoderGetOticks(unsigned char num);

/*****************************************************************************
* Function Name : amsEncoderGetOffset
* Description : Return the offset for an encoder
* Parameters : None
* Return Value : int value, for a single encoder
*****************************************************************************/
unsigned int amsEncoderGetOffset(unsigned char num);

#endif // __AMS_ENC_H
Loading

0 comments on commit b012786

Please sign in to comment.