-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMotorMatrixControl.vhd
More file actions
296 lines (276 loc) · 13.1 KB
/
MotorMatrixControl.vhd
File metadata and controls
296 lines (276 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity MotorMatrixControl is
generic (
rows : positive := 2; -- Número de linhas da matriz de motores
cols : positive := 5; -- Número de colunas da matriz de motores
dataWidth : positive := 32 -- Tamanho de cada entrada data
);
port (
clock, reset : in std_logic; -- Sinais padrões de clock e reset
enable : in std_logic; -- Controla quando o sistema começa a funcionar
acknowledge : in std_logic; -- Reconhece quando recebe o interrupt = 1
test : in std_logic; -- Entrada para começar o teste
bistIn : in std_logic; -- Entrada serial de teste
readwrite : in std_logic_vector(1 downto 0); -- Indica se lê ("10") ou escreve ("01") nos registradores
writedata : in std_logic_vector(dataWidth-1 downto 0); -- Entrada que escreve no registrador de dados
readdata : out std_logic_vector(dataWidth-1 downto 0); -- Saída que envia o registrador de status
interrupt : out std_logic; -- Sinal que sinaliza que algum comando foi concluído
bistOut : out std_logic; -- Saída serial de teste
motors_out : out std_logic_vector(rows*cols-1 downto 0) -- Saída criada apesar de não estar nas especificações, para poder ligar os motores
);
end entity; -- MotorMatrixControl
architecture structural of MotorMatrixControl is
type motor_data_array is array (0 to rows*cols) of std_logic_vector(7 downto 0); -- Vetor com as saídas para ligar os motores
constant motor_data_width, row_col_width, command_width : natural := 8; -- Tamanho dos dados do motor, da linha e coluna e dos comandos
constant timer_width : natural := 20; -- Tamanho do contador do delay. Para teste utilizar := 9
signal reg_command_data_out, reg_status_in : std_logic_vector(dataWidth-1 downto 0); -- Sinais internos dos registradores
signal command : std_logic_vector(command_width-1 downto 0); -- Sinal para receber os comandos
signal row, col : std_logic_vector(row_col_width-1 downto 0); -- Sinais para receber as linhas e colunas
signal data : std_logic_vector(motor_data_width-1 downto 0); -- Sinal para receber os dados
signal row_out, col_out : std_logic_vector(row_col_width-1 downto 0); -- Saída do controle com as linhas e colunas a serem modificadas
signal timer : std_logic_vector(timer_width-1 downto 0); -- Timer criado para fazer a contagem PWM de todos os motores e também para criar um atraso
signal sel_data_registers, sel_input_data_registers : std_logic_vector(1 downto 0); -- Seletores de qual registrador do motor deve ser escrito e de qual entrada pega o valor
signal new_command, clock_timer : std_logic; -- Sinal do controle que avisa que recebeu novo comando e sinal do clock que gera o delay
signal motors_data : motor_data_array; -- Sinal de saída dos motores
-- Timer que cria um contador compartilhado entre todos os motores para fazer o PWM e que gera um clock para o delay. O clock pode ser substituido por 1 externo.
component timer_N_bits
generic (
N : natural := 14
);
port (
clock : in std_logic;
timer : out std_logic_vector(N-1 downto 0);
clock_timer : out std_logic
);
end component;
-- Registrador geral de data_width bits com enable e reset assíncrono
component register_data_width
generic (
data_width : natural := 32
);
port (
clock, reset : in std_logic;
enable : in std_logic;
inpt : in std_logic_vector(dataWidth-1 downto 0);
outpt : out std_logic_vector(dataWidth-1 downto 0)
);
end component;
-- FSM de controle que diz qual comando, como e quando será executado. Também retorna interrupt = 1 quando termina um comando.
component fsm_commands
generic (
command_width : natural := 8;
row_col_width : natural := 8;
data_width : natural := 32;
rows : natural := 4;
cols : natural := 4
);
port (
clock, reset : in std_logic;
enable : in std_logic;
acknowledge : in std_logic;
command : in std_logic_vector(command_width-1 downto 0);
row, col : in std_logic_vector(row_col_width-1 downto 0);
row_out, col_out : out std_logic_vector(row_col_width-1 downto 0);
status : out std_logic_vector(data_width-1 downto 0);
sel_data_registers : out std_logic_vector(1 downto 0);
sel_input_data_registers : out std_logic_vector(1 downto 0);
command_done : out std_logic;
new_command : out std_logic
);
end component;
-- Controle de um motor por PWM que recebe potência, variação e decay da entrada ou dos motores anteriores (cima ou esquerda)
component motor
generic (
row_col_width : natural := 8;
own_row, own_col : natural := 0;
motor_data_width : natural := 8
);
port (
clock, reset : in std_logic;
new_command : in std_logic;
clock_timer : in std_logic;
timer : in std_logic_vector(motor_data_width-1 downto 0);
row, col : in std_logic_vector(row_col_width-1 downto 0);
sel_data_registers : in std_logic_vector(1 downto 0);
sel_input_data_registers : in std_logic_vector(1 downto 0);
motor_data : in std_logic_vector(motor_data_width-1 downto 0);
motor_up_data : in std_logic_vector(motor_data_width-1 downto 0);
motor_left_data : in std_logic_vector(motor_data_width-1 downto 0);
motor_data_out : out std_logic_vector(motor_data_width-1 downto 0);
power_wave : out std_logic
);
end component;
begin
-- Atribuição dos valores de command_data
command <= reg_command_data_out(dataWidth-1 downto dataWidth-8); -- Comando a ser executado
row <= reg_command_data_out(dataWidth-9 downto dataWidth-16); -- Número da linha (começa com 0, 1, 2, ..., n. 255 para todos)
col <= reg_command_data_out(dataWidth-17 downto dataWidth-24); -- Número da coluna (começa com 0, 1, 2, ..., n. 255 para todos)
data <= reg_command_data_out(dataWidth-25 downto dataWidth-32); -- Valor de potência
-- Timer de timer_width bits
timer_14_bits : timer_N_bits
generic map (
N => timer_width
)
port map (
clock => clock,
timer => timer,
clock_timer => clock_timer
);
-- Registrador de comandos, dados, linha e coluna
reg_command_data : register_data_width
generic map (
data_width => dataWidth
)
port map (
clock => clock,
reset => reset,
enable => readwrite(0),
inpt => writedata,
outpt => reg_command_data_out
);
-- Registrador de status ainda não utilizado
reg_status : register_data_width
generic map (
data_width => dataWidth
)
port map (
clock => clock,
reset => reset,
enable => readwrite(1),
inpt => reg_status_in,
outpt => readdata
);
-- FSM de controle dos comandos
commands_control : fsm_commands
generic map (
command_width => command_width,
row_col_width => row_col_width,
data_width => dataWidth,
rows => rows,
cols => cols
)
port map (
clock => clock,
reset => reset,
enable => enable,
acknowledge => acknowledge,
command => command,
row => row,
col => col,
row_out => row_out,
col_out => col_out,
status => reg_status_in,
sel_data_registers => sel_data_registers,
sel_input_data_registers => sel_input_data_registers,
command_done => interrupt,
new_command => new_command
);
-- Todos os motores estão descritos abaixo.
-- Foi necessário utilizar 4 for generates pois a primeira linha e a prmieira coluna recebe 0 na entrada dos motores anteriores.
motor_0x0 : motor
generic map(
row_col_width => row_col_width,
own_row => 0,
own_col => 0,
motor_data_width => motor_data_width
)
port map(
clock => clock,
reset => reset,
new_command => new_command,
clock_timer => clock_timer,
timer => timer(motor_data_width-1 downto 0),
row => row_out,
col => col_out,
sel_data_registers => sel_data_registers,
sel_input_data_registers => sel_input_data_registers,
motor_data => data,
motor_up_data => (others => '0'),
motor_left_data => (others => '0'),
motor_data_out => motors_data(0),
power_wave => motors_out(0)
);
motors_0xi :
for i in 1 to cols-1 generate
n_motors_0xi : motor
generic map(
row_col_width => row_col_width,
own_row => 0,
own_col => i,
motor_data_width => motor_data_width
)
port map(
clock => clock,
reset => reset,
new_command => new_command,
clock_timer => clock_timer,
timer => timer(motor_data_width-1 downto 0),
row => row_out,
col => col_out,
sel_data_registers => sel_data_registers,
sel_input_data_registers => sel_input_data_registers,
motor_data => data,
motor_up_data => (others => '0'),
motor_left_data => motors_data(i-1),
motor_data_out => motors_data(i),
power_wave => motors_out(i)
);
end generate;
motors_ix0 :
for i in 1 to rows-1 generate
n_motors_ix0 : motor
generic map(
row_col_width => row_col_width,
own_row => i,
own_col => 0,
motor_data_width => motor_data_width
)
port map(
clock => clock,
reset => reset,
new_command => new_command,
clock_timer => clock_timer,
timer => timer(motor_data_width-1 downto 0),
row => row_out,
col => col_out,
sel_data_registers => sel_data_registers,
sel_input_data_registers => sel_input_data_registers,
motor_data => data,
motor_up_data => motors_data((i-1)*cols),
motor_left_data => (others => '0'),
motor_data_out => motors_data(i*cols),
power_wave => motors_out(i*cols)
);
end generate;
motors_ixj :
for i in 1 to rows-1 generate
i_motors_ixj :
for j in 1 to cols-1 generate
j_motors_ixj : motor
generic map(
row_col_width => row_col_width,
own_row => i,
own_col => j,
motor_data_width => motor_data_width
)
port map(
clock => clock,
reset => reset,
new_command => new_command,
clock_timer => clock_timer,
timer => timer(motor_data_width-1 downto 0),
row => row_out,
col => col_out,
sel_data_registers => sel_data_registers,
sel_input_data_registers => sel_input_data_registers,
motor_data => data,
motor_up_data => motors_data((i-1)*cols + j),
motor_left_data => motors_data(i*cols + (j-1)),
motor_data_out => motors_data(i*cols + j),
power_wave => motors_out(i*cols + j)
);
end generate;
end generate;
end architecture; -- structural