Skip to content

Commit

Permalink
calibrate value before send payload.
Browse files Browse the repository at this point in the history
  • Loading branch information
Apaisal committed Dec 4, 2023
1 parent ec48121 commit 4dab890
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 51 deletions.
15 changes: 8 additions & 7 deletions src/apps/LoRaMac/encoder/SKiM980A/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,12 @@ static void PrepareTxFrame( uint8_t port )
potiPercentage = BoardGetPotiLevel( );
vdd = BoardGetBatteryLevel( );
AppDataSizeBackup = AppDataSize = 11;
int32_t dat = flow.fwd_cnt + ( flow.fwd_cnt / 159 );
#if 1
AppDataBuffer[0] = (flow.fwd_cnt >> 24) & 0xff;
AppDataBuffer[1] = (flow.fwd_cnt >> 16) & 0xff;
AppDataBuffer[2] = (flow.fwd_cnt >> 8) & 0xff;
AppDataBuffer[3] = (flow.fwd_cnt) & 0xff;
AppDataBuffer[0] = (dat >> 24) & 0xff;
AppDataBuffer[1] = (dat >> 16) & 0xff;
AppDataBuffer[2] = (dat >> 8) & 0xff;
AppDataBuffer[3] = (dat) & 0xff;
AppDataBuffer[4] = (flow.position >> 8) & 0xff;
AppDataBuffer[5] = (flow.position) & 0xff;
#else
Expand Down Expand Up @@ -609,8 +610,8 @@ static void OnLed4Toggle( void )
static void OnLed3Toggle( void )
{
// Switch LED 3 Toggle
// GpioToggle( &Led3 );
GpioWrite( &Led3, 1 );
GpioToggle( &Led3 );
//GpioWrite( &Led3, 1 );
TimerStart( &Led3Timer );
IsMacProcessPending = 1;
}
Expand Down Expand Up @@ -1312,7 +1313,7 @@ int main( void )
TimerSetValue( &Led2Timer, 25 );

TimerInit( &Led1Timer, OnLed1TimerEvent );
TimerSetValue( &Led1Timer, 1 );
TimerSetValue( &Led1Timer, 10 );
TimerStart(&Led1Timer);

// TimerInit( &WdtTimer, OnWdtTimerEvent );
Expand Down
100 changes: 56 additions & 44 deletions src/boards/SKiM980A/encoder-board.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
* \author Anol Paisal ( EmOne )
*/
#include "stm32l1xx.h"
#include "stm32l1xx_hal_tim.h"
#include "utilities.h"
#include "board.h"
#include "gpio.h"
#include "encoder-board.h"
#include "math.h"
#include "lpm-board.h"
#include "stdlib.h"
// #include "timer.h"

// #define USE_GPIO
Expand All @@ -50,8 +52,8 @@ extern Gpio_t Led3;
static uint8_t EncoderInitialized = 0;
uint32_t uwDirection = 0;
int16_t round_msb = 0;
int32_t max_lsb = 79;
int16_t counter = 0, last_counter = 0;
int32_t max_lsb = 160;
int32_t counter = 0, last_counter = 0;
int position = 0, speed = 0;
static int indx = 0;

Expand All @@ -73,27 +75,37 @@ static void update_encoder(flow_t *enc, TIM_HandleTypeDef *htim) {
if (__HAL_TIM_IS_TIM_COUNTING_DOWN(htim)) {
enc->rate = -enc->last
- (__HAL_TIM_GET_AUTORELOAD(htim) - temp_counter);
enc->status &= ~DIR_FLAG;
} else {
enc->rate = temp_counter - enc->last;
enc->status |= DIR_FLAG;
}
} else {
if (__HAL_TIM_IS_TIM_COUNTING_DOWN(htim)) {
enc->rate = temp_counter - enc->last;
enc->status &= ~DIR_FLAG;
} else {
enc->rate = temp_counter
+ (__HAL_TIM_GET_AUTORELOAD(htim) - enc->last);
enc->status |= DIR_FLAG;
}
}
}

if(abs(enc->rate) > 120)
{
enc->rate = enc->last_rate;
}

if (enc->rate < 0)
{
enc->rev_cnt += enc->rate;
}

counter = temp_counter;
enc->fwd_cnt += enc->rate;
enc->last = temp_counter;
enc->position = temp_counter * 360.0f / 80.0f;
enc->last_rate = enc->rate;
enc->position = (temp_counter) * 360.0f / max_lsb;
}

