Skip to content

Commit c83f624

Browse files
committed
Added 10 CAN EPs, fixed brake knob, added ml draft.
1 parent b9a31fa commit c83f624

48 files changed

Lines changed: 1001 additions & 245 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bench/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ run: $(TARGET)
4242
@ echo " RUN " $(notdir $<)
4343
@ $< bench
4444

45+
data: $(TARGET)
46+
@ echo " DATA " $(notdir $<)
47+
@ $< data
48+
4549
debug: $(TARGET)
4650
@ echo " GDB " $(notdir $<)
4751
@ $(GDB) $<

bench/bench.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#define TLM_FILE "/tmp/pm-TLM"
1414
#define PWM_FILE "/tmp/pm-PWM"
15+
#define MLD_FILE "/tmp/pm-mldata"
1516
#define AGP_FILE "/tmp/pm-auto.gp"
1617

1718
#define TLM_SIZE 100
@@ -27,6 +28,7 @@ typedef struct {
2728

2829
FILE *fd_tlm;
2930
FILE *fd_pwm;
31+
FILE *fd_mld;
3032
FILE *fd_gp;
3133
}
3234
tlm_t;
@@ -215,6 +217,31 @@ tlm_plot_grab()
215217
if (tlm.fd_gp != NULL) { fclose(tlm.fd_gp); tlm.fd_gp = NULL; }
216218

217219
fwrite(tlm.y, sizeof(float), TLM_SIZE, tlm.fd_tlm);
220+
221+
if (tlm.fd_mld != NULL) {
222+
223+
D = cos(m.state[3]);
224+
Q = sin(m.state[3]);
225+
226+
C = (m.pwm_A + m.pwm_B + m.pwm_C) / 3.;
227+
A = (m.pwm_A - C) * m.state[6] / (double) m.pwm_resolution;
228+
B = (m.pwm_B - C) * m.state[6] / (double) m.pwm_resolution;
229+
230+
B = 0.577350269189626 * A + 1.15470053837925 * B;
231+
232+
tlm.y[0] = D * m.state[0] + Q * m.state[1]; /* iX */
233+
tlm.y[1] = D * m.state[1] - Q * m.state[0]; /* iY */
234+
tlm.y[2] = A; /* uX */
235+
tlm.y[3] = B; /* uY */
236+
tlm.y[4] = D; /* cos(\th) */
237+
tlm.y[5] = Q; /* sin(\th) */
238+
tlm.y[6] = m.state[2]; /* \omega */
239+
tlm.y[7] = m.state[4]; /* Tc */
240+
tlm.y[8] = 0;
241+
tlm.y[9] = 0;
242+
243+
fwrite(tlm.y, sizeof(float), 10, tlm.fd_mld);
244+
}
218245
}
219246

220247
static void
@@ -435,6 +462,61 @@ void bench_script()
435462
tlm_PWM_grab();
436463
}
437464

