Rleg  2
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
Functions | Variables
communication.c File Reference

Communication Routines. More...

#include <stdio.h>
#include "communication.h"
Include dependency graph for communication.c:

Go to the source code of this file.

Functions

int devices_init (IMU_PARAM_STRUCT *imu_param, SPI_PARAM_STRUCT *spi_param, MRA_DATA_STRUCT *mra_data)
 INITIALIZATION OF SENSORS AND DEVICES. More...
 
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. More...
 
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. More...
 
void actuate (int spi_dev, MRA_DATA_STRUCT *mra_data)
 Applies the control signal to the actuator. More...
 
void dac_shut_down (void)
 

Variables

int status
 

Detailed Description

Communication Routines.

Definition in file communication.c.

Function Documentation

void actuate ( int  spi_dev,
MRA_DATA_STRUCT mra_data 
)

Applies the control signal to the actuator.

Parameters
spi_devContaining SPI variable ID
mra_dataStruct containing the control signal
Returns
nothing

Definition at line 179 of file communication.c.

References dac_write(), FAILURE, GPIO_CS_S0, GPIO_CS_S1, GPIO_CS_S2, GPIO_CS_S3, GPIO_DAC_SHDN, gpio_write(), MRA_DATA_STRUCT::new_ctl, SUCCESS, and MRA_DATA_STRUCT::v_ctl.

Referenced by control_task(), devices_close(), devices_init(), and periodic_task_1().

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 }
int dac_write(int spi_dev, uint8_t ch, uint8_t _shdn, unsigned short int data)
WRITE DATA TO D/A CONVERTER MCP3922.
#define GPIO_CS_S3
Definition: communication.h:19
#define FAILURE
Definition: calibration.h:7
int gpio_write(uint8_t gpio, uint8_t value)
#define GPIO_CS_S0
Definition: communication.h:16
#define GPIO_CS_S2
Definition: communication.h:18
#define SUCCESS
Definition: calibration.h:6
#define GPIO_DAC_SHDN
Definition: communication.h:21
short int v_ctl
Voltage level for control output.
#define GPIO_CS_S1
Definition: communication.h:17

Here is the call graph for this function:

Here is the caller graph for this function:

void dac_shut_down ( void  )

Definition at line 195 of file communication.c.

References GPIO_DAC_SHDN, gpio_read(), and gpio_write().

196 {
197  while( (gpio_read(GPIO_DAC_SHDN))==0 )
199  return;
200 }
int gpio_write(uint8_t gpio, uint8_t value)
int gpio_read(uint8_t gpio)
Definition: gpio_functions.c:3
#define GPIO_DAC_SHDN
Definition: communication.h:21

Here is the call graph for this function:

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.

[long description]

Todo:
Complete and test this function
Parameters
imu_param[description]
spi_param[description]
mra_data[description]
Returns
[description]

Definition at line 82 of file communication.c.

References actuate(), IMU_PARAM_STRUCT::i2c_dev, SPI_PARAM_STRUCT::spi_dev, SUCCESS, and MRA_DATA_STRUCT::v_ctl.

Referenced by main().

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 }
void actuate(int spi_dev, MRA_DATA_STRUCT *mra_data)
Applies the control signal to the actuator.
#define SUCCESS
Definition: calibration.h:6
short int v_ctl
Voltage level for control output.

Here is the call graph for this function:

Here is the caller graph for this function:

int devices_init ( IMU_PARAM_STRUCT imu_param,
SPI_PARAM_STRUCT spi_param,
MRA_DATA_STRUCT mra_data 
)

INITIALIZATION OF SENSORS AND DEVICES.

Parameters
*imu_paramStructure with IMU parameters
*spi_paramStructure with SPI parameters
*mra_dataStructure to control MRA
Returns
flag with SUCCESS or FAILURE

Definition at line 12 of file communication.c.

