diff --git a/platformio.ini b/platformio.ini index 881dac2..d6156f9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,5 +14,6 @@ board = upesy_wroom framework = arduino lib_extra_dirs = ~/Documents/Arduino/libraries lib_deps = - ./lib + ./lib + arduino-libraries/Stepper@^1.1.3 monitor_speed = 115200 diff --git a/src/adc1256.cpp b/src/adc1256.cpp index 031a217..f229c25 100644 --- a/src/adc1256.cpp +++ b/src/adc1256.cpp @@ -27,12 +27,12 @@ void adc1256_reset(void) void adc1256_set_channel(uint8_t ch) { + adc.waitDRDY(); adc.setChannel(ch); } float adc1256_read(void) { - // adc.waitDRDY(); return adc.readCurrentChannel(); } diff --git a/src/board.cpp b/src/board.cpp index c690f2d..a4afd96 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -10,5 +10,5 @@ void board_init(void) } Serial.println("ADS1256 - 2023-12-22"); adc1256_init(); - // motor_init(); + motor_init(); } diff --git a/src/main.cpp b/src/main.cpp index 40caaba..08f691e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,7 @@ volatile bool task_adc1256_flag = false; String read_val = ""; // 用于储存串口接收的字符串 float array[5000]; // 存储数据 - +int motor_home_step = 0; String fenge(String str, String fen, int index) { int weizhi; @@ -33,7 +33,7 @@ String fenge(String str, String fen, int index) void task_ads(void *pt) { - float *array; // 存储数据 + float array[1000]; // 存储数据 uint16_t i = 0, j = 0; float temp = 0; @@ -41,45 +41,85 @@ void task_ads(void *pt) { while (task_adc1256_flag) { - Serial.println("Waiting for processing ..."); - - String part01 = fenge(read_val, ",", 0); - String part02 = fenge(read_val, ",", 1); - String part03 = fenge(read_val, ",", 2); - if (part01 == "r") + if (read_val == "h") { - adc1256_set_channel(3); - if (part02 != "" && part03 != "") + if (digitalRead(LIMIT) == 1) { - int count = part02.toInt(); - int delay = part03.toInt(); + motor_limit = MOTOR_LIMIT_OUTSIDE; - array = (float *)malloc(count * sizeof(float)); - memset(array, 0, sizeof(array)); - for (i = 0; i < count; i++) - { - array[i] = adc1256_read(); - vTaskDelay(delay); - } - // 对array[i]从小到大排序后取中间一个值 - for (i = 0; i < count - 1; i++) - { - for (j = 0; j < count - 1 - i; j++) - { - if (array[j] > array[j + 1]) - { - temp = array[j]; - array[j] = array[j + 1]; - array[j + 1] = temp; - } - } - } - Serial.printf("r,%.4f", array[count / 2]); - free(array); + Serial.println("当前位置在限位器外,准备归位"); } else { - Serial.println("Error: Invalid input"); + motor_limit = MOTOR_LIMIT_WITHIN; + motor_dir(-1); + Serial.println("当前位置在限位器内,准备归位"); + } + motor_home_step = 0; + motor_limit_on(); + } + else if (read_val == "s") + { + motor_limit_off(); + } + else if (read_val == "r") + { + motor_dir(1); + } + else if (read_val == "l") + { + motor_dir(-1); + } + else if (read_val == "m") + { + motor_limit_on(); + } + else + { + Serial.println("开始读取ADC ..."); + String part01 = fenge(read_val, ",", 0); + String part02 = fenge(read_val, ",", 1); + String part03 = fenge(read_val, ",", 2); + if (part01 == "r") + { + adc1256_set_channel(3); + Serial.printf("r,%.4f", adc1256_read()); + // if (part02 != "" && part03 != "") + // { + // int count = part02.toInt(); + // int delay = part03.toInt(); + // if (count < 1000) + // { + // memset(array, 0, sizeof(array)); + // for (i = 0; i < count; i++) + // { + // array[i] = adc1256_read(); + // vTaskDelay(delay); + // } + // // 对array[i]从小到大排序后取中间一个值 + // for (i = 0; i < count - 1; i++) + // { + // for (j = 0; j < count - 1 - i; j++) + // { + // if (array[j] > array[j + 1]) + // { + // temp = array[j]; + // array[j] = array[j + 1]; + // array[j + 1] = temp; + // } + // } + // } + // Serial.printf("r,%.4f", array[count / 2]); + // } + // else + // { + // Serial.println("Error: Invalid input"); + // } + // } + // else + // { + // Serial.println("Error: Invalid input"); + // } } } @@ -107,6 +147,11 @@ void task_serial(void *pt) { task_adc1256_flag = true; } + else + { + // read_val = "r,100,10"; + // task_adc1256_flag = true; + } } else { @@ -117,17 +162,56 @@ void task_serial(void *pt) } } +void task_motor(void *pt) +{ + for (;;) + { + motor_home(); + int res = digitalRead(LIMIT); + if (motor_limit == MOTOR_LIMIT_WITHIN && res == 1) + { + motor_dir_reaction(); + motor_limit = MOTOR_LIMIT_OUTSIDE; + } + + if (motor_limit_flag == true && res == 0) + { + if (motor_limit == MOTOR_LIMIT_OUTSIDE && motor_dir_get() > 0) + { + if (++motor_home_step == 12) + { + motor_limit_off(); + Serial.println("归位完成"); + } + } + + if (motor_limit == MOTOR_LIMIT_OUTSIDE && motor_dir_get() < 0) + { + if (++motor_home_step == 6) + { + motor_limit_off(); + Serial.println("归位完成"); + } + } + } + + vTaskDelay(10); + } +} + void setup() { // put your setup code here, to run once: board_init(); xTaskCreate(task_serial, "task_serial", 1024 * 4, NULL, 1, NULL); - xTaskCreate(task_ads, "task_ads", 1024 * 4, NULL, 1, NULL); + xTaskCreate(task_ads, "task_ads", 1024 * 8, NULL, 1, NULL); + xTaskCreate(task_motor, "task_motor", 1024 * 8, NULL, 1, NULL); } void loop() { // put your main code here, to run repeatedly: - vTaskDelay(1000); // 延时100ms + + vTaskDelay(1000); } diff --git a/src/motor.cpp b/src/motor.cpp index d3d76e5..ab8a088 100644 --- a/src/motor.cpp +++ b/src/motor.cpp @@ -1,13 +1,60 @@ #include +#include #include "motor.h" +#define PUL 26 // CLK 输出端口为 PUL(脉冲信号) +#define DIR 25 // CW 输出端口为DIR(方向信号) + +#define PULSE 400.0 // 脉冲数 +#define ANGLE 360.0 / PULSE // 每个脉冲的角度 + +Stepper mstepper(PULSE, DIR, PUL); +volatile bool motor_limit_flag = false; +int motor_limit = MOTOR_LIMIT_OUTSIDE; +int _motor_dir_set = 1; + +void motor_dir(int dir) +{ + _motor_dir_set = dir; +} + +void motor_dir_reaction(void) +{ + _motor_dir_set = _motor_dir_set < 0 ? 1 : -1; +} + +int motor_dir_get(void) +{ + return _motor_dir_set; +} + +void motor_limit_on(void) +{ + motor_limit_flag = true; +} + +void motor_limit_off(void) +{ + motor_limit_flag = false; +} void motor_init(void) { - pinMode(PUL, OUTPUT); - pinMode(DIR, OUTPUT); + mstepper.setSpeed(PULSE); + pinMode(LIMIT, INPUT_PULLUP); + motor_limit_flag = false; +} + +// 步进电机归位 +void motor_home(void) +{ + if (motor_limit_flag == true) + { + int dir = _motor_dir_set < 0 ? -1 : 1; + + mstepper.step(PULSE * dir); + } } void motor_loop(void) { - digitalWrite(DIR, LOW); // 先让arduino向驱动板输出一个低电平使其反转。 } diff --git a/src/motor.h b/src/motor.h index ddd851e..5f99e3e 100644 --- a/src/motor.h +++ b/src/motor.h @@ -1,8 +1,23 @@ #ifndef MOTOR_H #define MOTOR_H -#define PUL 35 // CLK 定义9输出端口为 PUL(脉冲信号) -#define DIR 34 // CW 定义10输出端口为DIR(方向信号) + +#define LIMIT 22 // 限位器 + +typedef enum +{ + MOTOR_LIMIT_WITHIN, + MOTOR_LIMIT_OUTSIDE, +} motor_limit_e; + +extern volatile bool motor_limit_flag; +extern int motor_limit; // Function declarations void motor_init(void); void motor_loop(void); +void motor_home(void); +void motor_limit_on(void); +void motor_limit_off(void); +void motor_dir(int dir); +void motor_dir_reaction(void); +int motor_dir_get(void); #endif // MOTOR_H