Rleg  2
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
encoder_functions.c
Go to the documentation of this file.
1 
11 #include "encoder_functions.h"
12 #include "spi_functions.h"
13 //#include <time.h>
14 
15 
16 /*Here, the all the functions created at enconder_funtions.h are declared*/
17 
18 
19 int enc_zero_set(int spi_dev){
20 
21  uint8_t send = ENCODER_SET_ZERO_PT;
22  uint8_t receive = 0;
23 
24  /* Send command */
25  spi_trans_bytes(spi_dev,&send,&receive,1);
27  return FAILURE;
28  }
29 
30  /* Note:
31  * From datasheet, the encoder must be power-cycled after this command
32  * for the new zero position to be used in the position calculation. */
33 
34  return SUCCESS;
35 }
36 
37 int enc_read_pos(int spi_dev,unsigned short int *data){
38 
39  uint8_t send[] = {ENCODER_RD_POS,ENCODER_NO_OP};
40  uint8_t receive[ARRAY_SIZE(send)] = {0, };
41  uint8_t buffer[ARRAY_SIZE(send)] = {0, } ;
42 
43  /* Send command */
44  spi_trans_bytes(spi_dev,send,receive,1);
45  if (enc_wait_for_ack(spi_dev, ENCODER_RD_POS, INT_MAX) != SUCCESS) {
46  return FAILURE;
47  }
48 
49  send[0]=ENCODER_NO_OP;
50 
51  if(spi_trans_bytes(spi_dev,send,receive,2) != SUCCESS) {
52  return FAILURE;
53  }
54 
55  buffer[0] = receive[0];
56 
60  if(spi_trans_bytes(spi_dev,send,receive,2) != SUCCESS) {
61  return FAILURE;
62  }
63 
64  buffer[1] = receive[0];
65 
66  *data = buffer[0]*256+buffer[1];
67 
68  //printf("\nreceive = 0x %2x %2x %d\n",buffer[0],buffer[1],data);
69 
70  return SUCCESS;
71 }
72 
73 int enc_wait_for_ack(int spi_dev, uint8_t ack, int max_errors)
74 {
75  int errors = 0;
76  uint8_t send = ENCODER_NO_OP;
77  uint8_t receive = 1;
78 
79  //nanosleep(10);
80  //spi_trans_bytes(spi_dev,send,receive,1);
81  while (receive != ack) {
82  spi_trans_bytes(spi_dev,&send,&receive,1);
83  //printf("\nReceive = 0x%x \n",receive);
84  if (receive != ENCODER_WAIT_RESP) {
85  errors++;
86  if (errors > max_errors) { return FAILURE; }
87  }
88  }
89 
90 
91  return SUCCESS;
92 }
#define ENCODER_EEPROM_WR
int enc_wait_for_ack(int spi_dev, uint8_t ack, int max_errors)
send command and delay between reads
#define INT_MAX
int spi_trans_bytes(int spi_dev, uint8_t *send, uint8_t *receive, int n)
TRANSFER N BYTES.
#define ARRAY_SIZE(a)
Definition: spi_functions.h:29
#define FAILURE
Definition: calibration.h:7
int enc_zero_set(int spi_dev)
SET ZERO POINT.
#define ENCODER_NO_OP
#define ENCODER_WAIT_RESP
#define ENCODER_RD_POS
#define SUCCESS
Definition: calibration.h:6
Rev 0 - 18/06/2013 RLEG project - 2013.
int enc_read_pos(int spi_dev, unsigned short int *data)
READ POSITION.
#define ENCODER_SET_ZERO_PT