Sending unique measured current value was replaced by current accumulator. Now multip...
authorMartin Prudek <prudemar@fel.cvut.cz>
Thu, 16 Apr 2015 09:59:39 +0000 (11:59 +0200)
committerMartin Prudek <prudemar@fel.cvut.cz>
Thu, 16 Apr 2015 09:59:39 +0000 (11:59 +0200)
pmsm-control/adc_reader.vhdl
pmsm-control/rpi_pmsm_control.vhdl
pmsm-control/test_sw/main_pmsm.c
pmsm-control/test_sw/rp_spi.c
pmsm-control/test_sw/rp_spi.h

index c792525..14b2faa 100644 (file)
@@ -10,10 +10,11 @@ port (
        clk: in std_logic;                                      --input clk
        adc_reset: in std_logic;
        adc_miso: in std_logic;                                 --spi master in slave out
-       adc_channels: out std_logic_vector (35 downto 0);       --consistent data of 3 channels
+       adc_channels: out std_logic_vector (71 downto 0);       --consistent data of 3 channels
        adc_sclk: out std_logic;                                --spi clk
        adc_scs: out std_logic;                                 --spi slave select
-       adc_mosi: out std_logic                                 --spi master out slave in
+       adc_mosi: out std_logic;                                --spi master out slave in
+       measur_count: out std_logic_vector(8 downto 0)          --number of accumulated measurments
        
 );
 end adc_reader;
@@ -30,6 +31,9 @@ architecture behavioral of adc_reader is
        signal adc_data: std_logic_vector(11 downto 0); 
        signal adc_rst_old : std_logic_vector(1 downto 0);
        signal adc_address: std_logic_vector(2 downto 0);
+       signal cumul_data: std_logic_vector(71 downto 0);       --unconsistent data, containing different amounts of measurments
+       signal prepared_data: std_logic_vector(71 downto 0);    --consistent data, waiting for clk sync to propagate to output
+       signal m_count_sig: std_logic_vector(8 downto 0);       --measurments count waiting for clk to propagate to output
 begin
        
        
@@ -56,6 +60,12 @@ begin
                                adc_sclk<='0';                  --lower clock
                                data_ready:='0';                --mark data as unprepared
                                channel:=ch0;                   --prepare channel0
+                               adc_data<=(others=>'0');        --null working data
+                               cumul_data<=(others=>'0');      --null working data
+                               prepared_data<=(others=>'0');   --null the output
+                               adc_channels<=(others=>'0');    --null the output
+                               measur_count<=(others=>'0');    --null the count
+                               m_count_sig<=(others=>'0');     --null the count
                                adc_address<="001";             --set its address
                                reset_count:="0000";
                                state<=rst_wait;
@@ -122,29 +132,38 @@ begin
                        when r7=> --7th rising edge, data ready
                                adc_sclk<='1';
                                if (data_ready='1') then
+                                       --add the current current to sum and shift the register
+                                       cumul_data(71 downto 0)<=
+                                               std_logic_vector(unsigned(cumul_data(47 downto 24))
+                                                       +unsigned(adc_data(11 downto 0)))
+                                               & cumul_data(23 downto 0)
+                                               & cumul_data(71 downto 48);
+                               end if;
+                               state<=f8;
+                       when f8=> --8th falling edge
+                               adc_sclk<='0';
+                               adc_mosi<='0'; --PD0
+                               if (data_ready='1') then
                                        case channel is
                                                when ch0=>
-                                                       adc_channels(35 downto 24)<=adc_data(11 downto 0);
                                                        adc_address<="101";     --ch1 address
-                                                       channel:=ch1;
+                                                       channel:=ch1;           --next channel code
                                                when ch1=>
-                                                       adc_channels(23 downto 12)<=adc_data(11 downto 0);
                                                        adc_address<="010";     --ch2 address
-                                                       channel:=ch2;
+                                                       channel:=ch2;           --next channel code
                                                when ch2=>
