Other Parts Discussed in Thread: TM4C123BH6PM
Hi, I'm using an accelerometer(H3LIS331DL) to calculate position of an RC car with the Tiva MCU, TM4C123BH6PM.
I'm using SPI interface with the sensor.(2MHz SPI Clock) So far, reading the data is successful.
I'm using a average filter(50 samples) to reduce the errors, but the sensor shows some weird values...
Anyway, I'm trying to calculate distance with this sensor. I want to check whether my program flow is correct or not.
(I'm using CCS5 with XDS100V3)
#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_gpio.h"
#include "driverlib/systick.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/ssi.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/pwm.h"
#include "utils/uartstdio.h"
#include "My_headers/ACC_REG.h"//Registers about the Accelerometer
volatile uint32_t del_t=0;
volatile float dt;
volatile float ax_prev=0,ay_prev=0;
volatile float ax=0,ay=0;
volatile float vx=0,vy=0;
volatile float vx_prev=0, vy_prev=0;
volatile float x=0 , y=0;
volatile float x_prev=0 , y_prev=0;
int main(void){
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
FPUEnable();
FPULazyStackingEnable();
InitConsole();
// I initialized UART0, PB6 to PWM0, and SSI1(Initialize the accelerometer)
//Accelerometer-> Power : Normal mode, Output Data rate : 1000Hz, Low-pass cutoff freq : 780Hz,
//Enable X, Y, Z axis, High-pass cutoff freq : 2.5Hz, High-pass filter is reference mode
//data from internal filter sent to output register, Block data update enable, measurement range(scale) : +/- 100g\
//Sensitivity : 49mg/digit (12-bit representation)
//Checked the UART0 using putty and worked fine
//Checked PWM0 using oscilloscope , 966uHz pulse with 50% duty
//Checked SSI1 using oscilloscope. The data of the sensor came out correctly(like WHO_AM_I value)
////////////////////////////////////////////////////TimerA0 setting
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC_UP);
// Full-width periodic up-count timer
TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet());
TimerEnable(TIMER0_BASE, TIMER_A);
////////////////////////////////////////////////////
while(1){
del_t = TimerValueGet(TIMER0_BASE,TIMER_A);
get_accel(); // this function writes the value to variable ax, ay
del_t = TimerValueGet(TIMER0_BASE,TIMER_A) - del_t; //calculate how much time(counts) has passed
dt = (float)( del_t / SysCtlClockGet() );
vx = vx_prev + dt*(ax+ax_prev)/2 ; //numerical integration, trapezoid method
x = x_prev + dt*(vx+vx_prev)/2 ;
/////////////////////////////////////////// numerical integration
ax_prev = ax;
vx_prev = vx;
x_prev = x; //update previous method
}
//////////////////////End of the code
I get weird outputs... Is the flow incorrect? I think there's a problem about calculating dt.
Please, point out my horrible code T^T.
Always appreciate your help!
Regards, Min-Ku :D