void EncoderInit(Encoder_t *obj, EncoderId_t timId, PinNames pulse, PinNames dir, PinNames tampering, PinNames alarm, PinNames en)
Expand Down Expand Up @@ -132,8 +144,8 @@ void EncoderInit(Encoder_t *obj, EncoderId_t timId, PinNames pulse, PinNames dir
TimHandle.Instance = TIM2;
TimHandle.Init.Prescaler = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
TimHandle.Init.Period = max_lsb;
TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV4;
TimHandle.Init.Period = max_lsb - 1;
TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
sConfig.EncoderMode = TIM_ENCODERMODE_TI12;
sConfig.IC1Polarity = TIM_ICPOLARITY_RISING;
sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI;
Expand Down Expand Up @@ -206,7 +218,7 @@ void OnTamperingIrq( void* context )
flow.status |= TAMPER_FLAG;
printf( "\r\n###### ===== Tampering attached ==== ######\r\n\r\n" );
#ifndef USE_GPIO
HAL_TIM_Encoder_Start(&TimHandle, TIM_CHANNEL_ALL);
HAL_TIM_Encoder_Start_IT(&TimHandle, TIM_CHANNEL_ALL);
LpmSetStopMode(LPM_APPLI_ID, LPM_DISABLE);
if (Encoder.OnShowPulseDetect != NULL)
{
Expand All @@ -217,7 +229,7 @@ void OnTamperingIrq( void* context )
flow.status &= ~TAMPER_FLAG;
printf( "\r\n###### ===== Tampering released ==== ######\r\n\r\n" );
#ifndef USE_GPIO
HAL_TIM_Encoder_Stop(&TimHandle, TIM_CHANNEL_ALL);
HAL_TIM_Encoder_Stop_IT(&TimHandle, TIM_CHANNEL_ALL);
LpmSetStopMode(LPM_APPLI_ID, LPM_ENABLE);
if (Encoder.OnArcRelease != NULL)
{
Expand Down Expand Up @@ -295,34 +307,34 @@ void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
CRITICAL_SECTION_BEGIN();
if (htim->Instance == TIM2) {
// uint32_t current = 0;
#if ( USE_GPIO )
if (GpioRead(&Encoder.Direction) != GPIO_PIN_RESET)
#else
uwDirection = __HAL_TIM_DIRECTION_STATUS(&TimHandle);
counter = (int32_t) __HAL_TIM_GET_COUNTER(htim);
if (uwDirection != GPIO_PIN_RESET)
#endif /* USE_GPIO */
{
flow.status |= DIR_FLAG;
// printf( "\r\nForward cnt : %ld\r\n\r\n", flow.fwd_cnt);
if (counter == 0 && last_counter == max_lsb ) {
++round_msb;
}
} else {
flow.status &= ~DIR_FLAG;
// printf( "\r\nBackward cnt : %ld\r\n\r\n", flow.rev_cnt);
if (counter == max_lsb && last_counter == 0) {
--round_msb;
}
}
// flow.fwd_cnt = (round_msb << 6 | counter);
// last_counter = counter;
// flow.position = counter / max_lsb;
// current = HAL_GetTick(); //HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
// float delta = (float) abs(current - flow.last);
// flow.rate = (uint32_t) ((1.0f / delta) * 1000.0f);
// flow.last = HAL_GetTick();
// __NOP();
// #if ( USE_GPIO )
// if (GpioRead(&Encoder.Direction) != GPIO_PIN_RESET)
// #else
// // uwDirection = __HAL_TIM_DIRECTION_STATUS(&TimHandle);
// // counter = (int32_t) __HAL_TIM_GET_COUNTER(htim);
// // if (uwDirection != GPIO_PIN_RESET)
// #endif /* USE_GPIO */
// {
// flow.status |= DIR_FLAG;
// // printf( "\r\nForward cnt : %ld\r\n\r\n", flow.fwd_cnt);
// if (counter == 0 && last_counter == max_lsb ) {
// ++round_msb;
// }
// } else {
// flow.status &= ~DIR_FLAG;
// // printf( "\r\nBackward cnt : %ld\r\n\r\n", flow.rev_cnt);
// if (counter == max_lsb && last_counter == 0) {
// --round_msb;
// }
// }
// // flow.fwd_cnt = (round_msb << 6 | counter);
// // last_counter = counter;
// // flow.position = counter / max_lsb;
// // current = HAL_GetTick(); //HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
// // float delta = (float) abs(current - flow.last);
// // flow.rate = (uint32_t) ((1.0f / delta) * 1000.0f);
// // flow.last = HAL_GetTick();
// // __NOP();
}

if (Encoder.OnShowPulseDetect != NULL)
Expand All @@ -345,17 +357,17 @@ void OnPeriodElapsedCallback( void ) {
// indx++;
// if (indx == 100)
// {
uwDirection = __HAL_TIM_DIRECTION_STATUS(&TimHandle);
if (uwDirection != GPIO_PIN_RESET)
{
flow.status |= DIR_FLAG;
// uwDirection = __HAL_TIM_DIRECTION_STATUS(&TimHandle);
// if (uwDirection != GPIO_PIN_RESET)
// {
// flow.status |= DIR_FLAG;

} else {
flow.status &= ~DIR_FLAG;
// } else {
// flow.status &= ~DIR_FLAG;

}
// }
update_encoder(&flow, &TimHandle);
counter = flow.fwd_cnt;

position = flow.position;
speed = flow.rate;
// indx = 0;
Expand Down
1 change: 1 addition & 0 deletions src/system/encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef struct {
int32_t fwd_cnt;
int32_t rev_cnt;
int32_t rate; //Speed degree per second
int32_t last_rate;
uint8_t status; //[0:tampering, 1:alarm, 2:dir]
volatile uint16_t adc_lvl[3];
uint32_t adc_acc;
Expand Down

0 comments on commit 4dab890

Please sign in to comment.