-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpid_tune.rb
executable file
·84 lines (80 loc) · 1.73 KB
/
pid_tune.rb
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
#!/usr/bin/env ruby
#
#
# Generate the following parameters (12 bytes):
# int kp;
# int ki;
# int kd;
# uint_t k_div;
# uint_t u_mul;
# uint_t u_div;
#
def pid_generate(kp, ki, kd)
puts "Generate PID factors for Kp: #{kp}, Ki: #{ki}, Kd: #{kd}."
params = []
rkp = kp.rationalize
rki = ki.rationalize
rkd = kd.rationalize
kdiv = rkp.denominator.lcm(rki.denominator.lcm(rkp.denominator))
umul = udiv = 1
while true do
if kdiv > 65535
kdiv /= 2
next
end
ikp = rkp.numerator * kdiv / rkp.denominator
iki = rki.numerator * kdiv / rki.denominator
ikd = rkd.numerator * kdiv / rkd.denominator
break if ikp <= 65535 and iki <= 65535 and ikd <= 65535
kdiv /= 2
end
params[0] = ikp
params[1] = iki
params[2] = ikd
params[3] = kdiv
params[4] = umul
params[5] = udiv
params
end
data = [0x55aa]
#
# Generate PID data for roll.
data += pid_generate(1.013, 0.3335, 0.2)
#
# Generate PID data for pitch.
data += pid_generate(1.013, 0.1, 0.2)
#
# Generate PID data for yaw.
data += pid_generate(1.013, 0.1, 0.2)
#
# Add value for ESC divider
data += [600]
#
# Now push out the hex codes.
p data
offset = 0
File.open("promdata.eep", "w") do |f|
len = data.length * 2
while len > 0
nbytes = (len > 32) ? 32 : len
cksum = nbytes
cksum += (offset / 256) & 0xff
cksum += (offset & 0xff)
outstr = ":%02X%04X00" % [nbytes, offset]
value = 0
(0...nbytes).each do |i|
if (i & 1) == 0
value = data[(offset + i) / 2]
end
byte = value & 0xff
outstr += "%02X" % byte
cksum += byte
value >>= 8
end
outstr += "%02X\n" % (-cksum & 0xff)
f.puts outstr
offset += nbytes
len -= nbytes
end
f.puts ":00000001FF"
end