Implemented Basic Programming features, changed speed control (speed_helper function with timed interrupt)
This commit is contained in:
1 parent
df9a1848e4
commit
5a045b5fcf
3 files changed
+428
-344
No files matched your search
+140
-72
@@ -1,4 +1,5 @@
|
||||
#include <stdio.h>
|
||||
#include "string.h"
|
||||
#include "pico/stdlib.h"
|
||||
#include "pico/multicore.h"
|
||||
#include "hardware/irq.h"
|
||||
@@ -8,23 +9,23 @@
|
||||
#include "CV.h"
|
||||
#include "multicore.h"
|
||||
const uint8_t *CV_ARRAY_FLASH = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET);
|
||||
uint64_t last_bits = 0;
|
||||
bool active_functions[SIZE_ACTIVE_FUNCTIONS] = {false};
|
||||
uint32_t target_speed_step = 128;
|
||||
uint64_t last_bits = 0;
|
||||
bool target_direction = true;
|
||||
int error = 0;
|
||||
int e_old = 0;
|
||||
int e_sum = 0;
|
||||
float t_s = 0.01f;
|
||||
uint target_speed_step = 128;
|
||||
uint current_v_emf_target = 0;
|
||||
uint measurement = 0;
|
||||
bool reset_package_flag = false;
|
||||
int sum_limit_max = 1000;
|
||||
int sum_limit_min = -1000;
|
||||
float k_p = 0.09f;
|
||||
float k_i = 0.075f;
|
||||
float k_d = 0.008f;
|
||||
float output = 0;
|
||||
uint target_v_emf = 0;
|
||||
uint measurement;
|
||||
bool program_mode_flag = false;
|
||||
float t_s = 0.01f;
|
||||
uint measurement_delay_in_us;
|
||||
uint measurement_iterations;
|
||||
uint measurement_period_in_ms;
|
||||
struct repeating_timer speed_helper_timer;
|
||||
|
||||
int8_t check_for_package() //function returns number of bytes if valid bit-pattern is found. Otherwise -1 is returned
|
||||
{
|
||||
@@ -60,23 +61,59 @@ void bits_to_byte_array(int8_t number_of_bytes,uint8_t byte_array[]) {
|
||||
}
|
||||
}
|
||||
|
||||
void interp_speed_step(uint8_t speed_byte){
|
||||
target_speed_step = speed_byte;
|
||||
//Forward
|
||||
if(speed_byte>127){
|
||||
target_v_emf = (speed_byte-129)*32;
|
||||
target_direction = true;
|
||||
void acknowledge(){
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
gpio_put(MOTOR_PWM_REVERSE,0);
|
||||
gpio_put(MOTOR_PWM_FORWARD,1);
|
||||
sleep_ms(1);
|
||||
gpio_put(MOTOR_PWM_FORWARD,0);
|
||||
gpio_put(MOTOR_PWM_REVERSE,1);
|
||||
sleep_ms(1);
|
||||
}
|
||||
//Reverse
|
||||
if(speed_byte<128) {
|
||||
target_v_emf = (speed_byte-1)*32;
|
||||
target_direction = false;
|
||||
}
|
||||
if (speed_byte == 128 || speed_byte==0) target_v_emf = 0;
|
||||
}
|
||||
void verify_cv(uint16_t cv_address,uint8_t cv_data){
|
||||
if (CV_ARRAY_FLASH[cv_address] == cv_data) acknowledge();
|
||||
}
|
||||
|
||||
void program_mode(){
|
||||
void write_cv(uint16_t cv_address,uint8_t cv_data){
|
||||
multicore_reset_core1();
|
||||
uint32_t ints = save_and_disable_interrupts();
|
||||
// CV_n is at index n-1 of the array e.g.: CV_1 -> index = 0
|
||||
uint16_t cv_array_index = cv_address-1;
|
||||
//CV_7 and CV_8 are read-only
|
||||
if (cv_address == 7){
|
||||
}
|
||||
if (cv_address == 8){
|
||||
//Reset all CVs to Default
|
||||
if (cv_data == 8){
|
||||
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
|
||||
flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_DEFAULT, FLASH_PAGE_SIZE * 2);
|
||||
restore_interrupts(ints);
|
||||
acknowledge();
|
||||
}
|
||||
}
|
||||
else{
|
||||
uint8_t CV_ARRAY_TEMP[512] = {0};
|
||||
memcpy(CV_ARRAY_TEMP, CV_ARRAY_FLASH, sizeof(CV_ARRAY_TEMP));
|
||||
CV_ARRAY_TEMP[cv_array_index] = cv_data;
|
||||
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
|
||||
flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY_TEMP, FLASH_PAGE_SIZE * 2);
|
||||
restore_interrupts(ints);
|
||||
acknowledge();
|
||||
}
|
||||
multicore_launch_core1(core1_entry);
|
||||
}
|
||||
|
||||
void program_mode(uint8_t number_of_bytes, const uint8_t byte_array[]){
|
||||
//TODO: Program only on second identical Command/Packet...
|
||||
uint8_t instruction_type_mask = 0b00001100;
|
||||
uint8_t instruction_type = instruction_type_mask&byte_array[number_of_bytes-1];
|
||||
uint8_t cv_address_mask = 0b00000011;
|
||||
uint8_t cv_address_ms_bits = cv_address_mask&byte_array[number_of_bytes-1];
|
||||
uint16_t cv_address = byte_array[number_of_bytes-2]+(cv_address_ms_bits<<8);
|
||||
uint8_t cv_data = byte_array[number_of_bytes-3];
|
||||
if (instruction_type == 0b000000100) verify_cv(cv_address,cv_data);
|
||||
if (instruction_type == 0b000001100) write_cv(cv_address,cv_data);
|
||||
}
|
||||
|
||||
void set_outputs() {
|
||||
@@ -86,10 +123,10 @@ void set_outputs() {
|
||||
for (uint8_t i = 0; i < SIZE_ACTIVE_FUNCTIONS; i++) {
|
||||
if (active_functions[i]) {
|
||||
printf("F%u == 1\n",i);
|
||||
uint8_t func_cv_0 = CV_ARRAY[4 + i * 8 - 4 * target_direction];
|
||||
uint8_t func_cv_1 = CV_ARRAY[5 + i * 8 - 4 * target_direction];
|
||||
uint8_t func_cv_2 = CV_ARRAY[6 + i * 8 - 4 * target_direction];
|
||||
uint8_t func_cv_3 = CV_ARRAY[7 + i * 8 - 4 * target_direction];
|
||||
uint8_t func_cv_0 = CV_ARRAY_FLASH[4 + i * 8 - 4 * target_direction]; //TODO: Index needs to be corrected here...
|
||||
uint8_t func_cv_1 = CV_ARRAY_FLASH[5 + i * 8 - 4 * target_direction];
|
||||
uint8_t func_cv_2 = CV_ARRAY_FLASH[6 + i * 8 - 4 * target_direction];
|
||||
uint8_t func_cv_3 = CV_ARRAY_FLASH[7 + i * 8 - 4 * target_direction];
|
||||
uint32_t func_cv = (func_cv_0) + (func_cv_1 << 8) + (func_cv_2 << 16) + (func_cv_3 << 24);
|
||||
GPIO_to_be_set = (GPIO_to_be_set | func_cv) & filter_forbidden_GPIO;
|
||||
uint32_t mask = 1;
|
||||
@@ -132,11 +169,11 @@ 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[]) {
|
||||
uint16_t read_address;
|
||||
//Enters Programming Mode
|
||||
if (program_mode_flag && byte_array[number_of_bytes - 1]<128 && byte_array[number_of_bytes - 1]>111){
|
||||
program_mode();
|
||||
//Check for address 112-127 and for previous packet == reset package
|
||||
if (reset_package_flag && byte_array[number_of_bytes - 1]<128 && byte_array[number_of_bytes - 1]>111){
|
||||
program_mode(number_of_bytes,byte_array);
|
||||
return false;
|
||||
}
|
||||
program_mode_flag = false;
|
||||
//Check for Idle Package
|
||||
if (byte_array[number_of_bytes - 1] == 255)
|
||||
{
|
||||
@@ -155,16 +192,17 @@ bool address_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]) {
|
||||
uint16_t address_byte_1 = (byte_array[number_of_bytes - 1]) - 192; //remove long address identifier bits
|
||||
uint16_t address_byte_0 = (byte_array[number_of_bytes - 2]);
|
||||
read_address = (address_byte_1 << 8) + address_byte_0;
|
||||
//printf("long address: %u, was read. \n", read_address);
|
||||
uint8_t long_address_mask = 0b00111111;
|
||||
uint16_t long_address = ((CV_ARRAY_FLASH[16]&long_address_mask)<<8)+CV_ARRAY_FLASH[17];
|
||||
return(long_address==read_address);
|
||||
}
|
||||
//Short Address Package
|
||||
else
|
||||
{
|
||||
//start of transmission -> address_byte_0 -> ... -> end of transmission
|
||||
read_address = (byte_array[number_of_bytes - 1]);
|
||||
//printf("short address: %u, was read. \n",read_address);
|
||||
return (CV_ARRAY_FLASH[0] == read_address);
|
||||
}
|
||||
return (CV_ARRAY[0] == read_address); //returns true if Address Matches
|
||||
}
|
||||
|
||||
void instruction_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[]) {
|
||||
@@ -181,14 +219,12 @@ void instruction_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[])
|
||||
if (command_byte_n == 0b00000000){
|
||||
target_speed_step = 1;
|
||||
update_active_functions(0,0,37);
|
||||
program_mode_flag = true;
|
||||
}
|
||||
reset_package_flag = true;
|
||||
}else reset_package_flag = false;
|
||||
//0011-1111 (128 Speed Step Control) - 2 Byte length
|
||||
if (command_byte_n == 0b00111111)
|
||||
{
|
||||
//printf("0011-1111 (128 Speed Step Control) Instruction\n");
|
||||
uint8_t command_byte_n_minus1 = byte_array[command_byte_start_index - 1];
|
||||
if (command_byte_n_minus1 != target_speed_step) interp_speed_step(command_byte_n_minus1);
|
||||
target_speed_step = byte_array[command_byte_start_index - 1];
|
||||
}
|
||||
// 10XX-XXXX (Function Group Instruction)
|
||||
if (command_byte_n >> 6 == 0b00000010)
|
||||
@@ -234,13 +270,13 @@ void instruction_evaluation(uint8_t number_of_bytes,const uint8_t byte_array[])
|
||||
}
|
||||
set_outputs();
|
||||
}
|
||||
|
||||
//Interrupt handler for DCC Logic Signal
|
||||
void gpio_callback_rise(unsigned int gpio, long unsigned int events) {
|
||||
writeLastBit(readBit());
|
||||
int8_t number_of_bytes = check_for_package();
|
||||
if (number_of_bytes != -1) {
|
||||
uint8_t byte_array[SIZE_BYTE_ARRAY] = {0};
|
||||
//uint8_t byte_array[SIZE_BYTE_ARRAY] = {(5^128^63),128,0b00111111,5,0};
|
||||
bits_to_byte_array(number_of_bytes,byte_array);
|
||||
if (error_detection(number_of_bytes,byte_array)) { //Returns true if the XOR verification was error-free
|
||||
if (address_evaluation(number_of_bytes,byte_array)) { //Returns true if address matches with CV
|
||||
@@ -249,6 +285,7 @@ void gpio_callback_rise(unsigned int gpio, long unsigned int events) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void adjust_pwm_level(uint16_t level)
|
||||
{
|
||||
if(target_direction){
|
||||
@@ -264,17 +301,21 @@ void adjust_pwm_level(uint16_t level)
|
||||
void measure(){
|
||||
pwm_set_gpio_level(MOTOR_PWM_FORWARD,0);
|
||||
pwm_set_gpio_level(MOTOR_PWM_REVERSE,0);
|
||||
busy_wait_us(100);
|
||||
busy_wait_us(measurement_delay_in_us);
|
||||
uint sum = 0;
|
||||
for (int i = 0; i < N_MEASUREMENTS; ++i) {
|
||||
for (int i = 0; i < measurement_iterations; ++i) {
|
||||
sum = sum+adc_read();
|
||||
}
|
||||
measurement = (int)sum / N_MEASUREMENTS;
|
||||
measurement = (int)sum / measurement_iterations;
|
||||
}
|
||||
|
||||
void pid_control(){
|
||||
static float output;
|
||||
static int error;
|
||||
static int e_old;
|
||||
static int e_sum;
|
||||
measure();
|
||||
error = (int)target_v_emf - (int)measurement;
|
||||
error = (int)current_v_emf_target - (int)measurement;
|
||||
e_sum = e_sum + error;
|
||||
if (e_sum > sum_limit_max) e_sum = sum_limit_max;
|
||||
if (e_sum < sum_limit_min) e_sum = sum_limit_min;
|
||||
@@ -282,9 +323,23 @@ void pid_control(){
|
||||
e_old = error;
|
||||
if (output > 5000) output = 5000;
|
||||
if (output < 1400) output = 1400;
|
||||
if (target_v_emf == 0) output = 0;
|
||||
if (current_v_emf_target == 0) output = 0;
|
||||
adjust_pwm_level((u_int16_t )output);
|
||||
busy_wait_us(10000);
|
||||
sleep_ms(measurement_period_in_ms);
|
||||
}
|
||||
|
||||
void init_pid(){
|
||||
k_p = (float)CV_ARRAY_FLASH[48]/1024;
|
||||
k_i = (float)CV_ARRAY_FLASH[49]/1024;
|
||||
k_d = (float)CV_ARRAY_FLASH[50]/16384;
|
||||
int t_s_in_us = CV_ARRAY_FLASH[47]*1000+CV_ARRAY_FLASH[46]+CV_ARRAY_FLASH[45]*3;
|
||||
t_s = (float)t_s_in_us/1000000;
|
||||
sum_limit_max = CV_ARRAY_FLASH[51]*10;
|
||||
sum_limit_min = CV_ARRAY_FLASH[52]*(-10);
|
||||
measurement_delay_in_us = CV_ARRAY_FLASH[46];
|
||||
measurement_iterations = CV_ARRAY_FLASH[45];
|
||||
measurement_period_in_ms = CV_ARRAY_FLASH[47];
|
||||
printf("PID variables:\nk_p = %f;\nk_i = %f;\nk_d = %f;\nt_s = %f;\n",k_p,k_i,k_d,t_s);
|
||||
}
|
||||
|
||||
void init_pwm(uint gpio) {
|
||||
@@ -294,14 +349,39 @@ void init_pwm(uint gpio) {
|
||||
pwm_set_gpio_level(gpio,0);
|
||||
pwm_set_clkdiv(slice_num,1);
|
||||
pwm_set_enabled(slice_num, true);
|
||||
printf("PWM on GPIO %u was initialized.\n",gpio);
|
||||
printf("PWM on GPIO %u was initialized.\n",gpio);
|
||||
}
|
||||
|
||||
void init_adc(uint gpio){
|
||||
adc_init();
|
||||
adc_gpio_init(gpio);
|
||||
adc_select_input(0);
|
||||
printf("ADC on GPIO %d was initialized.\n",gpio);
|
||||
printf("ADC on GPIO %d was initialized.\n",gpio);
|
||||
}
|
||||
bool speed_helper(struct repeating_timer *t) {
|
||||
uint end_v_emf_target;
|
||||
//Emergency Stop
|
||||
if (target_speed_step == 129 || target_speed_step==1) current_v_emf_target = 0;
|
||||
else{
|
||||
//First Speed currently equals 2*32 while all the other steps are 1*32
|
||||
//Forward
|
||||
if(target_speed_step>127){
|
||||
target_direction = true;
|
||||
if (target_speed_step == 128) end_v_emf_target = 0;
|
||||
else end_v_emf_target = (target_speed_step - 129) * 32;
|
||||
if (end_v_emf_target > current_v_emf_target) current_v_emf_target+=CV_ARRAY_FLASH[2]+1/8;
|
||||
if (end_v_emf_target < current_v_emf_target) current_v_emf_target-=CV_ARRAY_FLASH[3]+1/8;
|
||||
}
|
||||
//Reverse
|
||||
if(target_speed_step<128) {
|
||||
target_direction = false;
|
||||
if (target_speed_step == 0) end_v_emf_target = 0;
|
||||
else end_v_emf_target = (target_speed_step-1) * 32;
|
||||
if (end_v_emf_target > current_v_emf_target) current_v_emf_target+=CV_ARRAY_FLASH[2]+1/8;
|
||||
if (end_v_emf_target < current_v_emf_target) current_v_emf_target-=CV_ARRAY_FLASH[3]+1/8;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void core1_entry() {
|
||||
@@ -310,48 +390,36 @@ void core1_entry() {
|
||||
init_adc(V_EMF_ADC_PIN);
|
||||
init_pwm(MOTOR_PWM_FORWARD);
|
||||
init_pwm(MOTOR_PWM_REVERSE);
|
||||
init_pid();
|
||||
add_repeating_timer_ms(-CV_ARRAY_FLASH[44], speed_helper, NULL, &speed_helper_timer);
|
||||
printf("Core 1 was initialized.\n");
|
||||
while (1) pid_control();
|
||||
}
|
||||
|
||||
|
||||
void print_buf(const uint8_t *buf, size_t len) {
|
||||
void print_cv_array(const uint8_t *buf, size_t len) {
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
printf("CV_%u: %02x",i+1, buf[i]);
|
||||
if (i % 8 == 7)
|
||||
if (i % 4 == 3)
|
||||
printf("\n");
|
||||
else
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
|
||||
void write_updated_cvs_to_flash(){
|
||||
multicore_reset_core1();
|
||||
uint32_t ints = save_and_disable_interrupts();
|
||||
printf("Erasing CV's...\n");
|
||||
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE*2);
|
||||
printf("Erasing Done.\n");
|
||||
printf("Programming CV's...\n");
|
||||
flash_range_program(FLASH_TARGET_OFFSET, CV_ARRAY, FLASH_PAGE_SIZE * 2);
|
||||
printf("Programming Done. Read back CV's:\n");
|
||||
print_buf(CV_ARRAY_FLASH, FLASH_PAGE_SIZE * 2);
|
||||
restore_interrupts(ints);
|
||||
multicore_launch_core1(core1_entry);
|
||||
}
|
||||
|
||||
int main() {
|
||||
sleep_ms(1000);
|
||||
stdio_init_all();
|
||||
gpio_init(DCC_INPUT_PIN);
|
||||
gpio_set_dir(DCC_INPUT_PIN, GPIO_IN);
|
||||
multicore_launch_core1(core1_entry);
|
||||
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
|
||||
gpio_put(PICO_DEFAULT_LED_PIN, true);
|
||||
busy_wait_ms(1000); //This delay is necessary to catch the breakpoint
|
||||
gpio_set_irq_enabled_with_callback(DCC_INPUT_PIN, GPIO_IRQ_EDGE_RISE, true, &gpio_callback_rise);
|
||||
busy_wait_ms(200); //This delay is necessary to catch the breakpoint
|
||||
//gpio_callback_rise(0,0);
|
||||
gpio_init(PICO_DEFAULT_LED_PIN);
|
||||
gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
|
||||
gpio_put(PICO_DEFAULT_LED_PIN, true);
|
||||
while (1);
|
||||
}
|
||||
|
||||
printf("Core 0 was initialized.\n");
|
||||
printf("CV_ARRAY read from flash:\n");
|
||||
print_cv_array(CV_ARRAY_FLASH, FLASH_PAGE_SIZE * 2);
|
||||
multicore_launch_core1(core1_entry);
|
||||
while (1);
|
||||
}
|
||||
Reference in new issue
Block a user