Rleg  2
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
communication.c
Go to the documentation of this file.
1 
7 #include <stdio.h>
8 #include "communication.h"
9 
10 int status;
11 
13 {
14 #if USE_IMU
15 
16  if( (imu_param->i2c_dev = open("/dev/i2c-3", O_RDWR))<0 )
17  {
18  perror("Failed to open i2c_dev");
19  return FAILURE;
20  }
21 
22  if ((ioctl(imu_param->i2c_dev, I2C_SLAVE, ADD_HMC5883)) < 0) {
23  perror("ioctl(I2C_SLAVE) mag");
24  return FAILURE;
25  }
26 
27  if( (mag_init(imu_param->i2c_dev, imu_param->mag.rate, imu_param->mag.range, imu_param->mag.samples_avg, imu_param->mag.meas_mode, imu_param->mag.op_mode))< 0)
28  {
29  perror("Error in magnetometer initialization");
30  return FAILURE;
31  }
32 
33  if( ioctl(imu_param->i2c_dev, I2C_SLAVE, ADD_ITG3200) < 0) {
34  perror("ioctl(I2C_SLAVE) gyr");
35  return FAILURE;
36  }
37 
38  if( gyr_init(imu_param->i2c_dev, imu_param->gyr.rate, imu_param->gyr.lpf_bw, imu_param->gyr.clk_source, imu_param->gyr.act)<0 )
39  {
40  perror("Error in gyrometer initialization");
41  //return FAILURE;
42  }
43 
44  if ( (ioctl(imu_param->i2c_dev, I2C_SLAVE, ADD_ADXL345)) < 0) {
45  perror("ioctl(I2C_SLAVE) acc");
46  return FAILURE;
47  }
48 
49  if( acc_init(imu_param->i2c_dev, imu_param->acc.full_res, imu_param->acc.rate, imu_param->acc.range)<0)
50  {
51  perror("Error in accelerometer initialization");
52  return FAILURE;
53  }
54 
55 #endif
56 
57  if ((spi_param->spi_dev=spi_init(spi_param->mode,spi_param->speed,spi_param->cs))<0)
58  {
59  perror("Error in SPI device initialization");
60  return FAILURE;
61  }
62 
63  //Enable DAC and writing 0
64  mra_data->v_ctl=0;
65 // mra_data->Out_1=0;gpio_write(GPIO_CS_S3,1)==FAILURE
66 
67  actuate(spi_param->spi_dev, mra_data);
68  if( mra_data->new_ctl == FAILURE )
69  {
70  perror("Error in MRA initialization");
71  return FAILURE;
72  }
73  // if( gpio_write(GPIO_DAC_SHDN,1)==FAILURE )
74  // {
75 // perror("Error in SHDN initialization");
76 // return FAILURE;
77 // }
78 
79  return SUCCESS;
80 }
81 
83 {
84  //Writing 0 and Disable DAC
85  mra_data->v_ctl=0;
86  actuate(spi_param->spi_dev, mra_data);
87 
88  close(imu_param->i2c_dev);
89  close(spi_param->spi_dev);
90 
91  return SUCCESS;
92 }
93 
95 {
96  short int data[4];
97  int f=0;
98  // Read IMU data
99  imu_data->new_data=SUCCESS;
100 #if USE_IMU
101  if ( (ioctl(i2c_dev, I2C_SLAVE, ADD_ADXL345))<0)
102  imu_data->new_data=0;
103  else if( (acc_read_all_data(i2c_dev,data))==FAILURE )
104  {
105  imu_data->new_data=0;
106  //printf("\nimu_data->new_data = %d\n",imu_data->new_data);
107  //f=1;
108  }
109  else{
110  imu_data->acc.x=data[0];
111  imu_data->acc.y=data[1];
112  imu_data->acc.z=data[2];
113  }
114 
115  if ( (ioctl(i2c_dev, I2C_SLAVE, ADD_ITG3200)) < 0)
116  imu_data->new_data=0;
117  else if( (gyr_read_all_data(i2c_dev,data))==FAILURE )
118  imu_data->new_data=0;
119  else{
120  imu_data->gyr.x=data[0];
121  imu_data->gyr.y=data[1];
122  imu_data->gyr.z=data[2];
123  imu_data->temp=data[3];
124  }
125 
126  if ( (ioctl(i2c_dev, I2C_SLAVE, ADD_HMC5883)) < 0)
127  imu_data->new_data=0;
128  else if( (mag_read_all_data(i2c_dev,data))==FAILURE )
129  imu_data->new_data=0;
130  else{
131  imu_data->mag.x=data[0];
132  imu_data->mag.y=data[1];
133  imu_data->mag.z=data[2];
134  }
135 #endif
136  //Read Efforts data
137  eff_data->new_data=SUCCESS;
138 
140  eff_data->new_data=FAILURE;
141  else{
142  if( (adc_read(spi_dev,0,1,data))==FAILURE )
143  eff_data->new_data=FAILURE;
144  else eff_data->F.x=data[0];
145  //Read MR actuator voltage control
146  if( (adc_read(spi_dev,7,1,data))==SUCCESS )
147  {
148  mra_data->v_ctl_read=data[0];
149  mra_data->new_data=SUCCESS;
150  }
151  }
152  //if( f==1 )
153  //printf("imu_data->new_data antes do return= %d\n",imu_data->new_data);
154 
155  //Read Encoder data
156  enc_data->new_data = SUCCESS;
157 #if USE_ENCODER
158  enc_data->spi_dev = spi_dev;
160  enc_data->new_data=FAILURE;
161  if(enc_read_pos(enc_data->spi_dev,&(enc_data->position))==FAILURE)
162  enc_data->new_data = FAILURE;
164  enc_data->calib.position = ((4096 - enc_data->position)/11.37778);
165 #endif
166 
167  if( imu_data->new_data==FAILURE || eff_data->new_data==FAILURE || mra_data->new_data==FAILURE || enc_data->new_data == FAILURE)
168  return FAILURE;
169 
170  return SUCCESS;
171 }
172 
173 /*Function: actuate
174  * Summary: Applies the control signal to the actuator
175  * Parameters: spi_dev containing SPI variable ID
176  * mra_data struct containing the control signal
177  * Return: nothing
178  */
179 void actuate(int spi_dev, MRA_DATA_STRUCT *mra_data)
180 {
181  //printf("i = %d\n",i);
182  //i++;
184  mra_data->new_ctl=FAILURE; else
186  mra_data->new_ctl=FAILURE;
187  else if( (dac_write(spi_dev,0,1,mra_data->v_ctl))==FAILURE )
188  mra_data->new_ctl=FAILURE;
189 // else if( (dac_write(spi_dev,1,1,mra_data->???))==FAILURE )
190 // mra_data->success=FAILURE;
191  else mra_data->new_ctl=SUCCESS;
192  return;
193 }
194 
195 void dac_shut_down(void)
196 {
197  while( (gpio_read(GPIO_DAC_SHDN))==0 )
199  return;
200 }
MRA_DATA_STRUCT mra_data
Definition: main.c:32
Configs for SPI.
Definition: communication.h:65
int dac_write(int spi_dev, uint8_t ch, uint8_t _shdn, unsigned short int data)
WRITE DATA TO D/A CONVERTER MCP3922.
struct IMU_PARAM_STRUCT::param_mag mag
void actuate(int spi_dev, MRA_DATA_STRUCT *mra_data)
Applies the control signal to the actuator.
ENC_DATA_STRUCT enc_data
Definition: main.c:31
#define GPIO_CS_S3
Definition: communication.h:19
Struct to control MRA.
Data of IMU structure.
Definition: communication.h:93
#define ADD_ITG3200
Definition: imu_functions.h:37
short int y
Definition: communication.h:77
SPI_PARAM_STRUCT spi_param
Definition: main.c:26
int spi_init(uint8_t mode, uint32_t speed, uint8_t cs)
spi Functions to deal communication by SPI protocol
#define FAILURE
Definition: calibration.h:7
IMU_PARAM_STRUCT imu_param
Definition: main.c:25
#define ADD_ADXL345
Definition: imu_functions.h:36
int adc_read(int spi_dev, uint8_t ch, uint8_t sgl, short int *data)
READ DATA FROM A/D CONVERTER MCP3208.
IMU_DATA_STRUCT imu_data
Definition: main.c:30
DATA_XYZ mag
Magnetormeter Vector.
Definition: communication.h:96
struct IMU_PARAM_STRUCT::param_gyr gyr
struct IMU_PARAM_STRUCT::param_acc acc
void dac_shut_down(void)
int gpio_write(uint8_t gpio, uint8_t value)
int acc_read_all_data(int i2c_dev, short int *data)
READ ALL DATA AT ONCE (X, Y and Z)
int status
Definition: communication.c:10
int gpio_read(uint8_t gpio)
Definition: gpio_functions.c:3
#define GPIO_CS_S0
Definition: communication.h:16
short int v_ctl_read
Voltage level read from the actuator.
#define GPIO_CS_S2
Definition: communication.h:18
DATA_XYZ acc
Accel Vector.
Definition: communication.h:94
#define SUCCESS
Definition: calibration.h:6
struct ENC_DATA_STRUCT::calibrate calib
int devices_init(IMU_PARAM_STRUCT *imu_param, SPI_PARAM_STRUCT *spi_param, MRA_DATA_STRUCT *mra_data)
INITIALIZATION OF SENSORS AND DEVICES.
Definition: communication.c:12
short int z
Definition: communication.h:78
unsigned short int position
#define GPIO_DAC_SHDN
Definition: communication.h:21
int read_all_data(int i2c_dev, int spi_dev, IMU_DATA_STRUCT *imu_data, EFF_DATA_STRUCT *eff_data, MRA_DATA_STRUCT *mra_data, ENC_DATA_STRUCT *enc_data)
READ ALL DATA FROM SENSORS AND ADC.
Definition: communication.c:94
unsigned short int position
int enc_read_pos(int spi_dev, unsigned short int *data)
READ POSITION.
int mag_init(int i2c_dev, uint8_t rate, uint8_t range, uint8_t samples_avg, uint8_t meas_mode, uint8_t op_mode)
INITIALIZE MAGNETOMETER.
Definition: mag_functions.c:53
Configs of IMU.
Definition: communication.h:31
int gyr_init(int i2c_dev, float rate, short int lpf_bw, char clk_source, char *act)
INITIALIZE GYROMETER.
Definition: gyr_functions.c:53
int devices_close(IMU_PARAM_STRUCT *imu_param, SPI_PARAM_STRUCT *spi_param, MRA_DATA_STRUCT *mra_data)
Fuction to close all communication with the sensors and devices.
Definition: communication.c:82
DATA_XYZ gyr
Gyrometer Vector.
Definition: communication.h:95
EFF_DATA_STRUCT eff_data
Definition: main.c:29
int gyr_read_all_data(int i2c_dev, short int *data)
READ ALL DATA AT ONCE (X, Y, Z and T)
short int x
Definition: communication.h:76
int mag_read_all_data(int i2c_dev, short int *data)
READ ALL DATA AT ONCE (X, Y and Z)
short int v_ctl
Voltage level for control output.
int acc_init(int i2c_dev, uint8_t full_res, uint16_t rate, uint8_t range)
Rev 0 - 11/11/2012 RLEG project - 2012.
Definition: acc_functions.c:11
#define GPIO_CS_S1
Definition: communication.h:17
#define ADD_HMC5883
Definition: imu_functions.h:38