References IMU_PARAM_STRUCT::acc, acc_init(), IMU_PARAM_STRUCT::param_gyr::act, actuate(), ADD_ADXL345, ADD_HMC5883, ADD_ITG3200, IMU_PARAM_STRUCT::param_gyr::clk_source, SPI_PARAM_STRUCT::cs, FAILURE, IMU_PARAM_STRUCT::param_acc::full_res, IMU_PARAM_STRUCT::gyr, gyr_init(), IMU_PARAM_STRUCT::i2c_dev, IMU_PARAM_STRUCT::param_gyr::lpf_bw, IMU_PARAM_STRUCT::mag, mag_init(), IMU_PARAM_STRUCT::param_mag::meas_mode, SPI_PARAM_STRUCT::mode, MRA_DATA_STRUCT::new_ctl, IMU_PARAM_STRUCT::param_mag::op_mode, IMU_PARAM_STRUCT::param_acc::range, IMU_PARAM_STRUCT::param_mag::range, IMU_PARAM_STRUCT::param_acc::rate, IMU_PARAM_STRUCT::param_gyr::rate, IMU_PARAM_STRUCT::param_mag::rate, IMU_PARAM_STRUCT::param_mag::samples_avg, SPI_PARAM_STRUCT::speed, SPI_PARAM_STRUCT::spi_dev, spi_init(), SUCCESS, and MRA_DATA_STRUCT::v_ctl.

Referenced by main(), and periodic_task_2().

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 }
struct IMU_PARAM_STRUCT::param_mag mag
void actuate(int spi_dev, MRA_DATA_STRUCT *mra_data)
Applies the control signal to the actuator.
#define ADD_ITG3200
Definition: imu_functions.h:37
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
#define ADD_ADXL345
Definition: imu_functions.h:36
struct IMU_PARAM_STRUCT::param_gyr gyr
struct IMU_PARAM_STRUCT::param_acc acc
#define SUCCESS
Definition: calibration.h:6
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
int gyr_init(int i2c_dev, float rate, short int lpf_bw, char clk_source, char *act)
INITIALIZE GYROMETER.
Definition: gyr_functions.c:53
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 ADD_HMC5883
Definition: imu_functions.h:38

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Parameters
i2c_dev
spi_dev
*imu_data
*eff_data
*mra_data
Returns
flag with SUCCESS or FAILURE

Definition at line 94 of file communication.c.

References IMU_DATA_STRUCT::acc, acc_read_all_data(), adc_read(), ADD_ADXL345, ADD_HMC5883, ADD_ITG3200, ENC_DATA_STRUCT::calib, enc_read_pos(), EFF_DATA_STRUCT::F, FAILURE, GPIO_CS_S0, GPIO_CS_S1, GPIO_CS_S2, GPIO_CS_S3, gpio_write(), IMU_DATA_STRUCT::gyr, gyr_read_all_data(), IMU_DATA_STRUCT::mag, mag_read_all_data(), ENC_DATA_STRUCT::new_data, IMU_DATA_STRUCT::new_data, EFF_DATA_STRUCT::new_data, MRA_DATA_STRUCT::new_data, ENC_DATA_STRUCT::position, ENC_DATA_STRUCT::calibrate::position, ENC_DATA_STRUCT::spi_dev, SUCCESS, IMU_DATA_STRUCT::temp, MRA_DATA_STRUCT::v_ctl_read, DATA_XYZ::x, DATA_XYZ::y, and DATA_XYZ::z.

Referenced by control_task(), periodic_task_1(), and ui_task().

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 }
#define GPIO_CS_S3
Definition: communication.h:19
#define ADD_ITG3200
Definition: imu_functions.h:37
short int y
Definition: communication.h:77
#define FAILURE
Definition: calibration.h:7
#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.
DATA_XYZ mag
Magnetormeter Vector.
Definition: communication.h:96
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)
#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
short int z
Definition: communication.h:78
unsigned short int position
unsigned short int position
int enc_read_pos(int spi_dev, unsigned short int *data)
READ POSITION.
DATA_XYZ gyr
Gyrometer Vector.
Definition: communication.h:95
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)
#define GPIO_CS_S1
Definition: communication.h:17
#define ADD_HMC5883
Definition: imu_functions.h:38

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

int status