-                                                       adc_channels(11 downto 0)<=adc_data(11 downto 0);
+                                                       prepared_data(71 downto 0)<=cumul_data(71 downto 0);
+                                                       m_count_sig<=std_logic_vector(unsigned(m_count_sig)+1);
                                                        adc_address<="001";     --ch0 address
-                                                       channel:=ch0;
+                                                       channel:=ch0;           --next channel code
                                        end case;
                                end if;
                                data_ready:='1';
-                               state<=f8;      
-                       when f8=> --8th falling edge
-                               adc_sclk<='0';
-                               adc_mosi<='0'; --PD0
                                state<=r8;
-                       when r8=> --8th rising edge (adc gets PD0)
+                       when r8=> --8th rising edge (adc gets PD0), we propagate our results to output
                                adc_sclk<='1';
+                               adc_channels <= prepared_data;          --data
+                               measur_count <= m_count_sig;            --count of measurments
                                state<=f9;
                        when f9=> --9th falling edge busy state between conversion (we write nothing)
                                adc_sclk<='0';
@@ -155,7 +174,7 @@ begin
                        when f10=> --10th falling edge
                                adc_sclk<='0';
                                state<=r10;
-                       when r10=>  --10th rising edge (we read 1. bit of conversion)
+                       when r10=>  --10th rising edge (we read 1. bit of new conversion)
                                adc_sclk<='1';
                                adc_data(11)<=adc_miso;
                                state<=f11;
index 104d272..47fd12c 100644 (file)
@@ -149,14 +149,16 @@ architecture behavioral of rpi_mc_simple_dc is
                adc_channels: out std_logic_vector (35 downto 0);       --consistent data of 3 channels
                adc_sclk: out std_logic;                                --spi clk
                adc_scs: out std_logic;                                 --spi slave select
-               adc_mosi: out std_logic                                 --spi master out slave in
+               adc_mosi: out std_logic;                                --spi master out slave in
+               measur_count: out std_logic_vector(8 downto 0)          --number of accumulated measurments
        
        );
        end component;
        
        
        signal adc_reset : std_logic;
-       signal adc_channels: std_logic_vector(35 downto 0);
+       signal adc_channels: std_logic_vector(71 downto 0);
+       signal adc_m_count: std_logic_vector(8 downto 0);
        
        signal spiclk_old: std_logic_vector(1 downto 0); --pro detekci hrany SPI hodin
        --signal pwm_in, pwm_dir_in: std_logic;
@@ -176,6 +178,7 @@ architecture behavioral of rpi_mc_simple_dc is
        signal pwm_sync: std_logic;
        signal pwm_en_p: std_logic_vector(1 to 3);
        signal pwm_en_n: std_logic_vector(1 to 3);
+       signal pwm_sig: std_logic_vector(1 to 3);
        
        signal income_data_valid: std_logic;
        
@@ -232,7 +235,7 @@ begin
                        match => pwm_match(i),
                        count => pwm_count,
                        -- outputs
-                       out_p => open,--pwm(i),                         --positive signal
+                       out_p => pwm_sig(i),                            --positive signal
                        out_n => shdn(i)                                --reverse signal is in shutdown mode
                );
        end generate;
@@ -253,7 +256,8 @@ begin
                adc_channels => adc_channels,
                adc_sclk => adc_sclk,
                adc_scs => adc_scs,
-               adc_mosi => adc_mosi
+               adc_mosi => adc_mosi,
+               measur_count => adc_m_count
                
        );
 
@@ -291,13 +295,9 @@ begin
        rs485_dir <= '0';
 
 
-       --shdn(1) <= '0';
-       --shdn(2) <= '1';
-       --shdn(3) <= '0';
-
-       --pwm(1) <= '0';
-       --pwm(2) <= '0';
-       --pwm(3) <= '0';
+       pwm(1) <= pwm_sig(1) and dip_sw(1);
+       pwm(2) <= pwm_sig(2) and dip_sw(1);
+       pwm(3) <= pwm_sig(3) and dip_sw(1);
        
                
        
