@@ -12,7 +12,7 @@ local SWEEP_ACTION_CHANNEL = 6 -- RCIN channel to start a SWEEP when high (>1700
12
12
local SWEEP_CHOICE_CHANNEL = 7 -- RCIN channel to choose elevator (low) or rudder (high)
13
13
local SWEEP_FUCNTION = 1 -- which control surface (SERVOx_FUNCTION) number will have a SWEEP happen
14
14
-- 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
16
16
local SWEEP_TIME = 120000 -- period of SWEEP signal in ms
17
17
local start_freq = 0.05 -- start freqeuncy in hz
18
18
local end_freqeuncy = 5 -- end freqeuncy in Hz
@@ -29,6 +29,24 @@ local K_THROTTLE = 70
29
29
local K_RUDDER = 21
30
30
local SWEEP_srv_trim = - 100
31
31
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
+
32
50
function retry_set_mode (mode )
33
51
if vehicle :set_mode (mode ) then
34
52
-- if the mode was set successfully, carry on as normal
@@ -40,11 +58,23 @@ function retry_set_mode(mode)
40
58
return retry_set_mode , 1
41
59
end
42
60
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 ])
43
70
71
+
72
+
73
+ end
44
74
function SWEEP (pre_SWEEP_elevator , pre_SWEEP_aileron , pre_SWEEP_rudder , pre_SWEEP_throttle )
45
75
local now = millis ()
46
76
local SWEEP_choice_pwm = rc :get_pwm (SWEEP_CHOICE_CHANNEL )
47
- local callback_time = 100
77
+ local callback_time = 10
48
78
if rc :get_pwm (SWEEP_ACTION_CHANNEL ) < 1700 then
49
79
pre_SWEEP_elevator = SRV_Channels :get_output_pwm (K_ELEVATOR )
50
80
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
53
83
end
54
84
if arming :is_armed () == true and rc :get_pwm (SWEEP_ACTION_CHANNEL ) > 1700 then
55
85
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 )
56
90
-- choosing control actuator, 900 for elevator, 1100 for aileron, 1300 for rudder, 1500 for throttle
57
91
if SWEEP_choice_pwm < 1000 then
58
92
SWEEP_FUCNTION = K_ELEVATOR
@@ -90,21 +124,34 @@ function SWEEP(pre_SWEEP_elevator, pre_SWEEP_aileron, pre_SWEEP_rudder, pre_SWEE
90
124
retry_set_mode (MODE_MANUAL )
91
125
end
92
126
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)))
102
127
gcs :send_text (6 , " in sweep loop" .. SWEEP_srv_chan )
103
128
t_i = tonumber (tostring (now - start_time ))
104
129
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)
105
131
amplitude_sent = math.floor (amplitude_sent )
106
132
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
+
108
155
109
156
elseif now > start_time + (SWEEP_TIME ) then
110
157
-- stick fixed response
@@ -126,4 +173,4 @@ local pre_SWEEP_elevator = SRV_Channels:get_output_pwm(K_ELEVATOR)
126
173
local pre_SWEEP_aileron = SRV_Channels :get_output_pwm (K_AILERON )
127
174
local pre_SWEEP_rudder = SRV_Channels :get_output_pwm (K_RUDDER )
128
175
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