remove measure_base_pwm; add comments

This commit is contained in:
gab-k committed 2024-01-09 21:30:32 +01:00
1 parent c1564feb4b
commit d417235df5
2 files changed
+109 -79

No files matched your search

+9 -51
View File
@@ -13,7 +13,6 @@ uint16_t level_table[32] = {0};
absolute_time_t falling_edge_time, rising_edge_time;
// Function returns average of values deviating less than twice the standard deviation from original average
float two_std_dev(const float arr[], const uint32_t length) {
// Calculate arithmetic average
float sum = 0;
@@ -91,8 +90,6 @@ void adc_offset_adjustment(const uint32_t n) {
}
// Confirmation after writing or verifying a CV
// Works by creating a spike in current consumption, this is detected by the command station
void acknowledge() {
const uint16_t max_lvl = _125M / (CV_ARRAY_FLASH[8] * 100 + 10000);
pwm_set_gpio_level(MOTOR_FWD_PIN, max_lvl);
@@ -104,8 +101,8 @@ void acknowledge() {
}
// When bit matches sent byte -> acknowledge()
void verify_cv_bit(const uint16_t cv_address, const bool bit_val, const uint8_t bit_pos) {
// Check for matching bit, when found call acknowledge()
const uint8_t mask = 0b00000001;
const bool res = ((CV_ARRAY_FLASH[cv_address] >> bit_pos) & mask) == bit_val;
if (res) {
@@ -114,17 +111,17 @@ void verify_cv_bit(const uint16_t cv_address, const bool bit_val, const uint8_t
}
// When byte matches sent byte -> acknowledge()
void verify_cv_byte(const uint16_t cv_address, const uint8_t cv_data) {
// Check for matching byte, when found call acknowledge()
if (CV_ARRAY_FLASH[cv_address] == cv_data) acknowledge();
}
// CV writing function first erases necessary amount of blocks in flash and then rewrites to flash
void regular_cv_write(const uint16_t cv_index, const uint8_t cv_data) {
void write_cv_byte(uint16_t cv_address, uint8_t cv_data) {
uint8_t CV_ARRAY_TEMP[CV_ARRAY_SIZE];
memcpy(CV_ARRAY_TEMP, CV_ARRAY_FLASH, sizeof(CV_ARRAY_TEMP));
CV_ARRAY_TEMP[cv_index] = cv_data;
CV_ARRAY_TEMP[cv_address] = cv_data;
// First erase necessary amount of blocks in flash and then rewrite to flash
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_TEMP, FLASH_PAGE_SIZE * 2);
acknowledge();
@@ -140,7 +137,7 @@ void write_cv_handler(const uint16_t cv_index, const uint8_t cv_data) {
break;
}
else {
regular_cv_write(cv_index, cv_data);
write_cv_byte(cv_index, cv_data);
}
break;
case 6: //CV_7
@@ -166,13 +163,13 @@ void write_cv_handler(const uint16_t cv_index, const uint8_t cv_data) {
if ((cv_data < 192 || cv_data > 231) || (CV_ARRAY_FLASH[17] == 0 && cv_data == 192)) {
break;
}
regular_cv_write(cv_index, cv_data);
write_cv_byte(cv_index, cv_data);
break;
case 17: //CV_18
if(CV_ARRAY_FLASH[16] == 192 && cv_data == 0){
break;
}
regular_cv_write(cv_index, cv_data);
write_cv_byte(cv_index, cv_data);
break;
// CV_31 & CV_32 Index high byte isn't implemented and shall not be set
case 30: //CV_31
@@ -180,7 +177,7 @@ void write_cv_handler(const uint16_t cv_index, const uint8_t cv_data) {
break;
// Regular CV write is carried out
default:
regular_cv_write(cv_index, cv_data);
write_cv_byte(cv_index, cv_data);
}
}
@@ -238,7 +235,6 @@ void program_mode(const uint8_t number_of_bytes, const uint8_t *const byte_array
}
// Set outputs according to configuration (PWM on/off, PWM duty cycle/level, ...)
void set_outputs(const uint32_t functions_to_set_bitmask) {
// Get outputs with pwm enabled and preset outputs_to_set_PWM variable with resulting GPIO Bitmask
const uint32_t PWM_enabled_outputs = get_32bit_CV(111);
@@ -538,44 +534,6 @@ void init_outputs() {
}
}
uint16_t measure_base_pwm(const bool direction, const uint8_t iterations) {
uint gpio;
if (direction) gpio = MOTOR_FWD_PIN;
else gpio = MOTOR_REV_PIN;
float level_arr[iterations];
for (int i = 0; i < iterations; ++i) {
const uint16_t max_level = _125M / (CV_ARRAY_FLASH[8] * 100 + 10000);
uint16_t level = max_level / 20;
LOG(3, "iteration %d: maxlevel %d level %d\n", i, max_level, level);
float measurement;
do {
pwm_set_gpio_level(gpio, level);
busy_wait_ms(30);
level += max_level / 500;
measurement = measure(CV_ARRAY_FLASH[60],
CV_ARRAY_FLASH[61],
CV_ARRAY_FLASH[62],
CV_ARRAY_FLASH[63],
direction);
LOG(3, "level %d measurement %f loop-cond: %f\n", level, measurement, measurement - (float) CV_ARRAY_FLASH[171]);
if (level > max_level) {
// Abort measurement and write default value of 0 to flash
LOG(1, "measure_base_pwm: abort measurement and return 0\n");
return 0;
}
} while ((measurement - (float) CV_ARRAY_FLASH[171]) < 5.0f);
LOG(3, "level_arr[%d] = %d\n", i, level);
level_arr[i] = (float) level;
busy_wait_ms(100);
}
// Find and return overall average discarding outliers in measurement - multiply with 0.9
const float retVal = 0.9f * two_std_dev(level_arr, 5);
LOG(1, "measure_base_pwm: %d(%f)\n", (uint16_t) retVal, retVal);
return (uint16_t) retVal;
}
void cv_setup_check() {
// Check for flash factory setting and set CV_FLASH_ARRAY to default values when factory condition ("0xFF") is found.
if (CV_ARRAY_FLASH[64] == 0xFF) {
+100 -28
View File
@@ -5,8 +5,10 @@
*
*/
#pragma once
#include "shared.h"
#include "CV.h"
#define SIZE_BYTE_ARRAY 5
#define MESSAGE_3_BYTES 0b11111111110000000000000000000000000001
#define MESSAGE_MASK_3_BYTES 0b11111111111000000001000000001000000001
@@ -14,6 +16,18 @@
#define MESSAGE_MASK_4_BYTES 0b11111111111000000001000000001000000001000000001
#define MESSAGE_5_BYTES 0b11111111110000000000000000000000000000000000000000000001
#define MESSAGE_MASK_5_BYTES 0b11111111111000000001000000001000000001000000001000000001
/*!
* \brief Bitmasks used for clearing bits corresponding to function blocks
*
* [0] = F0-F4
* [1] = F5-F8
* [2] = F9-F12
* [3] = F13-F20
* [4] = F21-F28
* [5] = F29-F31
*
*/
const uint32_t clr_bit_arr[6] = {
0b11111111111111111111111111100000, // Clear Bits 0-4 (F0-F4)
0b11111111111111111111111000011111, // Clear Bits 5-8 (F5-F8)
@@ -23,19 +37,51 @@ const uint32_t clr_bit_arr[6] = {
0b00011111111111111111111111111111, // Clear Bits 29-31 (F29-F31)
};
/*!
* \brief Function returns average of values deviating less than twice the standard deviation from original average.
*
* \param arr input array
* \param length amount of elements in input array
* \return average value of values deviating less than 2*standard_dev.
*/
float two_std_dev(const float arr[], uint32_t length);
void adc_offset_adjustment(uint32_t length);
/*!
* \brief Measures ADC offset and saves the offset value to flash memory.
* \param n Amount of measurements to be done
*/
void adc_offset_adjustment(uint32_t n);
/*!
* \brief Function for acknowledging a CV write or CV verify instruction
*
* Acknowledgement works by creating a spike in current consumption, normally realized by running the motor in alternating directions.
* This current increase is detected by the command station.
* Refers to NMRA S-9.2.3 Section D and RCN-216 Section 3
*/
void acknowledge();
void verify_cv_bit(uint16_t cv_address,bool bit_val, uint8_t bit_pos);
/*!
* \brief Function for verifying a single bit of a CV
* \param cv_address CV Address
* \param bit_val Value of bit to be verified
* \param bit_pos Position of bit to be verified
*/
void verify_cv_bit(uint16_t cv_address, bool bit_val, uint8_t bit_pos);
/*!
* \brief Function for verifying a CV byte
* \param cv_address CV Address
* \param cv_data Byte to be verified
*/
void verify_cv_byte(uint16_t cv_address, uint8_t cv_data);
void regular_cv_write(uint16_t cv_index, uint8_t cv_data);
//void write_cv_handler(uint16_t cv_index, uint8_t cv_data);
#define ProgramInstrVerifyBit 0b10
#define ProgramInstrVerifyByte 0b01
#define ProgramInstrWriteByte 0b11
#define ProgramCmd(instr, adrmsbits) ((instr<<2) | (adrmsbits))
/*!
* \brief Function for writing a CV byte
* \param cv_address
* \param cv_data
*/
void write_cv_byte(uint16_t cv_address, uint8_t cv_data);
/*!
* \brief CV Programming mode function
@@ -55,17 +101,58 @@ void regular_cv_write(uint16_t cv_index, uint8_t cv_data);
* \param byte_array Pointer to byte_array[]
*/
void program_mode(uint8_t number_of_bytes, const uint8_t byte_array[]);
/*!
* \brief Set outputs according to function mapping in CV257 to CV512
*
* This function maps the functions and direction to the mapped outputs configured in CV257 to CV512.
* Also enables PWM when enabled according to CV112 to CV115.
*
* \param functions_to_set_bitmask Function bitmask F0 is bit0, F1 is bit1, ...
*/
void set_outputs(uint32_t functions_to_set_bitmask);
void update_active_functions(uint32_t new_function_bitmask, uint8_t clr_bit_ind, bool direction_change);
bool error_detection(int8_t number_of_bytes, const uint8_t * byte_array);
bool error_detection(int8_t number_of_bytes, const uint8_t *byte_array);
bool is_long_address(uint8_t number_of_bytes, const uint8_t byte_array[]);
bool address_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]);
void instruction_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]);
bool address_evaluation(uint8_t number_of_bytes, const uint8_t byte_array[]);
void instruction_evaluation(uint8_t number_of_bytes, const uint8_t byte_array[]);
bool reset_message_check(uint8_t number_of_bytes, const uint8_t *const byte_array);
int8_t verify_dcc_message();
void bits_to_byte_array(int8_t number_of_bytes,uint8_t byte_array[]);
void bits_to_byte_array(int8_t number_of_bytes, uint8_t byte_array[]);
void evaluate_message();
/*!
* \brief Track signal rising edge interrupt callback function
*
* Saves timer counter value using get_absolute_time(), then disables rising edge IRQ and enables falling edge IRQ.
* Parameters are unused.
*
* \param gpio Unused parameter
* \param events Unused parameter
*/
void track_signal_rise(unsigned int gpio, long unsigned int events);
/*!
* \brief Track signal falling edge interrupt callback function
*
* Saves timer counter value using get_absolute_time(), then calculates time difference between rising and falling edge.
* When the time difference is greater than 87us, this bit is interpreted as a logical 0.
* Refers to NMRA S-9.1 and RCN-210 standards
* Puts the latest bit value into buffer, then evaluates complete buffer using evaluate_message().
* Parameters are unused.
*
* \param gpio Unused parameter
* \param events Unused parameter
*/
void track_signal_fall(unsigned int gpio, long unsigned int events);
/*!
@@ -77,21 +164,6 @@ void track_signal_fall(unsigned int gpio, long unsigned int events);
*/
void init_outputs();
/*!
* \brief Measures base pwm duty cycles at which the motor barely starts moving.
*
* Function starts with 1/20 of the maximum duty cycle then increases the duty cycle by 1/500 of the maximum until
* the motor movement can be measured. This is done for the specified amount of iterations.
* The array with raw measurements values is then passed to two_std_dev() in order to detect outliers and
* calculate the average.An additional correction factor is applied and the overall measurement result is then
* returned. In case no motor movement could be measured a value of 0 is returned and normally written to flash,
* indicating a pending calibration next time the decoder starts, rerunning the calibration.
* \param direction Specifies direction which is to be measured
* \param iterations Specifies the amount of iterations the measurement is to be repeated and averaged out over
* \return Measurement
*/
uint16_t measure_base_pwm(bool direction, uint8_t iterations);
/*!
* \brief Checks various CV values in order to detect factory condition or pending calibrations
*