465+
void mld_script()
466+
{
467+
blm_enable(&m);
468+
blm_restart(&m);
469+
470+
tlm_restart();
471+
472+
m.Rs = 14.e-3;
473+
m.Ld = 10.e-6;
474+
m.Lq = 15.e-6;
475+
m.Udc = 22.;
476+
m.Rdc = 0.1;
477+
m.Zp = 14;
478+
m.lambda = blm_Kv_lambda(&m, 270.);
479+
m.Jm = 4.e-4;
480+
481+
ts_script_default();
482+
ts_script_base();
483+
blm_restart(&m);
484+
485+
pm.config_LU_ESTIMATE = PM_FLUX_KALMAN;
486+
pm.config_HFI_WAVETYPE = PM_HFI_SINE;
487+
488+
pm.fsm_req = PM_STATE_LU_STARTUP;
489+
ts_wait_IDLE();
490+
491+
pm.s_setpoint_speed = 60.f;
492+
sim_runtime(1.0);
493+
494+
tlm.fd_mld = fopen(MLD_FILE, "wb");
495+
496+
if (tlm.fd_mld == NULL) {
497+
498+
fprintf(stderr, "fopen: %s\n", strerror(errno));
499+
abort();
500+
}
501+
502+
pm.s_setpoint_speed = 60.f;
503+
sim_runtime(1.0);
504+
505+
pm.s_setpoint_speed = 900.f;
506+
sim_runtime(1.0);
507+
508+
pm.s_setpoint_speed = - 60.f;
509+
sim_runtime(1.0);
510+
511+
pm.s_setpoint_speed = - 900.f;
512+
sim_runtime(1.0);
513+
514+
pm.s_setpoint_speed = 5500.f;
515+
sim_runtime(2.0);
516+
517+
fclose(tlm.fd_mld);
518+
}
519+
438520
int main(int argc, char *argv[])
439521
{
440522
if (argc < 2) {
@@ -452,6 +534,10 @@ int main(int argc, char *argv[])
452534

453535
bench_script();
454536
}
537+
else if (strcmp(argv[1], "data") == 0) {
538+
539+
mld_script();
540+
}
455541

456542
if (tlm.fd_tlm != NULL) {
457543

bench/data.gp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env gp
2+
# vi:ft=conf
3+
4+
load 0 0 float 10 "/tmp/pm-mldata"
5+
6+
group 0 0
7+
deflabel 0 "(s)"
8+
9+
page "XY Current"
10+
label 1 "(A)"
11+
figure -1 0 "iX"
12+
figure -1 1 "iY"
13+
14+
page "XY Voltage"
15+
label 1 "(V)"
16+
figure -1 2 "uX"
17+
figure -1 3 "uY"
18+
19+
page "SIN/COS Position"
20+
figure -1 4 "COS"
21+
figure -1 5 "SIN"
22+
23+
page "Electrical Speed"
24+
figure -1 6 "\omega"
25+
26+
page "Machine Temperature"
27+
label 1 "(C)"
28+
figure -1 7 "Tc"

bench/keras.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/python3
2+
3+
import tensorflow as tf
4+
import os, random, keras, numpy as np
5+
6+
f = open('/tmp/pm-mldata', 'rb')
7+
raw = np.fromfile(f, dtype=np.float32).reshape((-1, 10))
8+
9+
f.close()
10+
11+
data = raw[:, :6] / np.array([50, 50, 20, 20, 1, 1])
12+
13+
fmean = np.mean(data, axis=0)
14+
fstd = np.max(data, axis=0) - np.min(data, axis=0)
15+
data = (data - fmean) / fstd
16+
17+
print(fmean)
18+
print(fstd)
19+
20+
length = data.shape[0]
21+
episode = 2000
22+
23+
train = []
24+
valid = []
25+
26+
random.seed(10)
27+
28+
for index in range(0, length, episode):
29+
if index + episode < length:
30+
if random.uniform(0, 100) < 70:
31+
train.append(data[index:index + episode, :])
32+
else:
33+
valid.append(data[index:index + episode, :])
34+
35+
train_data = np.stack(train)
36+
valid_data = np.stack(valid)
37+
38+
X = train_data[:, :, :4]
39+
Y = train_data[:, :, 4:6]
40+
vX = valid_data[:, :, :4]
41+
vY = valid_data[:, :, 4:6]
42+
43+
print(X.shape)
44+
print(Y.shape)
45+
print(vX.shape)
46+
print(vY.shape)
47+
48+
inputs = keras.layers.Input(shape=(None, 4))
49+
gru_out = keras.layers.GRU(20, return_sequences=True) (inputs)
50+
sincos = keras.layers.Dense(2, activation='tanh') (gru_out)
51+
outputs = [sincos]
52+
model = keras.models.Model(inputs, outputs)
53+
54+
model.compile(optimizer=keras.optimizers.Adam(),
55+
loss=keras.losses.MeanSquaredError())
56+
57+
model.summary()
58+
59+
model.fit(X, Y, validation_data=(vX, vY), epochs=300, batch_size=8)
60+
61+
#callbacks=keras.callbacks.EarlyStopping(patience=10)

bench/plot.gp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ page "E.Position Error"
2727
label 1 "(°)"
2828
figure 0 14 "m.theta - pm.lu_Fg"
2929

30-
page "Motor Temperature"
30+
page "Machine Temperature"
3131
label 1 "(C)"
3232
figure 0 5 "m.Tc"
3333

bench/tsfunc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ void ts_script_test()
892892
m.Rdc = 0.1;
893893
m.Zp = 14;
894894
m.lambda = blm_Kv_lambda(&m, 270.);
895-
m.Jm = 3.e-4;
895+
m.Jm = 4.e-4;
896896

897897
ts_script_default();
898898
ts_script_base();

doc/IntegritySelfTest.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ settings and depends on transient precess in power stages.
5858
It may be useful to enable PWM with constant DC when testing and debugging a
5959
new hardware.
6060

61+
(pmc) hal_PWM_set_Z 0
6162
(pmc) hal_PWM_set_DC <xDC>
6263

6364
## See also

pgui/config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ void config_default(struct config_phobia *fe)
245245

246246
strcpy(fe->fuzzy, "setpoint");
247247

248-
fe->regfile = 500;
248+
fe->regfile = 600;
249249
}
250250

251251
void config_storage_path(struct config_phobia *fe, char *lbuf, const char *file)

pgui/gp/async.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Graph Plotter is a tool to analyse numerical data.
3-
Copyright (C) 2025 Roman Belov <romblv@gmail.com>
3+
Copyright (C) 2026 Roman Belov <romblv@gmail.com>
44
55
This program is free software: you can redistribute it and/or modify
66
it under the terms of the GNU General Public License as published by

pgui/gp/async.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
Graph Plotter is a tool to analyse numerical data.
3-
Copyright (C) 2025 Roman Belov <romblv@gmail.com>
3+
Copyright (C) 2026 Roman Belov <romblv@gmail.com>
44
55
This program is free software: you can redistribute it and/or modify
66
it under the terms of the GNU General Public License as published by

0 commit comments

Comments
 (0)