-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set mode #53
base: master
Are you sure you want to change the base?
Set mode #53
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,110 @@ | ||||||||
import logger from './common/logger'; | ||||||||
import _ from 'lodash'; | ||||||||
import { cleanupAfter } from './util'; | ||||||||
|
||||||||
const MODE_TIMEOUT = 15000; | ||||||||
|
||||||||
const mode_mapping_apm = { | ||||||||
0: 'MANUAL', | ||||||||
1: 'CIRCLE', | ||||||||
2: 'STABILIZE', | ||||||||
3: 'TRAINING', | ||||||||
4: 'ACRO', | ||||||||
5: 'FBWA', | ||||||||
6: 'FBWB', | ||||||||
7: 'CRUISE', | ||||||||
8: 'AUTOTUNE', | ||||||||
10: 'AUTO', | ||||||||
11: 'RTL', | ||||||||
12: 'LOITER', | ||||||||
14: 'LAND', | ||||||||
15: 'GUIDED', | ||||||||
16: 'INITIALISING', | ||||||||
17: 'QSTABILIZE', | ||||||||
18: 'QHOVER', | ||||||||
19: 'QLOITER', | ||||||||
20: 'QLAND', | ||||||||
21: 'QRTL', | ||||||||
22: 'QAUTOTUNE' | ||||||||
}; | ||||||||
|
||||||||
const nameMappingAPM = _.invert(mode_mapping_apm); | ||||||||
|
||||||||
let unknownWarn = true; | ||||||||
|
||||||||
const MAV_TYPE_FIXED_WING = 1; | ||||||||
const MAV_AUTOPILOT_ARDUPILOTMEGA = 3; | ||||||||
const MAV_CMD_DO_SET_MODE = 176; | ||||||||
const MAV_RESULT_ACCEPTED = 0; | ||||||||
const MAV_RESULT_TEMPORARILY_REJECTED = 1; | ||||||||
const MAV_RESULT_IN_PROGRESS = 5; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Frankly, I don't know what to do with all these constants |
||||||||
|
||||||||
export function onHeartbeat(fields) { | ||||||||
|
||||||||
let ov = this._overview; | ||||||||
ov.time = Date.now() / 1000; | ||||||||
|
||||||||
if (fields.type == MAV_TYPE_FIXED_WING && | ||||||||
fields.autopilot == MAV_AUTOPILOT_ARDUPILOTMEGA) { | ||||||||
ov.mode.name = mode_mapping_apm[fields.custom_mode]; | ||||||||
} else if (unknownWarn) { | ||||||||
unknownWarn = false; | ||||||||
if (fields.type !== MAV_TYPE_FIXED_WING) | ||||||||
logger.warn(`Unknown autopilot ${fields.autopilot}`); | ||||||||
if (fields.autopilot !== MAV_AUTOPILOT_ARDUPILOTMEGA) | ||||||||
logger.warn(`Unknown vehicle type ${fields.type}`); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These error messages are swapped |
||||||||
} | ||||||||
} | ||||||||
|
||||||||
export async function sendMode(mav, mode) { | ||||||||
let modeNumber = nameMappingAPM[mode.name]; | ||||||||
|
||||||||
if (modeNumber === undefined) { | ||||||||
throw new Error(`Invalid mode name ${mode.name}`); | ||||||||
} | ||||||||
|
||||||||
let setCurrentInt; | ||||||||
let onAck; | ||||||||
|
||||||||
let cleanupObj = cleanupAfter(() => { | ||||||||
clearInterval(setCurrentInt); | ||||||||
mav.removeListener('COMMAND_ACK', onAck); | ||||||||
}, MODE_TIMEOUT, 'setting mode took too long'); | ||||||||
|
||||||||
// Send a mode set current message repetitively. | ||||||||
_sendCommandLong(mav, modeNumber, 0); | ||||||||
let confirmation = 1; | ||||||||
|
||||||||
setCurrentInt = setInterval(() => { | ||||||||
return _sendCommandLong(mav, modeNumber, confirmation++); | ||||||||
}, 1000); | ||||||||
|
||||||||
onAck = (fields) => { | ||||||||
if (fields.type !== MAV_CMD_DO_SET_MODE) { | ||||||||
return; | ||||||||
} else if (fields.result === MAV_RESULT_ACCEPTED) { | ||||||||
cleanupObj.finish(); | ||||||||
} else if (fields.result === MAV_RESULT_TEMPORARILY_REJECTED | ||||||||
|| fields.result === MAV_RESULT_IN_PROGRESS) { | ||||||||
return; | ||||||||
} else { | ||||||||
let err = Error('COMMAND_ACK returned CMD ID ' + fields.result); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
cleanupObj.finish(err); | ||||||||
} | ||||||||
}; | ||||||||
|
||||||||
mav.on('COMMAND_ACK', onAck); | ||||||||
|
||||||||
await cleanupObj.wait(); | ||||||||
} | ||||||||
|
||||||||
async function _sendCommandLong(mav, modeNumber, confirmation) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name of this function is a little misleading since it's hardcoded to set |
||||||||
await mav.send('COMMAND_LONG', { | ||||||||
target_system: 1, | ||||||||
target_component: 0, | ||||||||
command: 'MAV_CMD_DO_SET_MODE', | ||||||||
confirmation, | ||||||||
param1: 0, | ||||||||
param2: modeNumber | ||||||||
}); | ||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ beforeAll(async () => { | |
await planeSitl.start(); | ||
|
||
planeIp = (await planeSitl.inspect()).NetworkSettings.IPAddress; | ||
|
||
}, 10000); | ||
|
||
// Stop the plane-sitl container. | ||
|
@@ -193,6 +194,7 @@ test('get overview telemetry', async () => { | |
|
||
expect(res.status).toEqual(200); | ||
expect(res.body.pos.lat).toBeTruthy(); | ||
expect(res.body.mode.name).toEqual('MANUAL'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need test coverage for setting the mode |
||
}); | ||
|
||
// Take down the service once the other service tests are done. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what did they do to mode 9... o_o