Skip to content
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

Simplify logic for external position information. #408

Merged
merged 1 commit into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/modules/interface/crtp_localization_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ typedef enum
// Set up the callback for the CRTP_PORT_LOCALIZATION
void locSrvInit(void);

// Get the current position from the cache
bool getExtPosition(state_t *state);

// Send range in float. After 5 ranges it will send the packet.
void locSrvSendRangeFloat(uint8_t id, float range);

Expand Down
55 changes: 15 additions & 40 deletions src/modules/src/crtp_localization_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,12 @@ typedef struct {
int16_t z; // mm
} __attribute__((packed)) extPositionPackedItem;

/**
* Position data cache
*/
typedef struct
{
struct CrtpExtPosition targetVal[2];
bool activeSide;
uint32_t timestamp; // FreeRTOS ticks
} ExtPositionCache;

// Struct for logging position information
static positionMeasurement_t ext_pos;
static ExtPositionCache crtpExtPosCache;
static CRTPPacket pkRange;
static uint8_t rangeIndex;
static bool enableRangeStreamFloat = false;
static float extPosStdDev = 0.01;
static bool isInit = false;
static uint8_t my_id;

Expand Down Expand Up @@ -125,9 +115,13 @@ static void locSrvCrtpCB(CRTPPacket* pk)

static void extPositionHandler(CRTPPacket* pk)
{
crtpExtPosCache.targetVal[!crtpExtPosCache.activeSide] = *((struct CrtpExtPosition*)pk->data);
crtpExtPosCache.activeSide = !crtpExtPosCache.activeSide;
crtpExtPosCache.timestamp = xTaskGetTickCount();
const struct CrtpExtPosition* data = (const struct CrtpExtPosition*)pk;

ext_pos.x = data->x;
ext_pos.y = data->y;
ext_pos.z = data->z;
ext_pos.stdDev = extPosStdDev;
estimatorEnqueuePosition(&ext_pos);
}

static void genericLocHandle(CRTPPacket* pk)
Expand Down Expand Up @@ -156,37 +150,17 @@ static void extPositionPackedHandler(CRTPPacket* pk)
for (uint8_t i = 0; i < numItems; ++i) {
const extPositionPackedItem* item = (const extPositionPackedItem*)&pk->data[i * sizeof(extPositionPackedItem)];
if (item->id == my_id) {
struct CrtpExtPosition position;
position.x = item->x / 1000.0f;
position.y = item->y / 1000.0f;
position.z = item->z / 1000.0f;

crtpExtPosCache.targetVal[!crtpExtPosCache.activeSide] = position;
crtpExtPosCache.activeSide = !crtpExtPosCache.activeSide;
crtpExtPosCache.timestamp = xTaskGetTickCount();
ext_pos.x = item->x / 1000.0f;
ext_pos.y = item->y / 1000.0f;
ext_pos.z = item->z / 1000.0f;
ext_pos.stdDev = extPosStdDev;
estimatorEnqueuePosition(&ext_pos);

break;
}
}
}

bool getExtPosition(state_t *state)
{
// Only use position information if it's valid, recent, and if the kalman filter is enabled
if (getStateEstimator() == kalmanEstimator &&
(xTaskGetTickCount() - crtpExtPosCache.timestamp) < M2T(5)) {
// Get the updated position from the mocap
ext_pos.x = crtpExtPosCache.targetVal[crtpExtPosCache.activeSide].x;
ext_pos.y = crtpExtPosCache.targetVal[crtpExtPosCache.activeSide].y;
ext_pos.z = crtpExtPosCache.targetVal[crtpExtPosCache.activeSide].z;
ext_pos.stdDev = 0.01;
estimatorEnqueuePosition(&ext_pos);

return true;
}
return false;
}

void locSrvSendPacket(locsrv_t type, uint8_t *data, uint8_t length)
{
CRTPPacket pk;
Expand Down Expand Up @@ -230,5 +204,6 @@ LOG_GROUP_START(ext_pos)
LOG_GROUP_STOP(ext_pos)

PARAM_GROUP_START(locSrv)
PARAM_ADD(PARAM_UINT8, enRangeStreamFP32, &enableRangeStreamFloat)
PARAM_ADD(PARAM_UINT8, enRangeStreamFP32, &enableRangeStreamFloat)
PARAM_ADD(PARAM_FLOAT, extPosStdDev, &extPosStdDev)
PARAM_GROUP_STOP(locSrv)
1 change: 0 additions & 1 deletion src/modules/src/stabilizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ static void stabilizerTask(void* param)
controllerType = getControllerType();
}

getExtPosition(&state);
stateEstimator(&state, &sensorData, &control, tick);

commanderGetSetpoint(&setpoint, &state);
Expand Down