kureigu
Member level 2
![Member level 2](/styles/images/ranks/dlevel2.gif)
Hey all, I have a problem and I'm not sure of the best way to approach it, so I was hoping someone might be able to point me in the right direction.
I have an angular rate sensor that's going to be used to determine the change in angle on the horizontal plane. I have it all set up to and ADC and I'm able to read it via SPI using an SPI master component I developed. Now the problem is, the rate sensor outputs a rate of change in angle rather than an actual change in angle. So I figure the best way to get the actual angle out, is by integration (i.e. time averaging over a set number of samples).
This is where I begin to struggle a little since I'm not sure of the best way to go about implementing this in VHDL would be. I've done it before with embedded C, using a circular buffer and continual subtractions and additions to get a moving average, but I can't figure out how I'd do that in VHDL since the whole approach is different.
TL;DR; When a value from the rate sensor is read in, I want to integrate over n samples to determine the angular change, how do?
Edit: I attempted to write a component to do the averaging, but I don't think I've done it the best way. Ideally I want to use a generic to set up the number of samples to average over, but I'm unsure of how to make an array of 12 bit signals.
I have an angular rate sensor that's going to be used to determine the change in angle on the horizontal plane. I have it all set up to and ADC and I'm able to read it via SPI using an SPI master component I developed. Now the problem is, the rate sensor outputs a rate of change in angle rather than an actual change in angle. So I figure the best way to get the actual angle out, is by integration (i.e. time averaging over a set number of samples).
This is where I begin to struggle a little since I'm not sure of the best way to go about implementing this in VHDL would be. I've done it before with embedded C, using a circular buffer and continual subtractions and additions to get a moving average, but I can't figure out how I'd do that in VHDL since the whole approach is different.
TL;DR; When a value from the rate sensor is read in, I want to integrate over n samples to determine the angular change, how do?
Edit: I attempted to write a component to do the averaging, but I don't think I've done it the best way. Ideally I want to use a generic to set up the number of samples to average over, but I'm unsure of how to make an array of 12 bit signals.
Code:
library ieee;
use std_logic_1164.all;
use std_logic_arith.all;
use std_logic_unsigned.all;
entity time_avg is
generic( rate: integer := 64
);
port( sample: in unsigned(12 downto 0);
avg: out unsigned(12 downto 0)
);
end time_avg;
architecture integrate of time_avg is
signal sum: unsigned (13 downto 0);
signal running_avg: unsigned(12 downto 0) := "000000000000";
signal count: unsigned(1 downto 0) := 0;
signal s0, s1, s2, s3 := unsigned(12 downto 0) := "000000000000";
begin
process(sample)
case count is
when "00" =>
s0 <= sample;
when "01" =>
s1 <= sample;
when "10" =>
s2 <= sample;
when "11" =>
s3 <= sample;
end case;
sum <= s0 + s1 + s2 + s3;
running_avg <= sum(13 downto 2);
count <= count + 1;
end process;
end integrate;
Last edited: