Skip to content

Commit

Permalink
control: update
Browse files Browse the repository at this point in the history
  • Loading branch information
umarcor committed May 6, 2024
1 parent 8a2a569 commit 03f89d2
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 7 deletions.
22 changes: 22 additions & 0 deletions control/c/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <stdio.h>
#include "sim.c"

void main(){
float rk[] = {0.75, 0.5, 1};
float vk = 0.0;
float uk = 0.0;

int x, z;
int y = 0;

for ( z=0 ; z<3 ; z++ ) {
for ( x=0 ; x<250 ; x++ ) {
if ( y == 0 ) {
uk = Controller(rk[z] - vk);
}
vk = Motor(uk);
printf("%f %f %f\n", rk[z], uk, vk);
y = y>8 ? 0 : ++y;
}
}
}
46 changes: 46 additions & 0 deletions control/c/sim.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* File: motorsim.c
* Author: Koldo Basterretxea
*
* Created on 15 de abril de 2020, 12:51
*/

// Motor

const float c[] = {0.35003, 0.062554};
const float d[] = {-0.84659, 0.00183};

float v[] = {0, 0};
float th[] = {0, 0};

volatile float Motor(float vk) {
float thk = (c[0]*v[0]) + (c[1]*v[1]) - (d[0]*th[0]) - (d[1]*th[1]); // abiadura ardatzean rad/sec

v[1]=v[0];
v[0]=vk;
th[1]=th[0];
th[0]=thk;

return thk*9.549297; // rad/sec to rpm
}

// Controller

const float b[] = {0.005455, 0.003455};
const float a = -1;

volatile float uk = 0;
float uk1 = 0;
float ek1 = 0;;

volatile float Controller(float ek) {
uk = (b[0]*ek) + (b[1]*ek1) - (a*uk);
if (uk > 6) {
uk=6;
}

uk1=uk;
ek1=ek;

return uk;
}
23 changes: 21 additions & 2 deletions control/src/plant.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,36 @@ end entity;
---

architecture arch of plant is

signal s: std_logic_vector(7 downto 0);

constant c : real_vector(0 to 1) := (0.35003, 0.062554);
constant d : real_vector(0 to 1) := (-0.84659, 0.00183);

begin

process(CLK)
variable v : real_vector(0 to 1) := (others=>0.0);
variable th : real_vector(0 to 1) := (others=>0.0);
variable vk, thk : real;
begin
if rising_edge(CLK) then
if RST then
s <= (others=>'0');
elsif EN then
s <= std_logic_vector(signed(s)+1) when signed(I)>0 else
std_logic_vector(signed(s)-1) when signed(I)<0 else s;

--s <= std_logic_vector(signed(s)+1) when signed(I)>0 else
-- std_logic_vector(signed(s)-1) when signed(I)<0 else s;

-- vk := to_real(to_sfixed(I, 1, -6));
--
-- thk := (c(0)*v(0)) + (c(1)*v(1)) - (d(0)*th(0)) - (d(1)*th(1)); -- abiadura ardatzean rad/sec
--
-- v(1) := v(0);
-- v(0) := vk;
-- th(1) := th(0);
-- th(0) := thk;
-- O <= to_stdv(to_sfixed(thk*9.549297, 5, -2))
end if;
end if;
end process;
Expand Down
6 changes: 1 addition & 5 deletions control/test/tb_soft_twoproc_bin.vhd
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ begin
constant Dd : real := -0.9990;
begin
if rising_edge(clk) then
if rst = '1' then
y <= 0.0;
else
y <= Nd * u - Dd * y;
end if;
y <= 0.0 when rst else Nd * u - Dd * y;
end if;
end process;

Expand Down

0 comments on commit 03f89d2

Please sign in to comment.