Skip to content

Commit 34a15e4

Browse files
committed
added logging
1 parent 2d5f2dd commit 34a15e4

File tree

1 file changed

+60
-13
lines changed

1 file changed

+60
-13
lines changed

libraries/AP_Scripting/examples/plane-sweep.lua

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local SWEEP_ACTION_CHANNEL = 6 -- RCIN channel to start a SWEEP when high (>1700
1212
local SWEEP_CHOICE_CHANNEL = 7 -- RCIN channel to choose elevator (low) or rudder (high)
1313
local SWEEP_FUCNTION = 1 -- which control surface (SERVOx_FUNCTION) number will have a SWEEP happen
1414
-- A (Servo 1, Function 4), E (Servo 2, Function 19), and R (Servo 4, Function 21)
15-
local SWEEP_MAGNITUDE = 50 -- defined out of 45 deg used for set_output_scaled
15+
local SWEEP_MAGNITUDE = 200 -- defined out of 45 deg used for set_output_scaled
1616
local SWEEP_TIME = 120000 -- period of SWEEP signal in ms
1717
local start_freq = 0.05 -- start freqeuncy in hz
1818
local end_freqeuncy = 5 -- end freqeuncy in Hz
@@ -29,6 +29,24 @@ local K_THROTTLE = 70
2929
local K_RUDDER = 21
3030
local SWEEP_srv_trim = -100
3131

32+
--- logging parameters
33+
local roll_rate = 1
34+
local pitch_rate = 2
35+
local yaw_rate = 3
36+
local acc_x = 4
37+
local acc_y = 5
38+
local acc_z = 6
39+
local RcOut = 7
40+
local t_log = 8
41+
local input = 9
42+
local start_freq_rads = 2 * math.pi * start_freq
43+
local end_freq_rads = 2 * math.pi * end_freqeuncy
44+
local B = math.log(end_freq_rads / start_freq_rads)
45+
local k = B / SWEEP_TIME
46+
47+
48+
local interesting_data = {}
49+
3250
function retry_set_mode(mode)
3351
if vehicle:set_mode(mode) then
3452
-- if the mode was set successfully, carry on as normal
@@ -40,11 +58,23 @@ function retry_set_mode(mode)
4058
return retry_set_mode, 1
4159
end
4260
end
61+
local function write_to_dataflash()
62+
63+
-- care must be taken when selecting a name, must be less than four characters and not clash with an existing log type
64+
-- format characters specify the type of variable to be logged, see AP_Logger/README.md
65+
-- https://github.com/ArduPilot/ardupilot/tree/master/libraries/AP_Logger
66+
-- not all format types are supported by scripting only: i, L, e, f, n, M, B, I, E, and N
67+
-- lua automatically adds a timestamp in micro seconds
68+
-- logger:write('SCR1','Gx(deg),Gy(deg),Gz(deg)','fff',interesting_data[roll_rate],interesting_data[pitch_rate],interesting_data[yaw_rate])
69+
logger:write('SCR1', 'Gx,Gy,Gz,accx,accy,accz,RCOut,input,time', 'fffffffff', interesting_data[roll_rate], interesting_data[pitch_rate], interesting_data[yaw_rate],interesting_data[acc_x], interesting_data[acc_y], interesting_data[acc_z],interesting_data[RcOut],interesting_data[input],interesting_data[t_log])
4370

71+
72+
73+
end
4474
function SWEEP(pre_SWEEP_elevator, pre_SWEEP_aileron, pre_SWEEP_rudder, pre_SWEEP_throttle)
4575
local now = millis()
4676
local SWEEP_choice_pwm = rc:get_pwm(SWEEP_CHOICE_CHANNEL)
47-
local callback_time = 100
77+
local callback_time = 10
4878
if rc:get_pwm(SWEEP_ACTION_CHANNEL) < 1700 then
4979
pre_SWEEP_elevator = SRV_Channels:get_output_pwm(K_ELEVATOR)
5080
pre_SWEEP_aileron = SRV_Channels:get_output_pwm(K_AILERON)
@@ -53,6 +83,10 @@ function SWEEP(pre_SWEEP_elevator, pre_SWEEP_aileron, pre_SWEEP_rudder, pre_SWEE
5383
end
5484
if arming:is_armed() == true and rc:get_pwm(SWEEP_ACTION_CHANNEL) > 1700 then
5585
gcs:send_text(6, "in SWEEP function")
86+
pre_SWEEP_elevator = 1465
87+
pre_SWEEP_aileron = SRV_Channels:get_output_pwm(K_AILERON)
88+
pre_SWEEP_rudder = SRV_Channels:get_output_pwm(K_RUDDER)
89+
pre_SWEEP_throttle = SRV_Channels:get_output_pwm(K_THROTTLE)
5690
-- choosing control actuator, 900 for elevator, 1100 for aileron, 1300 for rudder, 1500 for throttle
5791
if SWEEP_choice_pwm < 1000 then
5892
SWEEP_FUCNTION = K_ELEVATOR
@@ -90,21 +124,34 @@ function SWEEP(pre_SWEEP_elevator, pre_SWEEP_aileron, pre_SWEEP_rudder, pre_SWEE
90124
retry_set_mode(MODE_MANUAL)
91125
end
92126
if now < (start_time + (SWEEP_TIME)) then
93-
-- gcs:send_text(6, "SWEEP up" .. SWEEP_srv_chan)
94-
-- up = SWEEP_srv_trim + math.floor((SWEEP_srv_max - SWEEP_srv_trim) * (SWEEP_MAGNITUDE / 45))
95-
-- SRV_Channels:set_output_pwm_chan_timeout(SWEEP_srv_chan, up, SWEEP_TIME / 2 + 100)
96-
-- gcs:send_text(6, "pwm" .. tostring(SRV_Channels:get_output_pwm(SWEEP_FUCNTION)))
97-
-- elseif now < (start_time + SWEEP_TIME) and now > (start_time + (SWEEP_TIME / 2))then
98-
-- gcs:send_text(6, "SWEEP down" .. SWEEP_srv_chan)
99-
-- down = SWEEP_srv_trim - math.floor((SWEEP_srv_trim - SWEEP_srv_min) * (SWEEP_MAGNITUDE / 45))
100-
-- SRV_Channels:set_output_pwm_chan_timeout(SWEEP_srv_chan, down, SWEEP_TIME / 2 + 100)
101-
-- gcs:send_text(6, "pwm" .. tostring(SRV_Channels:get_output_pwm(SWEEP_FUCNTION)))
102127
gcs:send_text(6, "in sweep loop" .. SWEEP_srv_chan)
103128
t_i = tonumber(tostring(now - start_time))
104129
amplitude_sent = SWEEP_MAGNITUDE*math.sin(tonumber(2*math.pi*(start_freq * t_i + (k / 2) * t_i*t_i)))
130+
-- amplitude_sent = SWEEP_MAGNITUDE * math.sin(start_freq_rads * (math.exp(k * t_i) - 1) / k)
105131
amplitude_sent = math.floor(amplitude_sent)
106132
SRV_Channels:set_output_pwm_chan_timeout(SWEEP_srv_chan,SWEEP_srv_trim + amplitude_sent,40)
107-
gcs:send_text(6, "pwm" .. tostring(SRV_Channels:get_output_pwm(SWEEP_FUCNTION)))
133+
local op = SRV_Channels:get_output_pwm(SWEEP_FUCNTION)
134+
gcs:send_text(6, "pwm" .. tostring(op))
135+
local rate = ahrs:get_gyro()
136+
gcs:send_text(6, "rate[1]" .. tostring(rate:x()))
137+
local acc = ahrs:get_accel()
138+
gcs:send_text(6, "acc[1]" .. tostring(acc:x()))
139+
if rate and acc then
140+
interesting_data[roll_rate] = rate:x()
141+
interesting_data[pitch_rate] = rate:y()
142+
interesting_data[yaw_rate] = rate:z()
143+
interesting_data[acc_x] = acc:x()
144+
interesting_data[acc_y] = acc:y()
145+
interesting_data[acc_z] = acc:z()
146+
interesting_data[RcOut] = op
147+
interesting_data[t_log] = t_i/1000
148+
interesting_data[input] = amplitude_sent
149+
write_to_dataflash()
150+
else
151+
gcs:send_text(6, "Invalid rate or acc data")
152+
end
153+
154+
108155

109156
elseif now > start_time + (SWEEP_TIME) then
110157
-- stick fixed response
@@ -126,4 +173,4 @@ local pre_SWEEP_elevator = SRV_Channels:get_output_pwm(K_ELEVATOR)
126173
local pre_SWEEP_aileron = SRV_Channels:get_output_pwm(K_AILERON)
127174
local pre_SWEEP_rudder = SRV_Channels:get_output_pwm(K_RUDDER)
128175
local pre_SWEEP_throttle = SRV_Channels:get_output_pwm(K_THROTTLE)
129-
return SWEEP(pre_SWEEP_elevator, pre_SWEEP_aileron, pre_SWEEP_rudder, pre_SWEEP_throttle), 500
176+
return SWEEP(pre_SWEEP_elevator, pre_SWEEP_aileron, pre_SWEEP_rudder, pre_SWEEP_throttle), 40

0 commit comments

Comments
 (0)