Created
October 13, 2016 12:59
-
-
Save gabonator/079181a70c1cef4f55c0cb41104cfa99 to your computer and use it in GitHub Desktop.
Mavlink drone to sygic connector, uses GPS coordinates from 3DR drone streamed over 433 MHz link in sygic navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// setup | |
var portaddr = "COM68"; | |
var sygic = "10.0.10.68"; | |
// main | |
var SerialPort = require("serialport"); | |
var mavlink = require('mavlink'); | |
var mav = new mavlink(1,1); | |
var WebSocketClient = require('websocket').client; | |
var client = new WebSocketClient(); | |
var wsConnection = false; | |
var port = false; | |
// mavlink | |
mav.on("ready", function() { | |
port = new SerialPort(portaddr, { | |
baudRate: 57600 | |
}); | |
port.on('open', function() { | |
console.log("Port opened"); | |
onMavReady(); | |
}); | |
port.on('data', function(data) { | |
mav.parse(data); | |
}); | |
mav.on("message", function(message) { | |
// console.log(mav.getMessageName(message.id) + " " + JSON.stringify(message)); | |
}); | |
mav.on("GPS_RAW_INT", function(message, fields) { | |
var metersPerSecond = fields.vel / 100; | |
var kmh = fields.vel / 1000 * 3600; | |
var speed = {raw: fields.vel, speed:metersPerSecond, kmh:kmh}; | |
// console.log(speed); | |
if ( wsConnection ) | |
wsConnection.sendUTF("GPS.SetSpeed("+kmh.toFixed(2)+");\n"); | |
}); | |
mav.on("GLOBAL_POSITION_INT", function(message, fields) { | |
var coord = {lat:fields.lat/1e7, lon:fields.lon/1e7, heading:fields.hdg/100}; | |
console.log(coord); | |
if ( wsConnection ) | |
{ | |
wsConnection.sendUTF("GPS.SetCourse("+coord.heading.toFixed(1)+");\n"); | |
wsConnection.sendUTF("GPS.SetPosition("+Math.floor(coord.lon*100000)+","+Math.floor(coord.lat*100000)+");\n"); | |
} | |
}); | |
}); | |
// sygic | |
client.on('connectFailed', function(error) { | |
console.log('Connect Error: ' + error.toString()); | |
}); | |
client.on('connect', function(connection) { | |
console.log('WebSocket Client Connected'); | |
wsConnection = connection; | |
connection.on('error', function(error) { | |
console.log("Connection Error: " + error.toString()); | |
}); | |
connection.on('close', function() { | |
console.log('echo-protocol Connection Closed'); | |
}); | |
connection.on('message', function(message) { | |
if (message.type === 'utf8') { | |
console.log("Received: '" + message.utf8Data + "'"); | |
} | |
}); | |
}); | |
// debug | |
function onMavReady() | |
{ | |
client.connect("ws://"+sygic+":38950/"); | |
// mav.createMessage("MISSION_REQUEST_LIST", {target_system:1, target_component:1}, function(message) { port.write(message.buffer); }); | |
// mav.on("MISSION_COUNT", function(message, fields) { console.log(fields); }); | |
var nav_commands = []; | |
nav_commands[16] = "MAV_CMD_NAV_WAYPOINT"; | |
nav_commands[20] = "MAV_CMD_NAV_RETURN_TO_LAUNCH"; | |
nav_commands[22] = "MAV_CMD_NAV_TAKEOFF"; | |
//mav.createMessage("MISSION_REQUEST", {target_system:1, target_component:1, seq:2}, function(message) { port.write(message.buffer); }); | |
//mav.on("MISSION_ITEM", function(message, fields) { console.log(nav_commands[fields.command]); console.log(fields); }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment