Skip to content

Commit a7c5dfe

Browse files
committed
Trying to make pid-ip2.5 module more general.
1 parent c061850 commit a7c5dfe

File tree

2 files changed

+80
-7
lines changed

2 files changed

+80
-7
lines changed

ams-enc.c

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
#include "i2c.h"
5050
#include "ams-enc.h"
5151
#include "utils.h"
52-
52+
#include "settings.h"
5353

5454
#define LSB2ENCDEG 0.0219
5555
#define ENC_I2C_CHAN 1 //Encoder is on I2C channel 1
@@ -73,8 +73,7 @@ EncObj encPos[NUM_ENC];
7373

7474
#define AMS_ENC_ANGLE_REG 0xFE
7575

76-
#define AMS_ENC_OFFSET_0 5758
77-
#define AMS_ENC_OFFSET_1 7706
76+
7877

7978
volatile unsigned char state = AMS_ENC_IDLE;
8079
volatile unsigned char encoder_number = 0;
@@ -129,8 +128,33 @@ void amsEncoderResetPos(void) {
129128
encPos[i].calibPos = 0;
130129
encPos[i].oticks = 0; // set revolution counter to 0
131130
}
131+
132+
//Set up offset
133+
//TODO: maybe move these to NVM somewhere in the flash, rather than project defines
134+
#ifdef AMS_ENC_OFFSET_0
132135
encPos[0].offset = AMS_ENC_OFFSET_0;
136+
#else
137+
encPos[0].offset = 0;
138+
#endif
139+
140+
#ifdef AMS_ENC_OFFSET_1
133141
encPos[1].offset = AMS_ENC_OFFSET_1;
142+
#else
143+
encPos[1].offset = 0;
144+
#endif
145+
146+
#ifdef AMS_ENC_OFFSET_2
147+
encPos[2].offset = AMS_ENC_OFFSET_2;
148+
#else
149+
encPos[2].offset = 0;
150+
#endif
151+
152+
#ifdef AMS_ENC_OFFSET_3
153+
encPos[3].offset = AMS_ENC_OFFSET_3;
154+
#else
155+
encPos[3].offset = 0;
156+
#endif
157+
134158
}
135159

136160
/*****************************************************************************
@@ -271,3 +295,30 @@ float amsEncoderGetFloatPos(unsigned char num) {
271295
pos = encPos[num].pos* LSB2ENCDEG; //calculate Float
272296
return pos;
273297
}
298+
299+
int amsEncoderGetPos(unsigned char num) {
300+
if(num < NUM_ENC){
301+
return encPos[num].pos;
302+
}
303+
else{
304+
return 0;
305+
}
306+
}
307+
308+
long amsEncoderGetOticks(unsigned char num) {
309+
if(num < NUM_ENC){
310+
return encPos[num].oticks;
311+
}
312+
else{
313+
return 0;
314+
}
315+
}
316+
317+
unsigned int amsEncoderGetOffset(unsigned char num){
318+
if(num < NUM_ENC){
319+
return encPos[num].offset;
320+
}
321+
else{
322+
return 0;
323+
}
324+
}

ams-enc.h

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ typedef struct {
5858
unsigned int offset; // initial reading on setup - relative zero position
5959
} EncObj;
6060

61-
extern EncObj encPos[NUM_ENC];
62-
6361
/*****************************************************************************
6462
* Function Name : amsHallSetup
6563
* Description : initialize I2C, and for initial calibration, set offset
@@ -106,11 +104,35 @@ void amsEncoderBlockingRead(unsigned char num);
106104
unsigned char amsEncoderStartAsyncRead(void);
107105

108106
/*****************************************************************************
109-
* Function Name : encGetFloatPos
107+
* Function Name : amsEncoderGetFloatPos
110108
* Description : Return the angular position of encoder[num] return as float
111109
* Parameters : None
112-
* Return Value : None
110+
* Return Value : float value for a single encoder, in degrees
113111
*****************************************************************************/
114112
float amsEncoderGetFloatPos(unsigned char num);
115113

114+
/*****************************************************************************
115+
* Function Name : amsEncoderGetPos
116+
* Description : Return the angular position of encoder[num] return as int
117+
* Parameters : None
118+
* Return Value : int value for a single encoder
119+
*****************************************************************************/
120+
int amsEncoderGetPos(unsigned char num);
121+
122+
/*****************************************************************************
123+
* Function Name : amsEncoderGetOticks
124+
* Description : Return the count of total revolutions
125+
* Parameters : None
126+
* Return Value : int value, for a single encoder
127+
*****************************************************************************/
128+
long amsEncoderGetOticks(unsigned char num);
129+
130+
/*****************************************************************************
131+
* Function Name : amsEncoderGetOffset
132+
* Description : Return the offset for an encoder
133+
* Parameters : None
134+
* Return Value : int value, for a single encoder
135+
*****************************************************************************/
136+
unsigned int amsEncoderGetOffset(unsigned char num);
137+
116138
#endif // __AMS_ENC_H

0 commit comments

Comments
 (0)