Skip to content

Commit

Permalink
Add file for presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
khancyr committed May 22, 2024
1 parent 17083b5 commit 0ecfd8d
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 20 deletions.
11 changes: 11 additions & 0 deletions 2rm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

Set SCR_ENABLE 1
use RC 8 to set/unset standby
use RC_override in broadcast to send to both FCUs.

Tools/autotest/sim_vehicle.py -v ArduCopter -f + --slave 1 -I0 --sysid 1 --use-dir=FCU1 --add-param-file=$(pwd)/2rm.parm -A "--serial1=tcp:5761 " --console --map --no-rcin

Tools/autotest/sim_vehicle.py -v ArduCopter --model json:0.0.0.0 --slave 0 -I1 --sysid 2 --use-dir=FCU2 --add-param-file=$(pwd)/2rm.parm -A "--serial1=tcpclient:127.0.0.1:5761 " --debug --no-rebuild -m "--console --source-system 252" --no-rcin


Tools/autotest/sim_vehicle.py -v ArduCopter -f + -I0 --sysid 1 --use-dir=FCU1 --add-param-file=$(pwd)/2rm.parm -A "--serial1=tcp:5761 " --add-param-file=$(pwd)/2rm.parm --console --map --no-rcin
1 change: 1 addition & 0 deletions 2rm.parm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SCR_ENABLE 1
95 changes: 95 additions & 0 deletions FCU1/scripts/redundance.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
-- This script is a test of param set and get

local count = 0
local last_value = 0
local last_rc8_input = 0

local switch_high = 2
local switch_low = 0
local standby_function = 76

local scripting_rc_1 = rc:find_channel_for_option(300)


-- for fast param acess it is better to get a param object,
-- this saves the code searching for the param by name every time
local JSON_MASTER = Parameter()
if not JSON_MASTER:init('SIM_JSON_MASTER') then
gcs:send_text(6, 'get JSON_MASTER failed')
end

local SYSID_THISMAV = Parameter()
if not SYSID_THISMAV:init('SYSID_THISMAV') then
gcs:send_text(6, 'get SYSID_THISMAV failed')
end


local sysid = SYSID_THISMAV:get()

-- this allows this example to catch the otherwise fatal error
-- not recommend if error is possible/expected, use separate construction and init

-- local user_param = Parameter('SCR_USER1')
-- is equivalent to:
-- local user_param = Parameter()
-- assert(user_param:init('SCR_USER1'), 'No parameter: SCR_USER1')
gcs:send_text(6, 'LUA: hello')

function update() -- this is the loop which periodically runs

-- get and print all the scripting parameters
local value = JSON_MASTER:get()
if value then
if value ~= last_value then
gcs:send_text(6, string.format('LUA: SIM_JSON_MASTER: %i',value))
last_value = value
end
else
gcs:send_text(6, 'LUA: get SIM_JSON_MASTER failed')
end

rc8_input = rc:get_pwm(8)
-- standby switch enable == high

if rc8_input ~= last_rc8_input then
if sysid == 2 then
if rc8_input > 1500 then
if not JSON_MASTER:set(0) then
gcs:send_text(6, string.format('LUA: failed to set JSON_MASTER'))
else
rc:run_aux_function(standby_function, switch_high)
gcs:send_text(6, string.format('LUA: set JSON_MASTER to 0'))
end
else
if not JSON_MASTER:set(1) then
gcs:send_text(6, string.format('LUA: failed to set JSON_MASTER'))
else
rc:run_aux_function(standby_function, switch_low)
gcs:send_text(6, string.format('LUA: set JSON_MASTER to 1'))
end
end
end
if sysid == 1 then
if rc8_input > 1500 then
if not JSON_MASTER:set(0) then
gcs:send_text(6, string.format('LUA: failed to set JSON_MASTER'))
else
rc:run_aux_function(standby_function, switch_low)
gcs:send_text(6, string.format('LUA: set JSON_MASTER to 0'))
end
else
if not JSON_MASTER:set(1) then
gcs:send_text(6, string.format('LUA: failed to set JSON_MASTER'))
else
rc:run_aux_function(standby_function, switch_high)
gcs:send_text(6, string.format('LUA: set JSON_MASTER to 1'))
end
end
end
last_rc8_input = rc8_input
end

return update, 1000 -- reschedules the loop
end

return update() -- run immediately before starting to reschedule
95 changes: 95 additions & 0 deletions FCU2/scripts/redundance.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
-- This script is a test of param set and get

local count = 0
local last_value = 0
local last_rc8_input = 0

local switch_high = 2
local switch_low = 0
local standby_function = 76

local scripting_rc_1 = rc:find_channel_for_option(300)


-- for fast param acess it is better to get a param object,
-- this saves the code searching for the param by name every time
local JSON_MASTER = Parameter()
if not JSON_MASTER:init('SIM_JSON_MASTER') then
gcs:send_text(6, 'get JSON_MASTER failed')
end

local SYSID_THISMAV = Parameter()
if not SYSID_THISMAV:init('SYSID_THISMAV') then
gcs:send_text(6, 'get SYSID_THISMAV failed')
end