@@ -348,14 +348,8 @@ begin
                        dat_reg(92 downto 90) <= pwm_en_p(1 to 3); --enable positive
                        dat_reg(89 downto 87) <= pwm_en_n(1 to 3); --shutdown
                        dat_reg(86 downto 81) <= (others=>'0');--pwm_match(1)(10 downto 5); --6 MSb of PWM1
-                       dat_reg(80 downto 74) <= (others=>'0');--pwm_match(2)(10 downto 4); --7 MSb of PWM2
-                       dat_reg(73 downto 68) <= (others=>'0');--pwm_match(3)(10 downto 5); --6 MSb of PWM3
-                       dat_reg(71 downto 60)<=(others=>'0');
-                       dat_reg(59 downto 48) <= adc_channels(35 downto 24); --current mesurments
-                       dat_reg(47 downto 36)<=(others=>'0');
-                       dat_reg(35 downto 24) <= adc_channels(23 downto 12); --current mesurments
-                       dat_reg(23 downto 12)<=(others=>'0');
-                       dat_reg(11 downto 0) <= adc_channels(11 downto 0); --current mesurments
+                       dat_reg(80 downto 72) <=adc_m_count(8 downto 0);        --count of measurments
+                       dat_reg(71 downto 0) <= adc_channels(71 downto 0);      --current mesurments
                        adc_reset<='0'; --remove reset flag, and wait on its rising edge
                elsif (ce0_old = "01") then --rising edge of SS, we should read the data
                        adc_reset<='1';
