@ -1,14 +1,18 @@
# include "MovesenseExerciseService.h"
# include "app-resources/resources.h"
# include "common/core/debug.h"
# include "meas_acc/resources.h"
# include "whiteboard/builtinTypes/UnknownStructure.h"
# include "ui_ind/resources.h"
# include <float.h>
# include <math.h>
const char * const MovesenseExerciseService : : LAUNCHABLE_NAME = " SampleA " ;
const char * const MovesenseExerciseService : : LAUNCHABLE_NAME = " MExercise " ;
# define SAMPLE_RATE 13
const size_t BLINK_PERIOD_MS = 1000 ;
static const whiteboard : : ExecutionContextId sExecutionContextId =
WB_RES : : LOCAL : : EXERCISE_SUMVECTOR_DENES : : EXECUTION_CONTEXT ;
@ -16,11 +20,27 @@ static const whiteboard::LocalResourceId sProviderResources[] = {
WB_RES : : LOCAL : : EXERCISE_SUMVECTOR_DENES : : LID ,
} ;
namespace {
whiteboard : : FloatVector3D SumVectorAverage ( const std : : vector < whiteboard : : FloatVector3D > & v )
{
whiteboard : : FloatVector3D sum ;
for ( size_t i = 0 ; i < v . size ( ) ; i + + )
{
whiteboard : : FloatVector3D fv = v [ i ] ;
sum + = fv ;
}
return sum / ( float ) v . size ( ) ;
}
}
MovesenseExerciseService : : MovesenseExerciseService ( )
: ResourceClient ( WBDEBUG_NAME ( __FUNCTION__ ) , sExecutionContextId ) ,
ResourceProvider ( WBDEBUG_NAME ( __FUNCTION__ ) , sExecutionContextId ) ,
LaunchableModule ( LAUNCHABLE_NAME , sExecutionContextId ) ,
isRunning ( false )
isRunning ( false ) ,
mLastTimeStamp ( 0 ) ,
mTimer ( whiteboard : : ID_INVALID_TIMER ) ,
mMaxAccelerationData ( SAMPLE_RATE )
{
}
@ -50,9 +70,21 @@ bool MovesenseExerciseService::startModule()
{
mModuleState = WB_RES : : ModuleStateValues : : STARTED ;
// Start LED timer. true = trigger repeatedly
mTimer = ResourceClient : : startTimer ( BLINK_PERIOD_MS , true ) ;
return true ;
}
void MovesenseExerciseService : : stopModule ( )
{
mModuleState = WB_RES : : ModuleStateValues : : STOPPED ;
// Stop LED timer
ResourceClient : : stopTimer ( mTimer ) ;
mTimer = whiteboard : : ID_INVALID_TIMER ;
}
void MovesenseExerciseService : : onUnsubscribeResult ( whiteboard : : RequestId requestId ,
whiteboard : : ResourceId resourceId ,
whiteboard : : Result resultCode ,
@ -86,10 +118,6 @@ whiteboard::Result MovesenseExerciseService::startRunning(whiteboard::RequestId&
DEBUGLOG ( " MovesenseExerciseService::startRunning() " ) ;
// Reset max acceleration members
mMaxAccelerationSq = FLT_MIN ;
mSamplesIncluded = 0 ;
wb : : Result result = getResource ( " Meas/Acc/13 " , mMeasAccResourceId ) ;
if ( ! wb : : RETURN_OKC ( result ) )
{
@ -157,36 +185,11 @@ void MovesenseExerciseService::onNotify(whiteboard::ResourceId resourceId, const
const whiteboard : : Array < whiteboard : : FloatVector3D > & arrayData = linearAccelerationValue . arrayAcc ;
uint32_t relativeTime = linearAccelerationValue . timestamp ;
mLastTimeStamp = linearAccelerationValue . timestamp ;
for ( size_t i = 0 ; i < arrayData . size ( ) ; i + + )
{
mSamplesIncluded + + ;
whiteboard : : FloatVector3D accValue = arrayData [ i ] ;
float accelerationSq = accValue . mX * accValue . mX +
accValue . mY * accValue . mY +
accValue . mZ * accValue . mZ ;
if ( mMaxAccelerationSq < accelerationSq )
mMaxAccelerationSq = accelerationSq ;
}
// 13Hz 5sec
if ( mSamplesIncluded > 5 * 13 )
{
// Reset counter and notify our subscribers
WB_RES : : SampleDataValue sampleDataValue ;
sampleDataValue . relativeTime = relativeTime ;
sampleDataValue . value = sqrtf ( mMaxAccelerationSq ) ;
// Reset members
mSamplesIncluded = 0 ;
mMaxAccelerationSq = FLT_MIN ;
// and update our WB resource. This causes notification to be fired to our subscribers
updateResource ( WB_RES : : LOCAL : : EXERCISE_SUMVECTOR_DENES ( ) ,
ResponseOptions : : Empty , sampleDataValue ) ;
mMaxAccelerationData . push_back ( arrayData [ i ] ) ;
}
}
break ;
@ -250,3 +253,30 @@ void MovesenseExerciseService::onClientUnavailable(whiteboard::ClientId clientId
DEBUGLOG ( " MovesenseExerciseService::onClientUnavailable() " ) ;
stopRunning ( ) ;
}
void MovesenseExerciseService : : onTimer ( whiteboard : : TimerId timerId )
{
if ( timerId ! = mTimer )
{
return ;
}
uint16_t indicationType = 2 ; // SHORT_VISUAL_INDICATION, defined in ui/ind.yaml
// Make PUT request to trigger led blink
asyncPut ( WB_RES : : LOCAL : : UI_IND_VISUAL : : ID , AsyncRequestOptions : : Empty , indicationType ) ;
// Notify our subscribers
/// @todo The timestamp is the last measurement's timestamp and not the current time.
WB_RES : : SampleDataValue sampleDataValue ;
sampleDataValue . relativeTime = mLastTimeStamp ;
sampleDataValue . value = SumVectorAverage ( mMaxAccelerationData ) ;
// Reset members
mLastTimeStamp = 0 ;
mMaxAccelerationData . clear ( ) ;
// and update our WB resource. This causes notification to be fired to our subscribers
updateResource ( WB_RES : : LOCAL : : EXERCISE_SUMVECTOR_DENES ( ) ,
ResponseOptions : : Empty , sampleDataValue ) ;
}