local sysid = SYSID_THISMAV:get()

-- this allows this example to catch the otherwise fatal error
-- not recommend if error is possible/expected, use separate construction and init

-- local user_param = Parameter('SCR_USER1')
-- is equivalent to:
-- local user_param = Parameter()
-- assert(user_param:init('SCR_USER1'), 'No parameter: SCR_USER1')
gcs:send_text(6, 'LUA: hello')

function update() -- this is the loop which periodically runs

-- get and print all the scripting parameters
local value = JSON_MASTER:get()
if value then
if value ~= last_value then
gcs:send_text(6, string.format('LUA: SIM_JSON_MASTER: %i',value))
last_value = value
end
else
gcs:send_text(6, 'LUA: get SIM_JSON_MASTER failed')
end

rc8_input = rc:get_pwm(8)
-- standby switch enable == high

if rc8_input ~= last_rc8_input then
if sysid == 2 then
if rc8_input > 1500 then
if not JSON_MASTER:set(0) then
gcs:send_text(6, string.format('LUA: failed to set JSON_MASTER'))
else
rc:run_aux_function(standby_function, switch_high)
gcs:send_text(6, string.format('LUA: set JSON_MASTER to 0'))
end
else
if not JSON_MASTER:set(1) then
gcs:send_text(6, string.format('LUA: failed to set JSON_MASTER'))
else
rc:run_aux_function(standby_function, switch_low)
gcs:send_text(6, string.format('LUA: set JSON_MASTER to 1'))
end
end
end
if sysid == 1 then
if rc8_input > 1500 then
if not JSON_MASTER:set(0) then
gcs:send_text(6, string.format('LUA: failed to set JSON_MASTER'))
else
rc:run_aux_function(standby_function, switch_low)
gcs:send_text(6, string.format('LUA: set JSON_MASTER to 0'))
end
else
if not JSON_MASTER:set(1) then
gcs:send_text(6, string.format('LUA: failed to set JSON_MASTER'))
else
rc:run_aux_function(standby_function, switch_high)
gcs:send_text(6, string.format('LUA: set JSON_MASTER to 1'))
end
end
end
last_rc8_input = rc8_input
end

return update, 1000 -- reschedules the loop
end

return update() -- run immediately before starting to reschedule
50 changes: 30 additions & 20 deletions libraries/AP_HAL_SITL/UARTDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,20 +402,18 @@ void UARTDriver::_tcp_start_connection(uint16_t port, bool wait_for_connection)
*/
void UARTDriver::_tcp_start_client(const char *address, uint16_t port)
{
int one=1;
struct sockaddr_in sockaddr;
int ret;

if (_connected) {
return;
}

_use_send_recv = true;

if (_fd != -1) {
close(_fd);
}

struct sockaddr_in sockaddr;
memset(&sockaddr,0,sizeof(sockaddr));

#ifdef HAVE_SOCK_SIN_LEN
Expand All @@ -425,32 +423,44 @@ void UARTDriver::_tcp_start_client(const char *address, uint16_t port)
sockaddr.sin_family = AF_INET;
sockaddr.sin_addr.s_addr = inet_addr(address);

_fd = socket(AF_INET, SOCK_STREAM, 0);
if (_fd == -1) {
fprintf(stderr, "socket failed - %s\n", strerror(errno));
exit(1);
}
ret = fcntl(_fd, F_SETFD, FD_CLOEXEC);
if (ret == -1) {
fprintf(stderr, "fcntl failed on setting FD_CLOEXEC - %s\n", strerror(errno));
exit(1);
}
constexpr auto one=1;
int ret;
for (int attempt = 0; attempt < 3; ++attempt) {
_fd = socket(AF_INET, SOCK_STREAM, 0);
if (_fd == -1) {
fprintf(stderr, "socket failed - %s\n", strerror(errno));
exit(1);
}
ret = fcntl(_fd, F_SETFD, FD_CLOEXEC);
if (ret == -1) {
fprintf(stderr, "fcntl failed on setting FD_CLOEXEC - %s\n", strerror(errno));
exit(1);
}

/* we want to be able to re-use ports quickly */
setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
/* we want to be able to re-use ports quickly */
setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));

ret = connect(_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
if (ret == 0) {
break;
}
fprintf(stderr, "connect failed on port %u - %s at %d retrying\n",
(unsigned) ntohs(sockaddr.sin_port), strerror(errno), AP_HAL::millis());
close(_fd);
// If connection failed, wait for a bit before retrying
sleep(1);
}

ret = connect(_fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
if (ret == -1) {
fprintf(stderr, "connect failed on port %u - %s\n",
(unsigned)ntohs(sockaddr.sin_port),
strerror(errno));
close(_fd);
exit(1);
}

setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
setsockopt(_fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one));
fcntl(_fd, F_SETFD, FD_CLOEXEC);
_connected = true;
fprintf(stdout, "New remote connection on serial port %u, p %u at %d\n", _portNumber, (unsigned) ntohs(sockaddr.sin_port), AP_HAL::millis());
}


Expand Down

0 comments on commit 0ecfd8d

Please sign in to comment.