index ec33728..45f98bc 100644 (file)
@@ -73,30 +73,37 @@ void substractOffset(struct rpi_in* data, struct rpi_in* offset){
 /*
  * pocita procentualni odchylku od prumerneho proudu
  */
-float diff_p(int16_t value){
+float diff_p(float value){
        return ((float)value-PRUM_PROUD)*100/PRUM_PROUD;
 }
 /*
  * pocita procentualni odchylku od prumerneho souctu proudu
  */
-float diff_s(int16_t value){
+float diff_s(float value){
        return ((float)value-PRUM_SOUC)*100/PRUM_SOUC;
 }
 /*
  * tiskne potrebna data
  */
 void printData(struct rpi_in data){
+       float cur0, cur1, cur2;
+       if (data.adc_m_count){
+               cur0=data.ch0/data.adc_m_count;
+               cur1=data.ch1/data.adc_m_count;
+               cur2=data.ch2/data.adc_m_count;
+       }
        printf("\npozice=%d\n",(int32_t)data.pozice);
        printf("hal1=%d, hal2=%d, hal3=%d\n",data.hal1,data.hal2,data.hal3);
-       printf("en1=%d, en2=%d, en3=%d (Predchozi hodnoty)\n",data.en1,data.en2,data.en3);
-       printf("shdn1=%d, shdn2=%d, shdn3=%d (P.h.)\n",data.shdn1,data.shdn2,data.shdn3);
-       printf("PWM1(10d5)b54=%d %d %d %d %d %d=b49(P.h.)\n",data.b54,data.b53,data.b52,data.b51,data.b50,data.b49);
-       printf("PWM2(10d4)b48=%d %d %d %d %d %d %d=b42(P.h.)\n",data.b48,data.b47,data.b46,data.b45,data.b44,data.b43,data.b42);
-       printf("PWM3(10d5)b41=%d %d %d %d %d %d=b36(P.h.)\n",data.b41,data.b40,data.b39,data.b38,data.b37,data.b36);
-       printf("(pwm1)proud1 (ch1)=%d (%2.2f%%) %x\n",data.ch1,diff_p(data.ch1),(uint16_t)data.ch1);
-       printf("(pwm2)proud2 (ch2)=%d (%2.2f%%) %x\n",data.ch2,diff_p(data.ch2),(uint16_t)data.ch2);
-       printf("(pwm3)proud3 (ch0)=%d (%2.2f%%) %x\n",data.ch0,diff_p(data.ch0),(uint16_t)data.ch0);
-       printf("soucet proudu=%d (%2.2f%%)\n",data.ch0+data.ch1+data.ch2,diff_s(data.ch0+data.ch1+data.ch2));
+       //printf("en1=%d, en2=%d, en3=%d (Predchozi hodnoty)\n",data.en1,data.en2,data.en3);
+       //printf("shdn1=%d, shdn2=%d, shdn3=%d (P.h.)\n",data.shdn1,data.shdn2,data.shdn3);
+       //printf("PWM1(10d5)b54=%d %d %d %d %d %d=b49(P.h.)\n",data.b54,data.b53,data.b52,data.b51,data.b50,data.b49);
+       //printf("PWM2(10d4)b48=%d %d %d %d %d %d %d=b42(P.h.)\n",data.b48,data.b47,data.b46,data.b45,data.b44,data.b43,data.b42);
+       //printf("PWM3(10d5)b41=%d %d %d %d %d %d=b36(P.h.)\n",data.b41,data.b40,data.b39,data.b38,data.b37,data.b36);
+       printf("Pocet namerenych proudu=%u\n",data.adc_m_count);
+       printf("(pwm1)proud1 (ch1)=%d (avg=%4.0f) (%2.2f%%)\n",data.ch1,cur1,diff_p(cur1));
+       printf("(pwm2)proud2 (ch2)=%d (avg=%4.0f)(%2.2f%%)\n",data.ch2,cur2,diff_p(cur2));
+       printf("(pwm3)proud3 (ch0)=%d (avg=%4.0f)(%2.2f%%)\n",data.ch0,cur0,diff_p(cur0));
+       printf("soucet prumeru=%5.0f (%2.2f%%)\n",cur0+cur1+cur2,diff_s(cur0+cur1+cur2));
 }
 void prepare_tx(uint8_t * tx){
 
index 98d4eca..ac3922d 100644 (file)
@@ -151,28 +151,16 @@ struct rpi_in spi_read(uint8_t * tx)
        in.shdn2=!!(0x01 & rx[4]);
        in.shdn3=!!(0x80 & rx[5]);
 
-       /*debug bits
-        *
+       /* current measurments count
+        * bits 80 downto 72
+        * bit 80 in rx[5]
+        * bits 79..72 in rx[6]
         */
-       in.b54=!!(0x40 & rx[5]);
-       in.b53=!!(0x20 & rx[5]);
-       in.b52=!!(0x10 & rx[5]);
-       in.b51=!!(0x08 & rx[5]);
-       in.b50=!!(0x04 & rx[5]);
-       in.b49=!!(0x02 & rx[5]);
-       in.b48=!!(0x01 & rx[5]);
-       in.b47=!!(0x80 & rx[6]);
-       in.b46=!!(0x40 & rx[6]);
-       in.b45=!!(0x20 & rx[6]);
-       in.b44=!!(0x10 & rx[6]);
-       in.b43=!!(0x08 & rx[6]);
-       in.b42=!!(0x04 & rx[6]);
-       in.b41=!!(0x02 & rx[6]);
-       in.b40=!!(0x01 & rx[6]);
-       in.b39=!!(0x80 & rx[7]);
-       in.b38=!!(0x40 & rx[7]);
-       in.b37=!!(0x20 & rx[7]);
-       in.b36=!!(0x10 & rx[7]);
+
+       in.adc_m_count=0x01 & rx[5];
+       in.adc_m_count<<=8;
+       in.adc_m_count|=rx[6];
+
 
        /** currents
         * ch0 - bits 71 downto 48
index 999d7b3..4486791 100644 (file)
@@ -14,6 +14,7 @@ struct rpi_in{
        int8_t en1, en2, en3;           /*(bool)last read pwm-enable values - !they are changed after reading ! */
        int8_t shdn1,shdn2,shdn3;       /*(bool)last read shutdown values - !they are changed after reading ! */
        int8_t b54, b53, b52, b51, b50, b49, b48, b47, b46, b45, b44, b43, b42, b41, b40, b39, b38, b37, b36; /*bits for debug*/
+       uint16_t adc_m_count;           /*current measurments count*/
 };
 
 /**