src/modules/land_detector/land_detector_main.cpp
/**
* Start new task, fails if it is already running. Returns OK if successful
**/
Start new task, fails if it is already running. Returns OK if successful
Start new task fails if it is already running . Returns OK if successful
static int land_detector_start(const char *mode)
{
	if (land_detector_task != nullptr || _landDetectorTaskID != -1) {
		errx(1, "already running");
		return -1;
	}
	if (!strcmp(mode, "fixedwing")) {
		land_detector_task = new FixedwingLandDetector();
	} else if (!strcmp(mode, "multicopter")) {
		land_detector_task = new MulticopterLandDetector();
	} else {
		errx(1, "[mode] must be either 'fixedwing' or 'multicopter'");
		return -1;
	}
	if (land_detector_task == nullptr) {
		errx(1, "alloc failed");
		return -1;
	}
	_landDetectorTaskID = task_spawn_cmd("land_detector",
					     SCHED_DEFAULT,
					     SCHED_PRIORITY_DEFAULT,
					     1200,
					     (main_t)&land_detector_deamon_thread,
					     nullptr);
	if (_landDetectorTaskID < 0) {
		errx(1, "task start failed: %d", -errno);
		return -1;
	}
	const uint32_t timeout = hrt_absolute_time() + 5000000; 
	while (!land_detector_task->isRunning()) {
		usleep(50000);
		printf(".");
		fflush(stdout);
		if (hrt_absolute_time() > timeout) {
			err(1, "start failed - timeout");
			land_detector_stop();
			exit(1);
		}
	}
	printf("\n");
	strncpy(_currentMode, mode, 12);
	exit(0);
	return 0;
}
static int land_detector_start ( const char * mode ) { if ( land_detector_task != nullptr || _landDetectorTaskID != -1 ) { errx ( 1 , " " ) ; return -1 ; } if ( ! strcmp ( mode , " " ) ) { land_detector_task = new FixedwingLandDetector ( ) ; } else if ( ! strcmp ( mode , " " ) ) { land_detector_task = new MulticopterLandDetector ( ) ; } else { errx ( 1 , " " ) ; return -1 ; } if ( land_detector_task == nullptr ) { errx ( 1 , " " ) ; return -1 ; } _landDetectorTaskID = task_spawn_cmd ( " " , SCHED_DEFAULT , SCHED_PRIORITY_DEFAULT , 1200 , ( main_t ) & land_detector_deamon_thread , nullptr ) ; if ( _landDetectorTaskID < 0 ) { errx ( 1 , " " , - errno ) ; return -1 ; } const uint32_t timeout = hrt_absolute_time ( ) + 5000000 ; while ( ! land_detector_task -> isRunning ( ) ) { usleep ( 50000 ) ; printf ( " " ) ; fflush ( stdout ) ; if ( hrt_absolute_time ( ) > timeout ) { err ( 1 , " " ) ; land_detector_stop ( ) ; exit ( 1 ) ; } } printf ( " \n " ) ; strncpy ( _currentMode , mode , 12 ) ; exit ( 0 ) ; return 0 ; }
Start new task, fails if it is already running.
Start new task fails if it is already running .
//Allocate memory //Check if alloc worked //Start new thread task /* avoid memory fragmentation by not exiting start handler until the task has fully started */ //5 second timeout //Remember current active mode