修改限位器
This commit is contained in:
parent
2792a8d347
commit
f874a1a70d
|
@ -14,5 +14,6 @@ board = upesy_wroom
|
||||||
framework = arduino
|
framework = arduino
|
||||||
lib_extra_dirs = ~/Documents/Arduino/libraries
|
lib_extra_dirs = ~/Documents/Arduino/libraries
|
||||||
lib_deps =
|
lib_deps =
|
||||||
./lib
|
./lib
|
||||||
|
arduino-libraries/Stepper@^1.1.3
|
||||||
monitor_speed = 115200
|
monitor_speed = 115200
|
||||||
|
|
|
@ -27,12 +27,12 @@ void adc1256_reset(void)
|
||||||
|
|
||||||
void adc1256_set_channel(uint8_t ch)
|
void adc1256_set_channel(uint8_t ch)
|
||||||
{
|
{
|
||||||
|
adc.waitDRDY();
|
||||||
adc.setChannel(ch);
|
adc.setChannel(ch);
|
||||||
}
|
}
|
||||||
|
|
||||||
float adc1256_read(void)
|
float adc1256_read(void)
|
||||||
{
|
{
|
||||||
// adc.waitDRDY();
|
|
||||||
return adc.readCurrentChannel();
|
return adc.readCurrentChannel();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,5 +10,5 @@ void board_init(void)
|
||||||
}
|
}
|
||||||
Serial.println("ADS1256 - 2023-12-22");
|
Serial.println("ADS1256 - 2023-12-22");
|
||||||
adc1256_init();
|
adc1256_init();
|
||||||
// motor_init();
|
motor_init();
|
||||||
}
|
}
|
||||||
|
|
158
src/main.cpp
158
src/main.cpp
|
@ -4,7 +4,7 @@
|
||||||
volatile bool task_adc1256_flag = false;
|
volatile bool task_adc1256_flag = false;
|
||||||
String read_val = ""; // 用于储存串口接收的字符串
|
String read_val = ""; // 用于储存串口接收的字符串
|
||||||
float array[5000]; // 存储数据
|
float array[5000]; // 存储数据
|
||||||
|
int motor_home_step = 0;
|
||||||
String fenge(String str, String fen, int index)
|
String fenge(String str, String fen, int index)
|
||||||
{
|
{
|
||||||
int weizhi;
|
int weizhi;
|
||||||
|
@ -33,7 +33,7 @@ String fenge(String str, String fen, int index)
|
||||||
|
|
||||||
void task_ads(void *pt)
|
void task_ads(void *pt)
|
||||||
{
|
{
|
||||||
float *array; // 存储数据
|
float array[1000]; // 存储数据
|
||||||
|
|
||||||
uint16_t i = 0, j = 0;
|
uint16_t i = 0, j = 0;
|
||||||
float temp = 0;
|
float temp = 0;
|
||||||
|
@ -41,45 +41,85 @@ void task_ads(void *pt)
|
||||||
{
|
{
|
||||||
while (task_adc1256_flag)
|
while (task_adc1256_flag)
|
||||||
{
|
{
|
||||||
Serial.println("Waiting for processing ...");
|
if (read_val == "h")
|
||||||
|
|
||||||
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);
|
if (digitalRead(LIMIT) == 1)
|
||||||
if (part02 != "" && part03 != "")
|
|
||||||
{
|
{
|
||||||
int count = part02.toInt();
|
motor_limit = MOTOR_LIMIT_OUTSIDE;
|
||||||
int delay = part03.toInt();
|
|
||||||
|
|
||||||
array = (float *)malloc(count * sizeof(float));
|
Serial.println("当前位置在限位器外,准备归位");
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
else
|
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;
|
task_adc1256_flag = true;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// read_val = "r,100,10";
|
||||||
|
// task_adc1256_flag = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
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()
|
void setup()
|
||||||
{
|
{
|
||||||
// put your setup code here, to run once:
|
// put your setup code here, to run once:
|
||||||
board_init();
|
board_init();
|
||||||
|
|
||||||
xTaskCreate(task_serial, "task_serial", 1024 * 4, NULL, 1, NULL);
|
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()
|
void loop()
|
||||||
{
|
{
|
||||||
// put your main code here, to run repeatedly:
|
// put your main code here, to run repeatedly:
|
||||||
vTaskDelay(1000); // 延时100ms
|
|
||||||
|
vTaskDelay(1000);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,60 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
#include <Stepper.h>
|
||||||
#include "motor.h"
|
#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)
|
void motor_init(void)
|
||||||
{
|
{
|
||||||
pinMode(PUL, OUTPUT);
|
mstepper.setSpeed(PULSE);
|
||||||
pinMode(DIR, OUTPUT);
|
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)
|
void motor_loop(void)
|
||||||
{
|
{
|
||||||
digitalWrite(DIR, LOW); // 先让arduino向驱动板输出一个低电平使其反转。
|
|
||||||
}
|
}
|
||||||
|
|
19
src/motor.h
19
src/motor.h
|
@ -1,8 +1,23 @@
|
||||||
#ifndef MOTOR_H
|
#ifndef MOTOR_H
|
||||||
#define 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
|
// Function declarations
|
||||||
void motor_init(void);
|
void motor_init(void);
|
||||||
void motor_loop(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
|
#endif // MOTOR_H
|
||||||
|
|
Reference in New Issue