292 lines
9.7 KiB
C
292 lines
9.7 KiB
C
#include "esp_log.h"
|
|
// #include "crazyadc.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "esp_adc/adc_oneshot.h"
|
|
#include "esp_adc/adc_cali.h"
|
|
#include "esp_adc/adc_cali_scheme.h"
|
|
#include "driver/gpio.h"
|
|
#include "string.h"
|
|
#include "adc_read.h"
|
|
#include "nvsSave.h"
|
|
|
|
#define RIGHT_SENSOR_X ADC_CHANNEL_1
|
|
#define RIGHT_SENSOR_Y ADC_CHANNEL_0
|
|
#define LEFT_SENSOR_X ADC_CHANNEL_4
|
|
#define LEFT_SENSOR_Y ADC_CHANNEL_3
|
|
|
|
#define BUTTON_A GPIO_NUM_10
|
|
#define BUTTON_B GPIO_NUM_7
|
|
#define BUTTON_C GPIO_NUM_6
|
|
// #define SIGNAL_T GPIO_NUM_13
|
|
|
|
#define ADCRATE 64
|
|
#define ADCCOMP 8
|
|
|
|
|
|
struct adcData_t adcData;
|
|
extern bool is_broadcast;
|
|
// struct button_t botton;
|
|
|
|
|
|
#define INPUT_PIN_REG (1 << BUTTON_A | 1 << BUTTON_B | 1 << BUTTON_C)
|
|
// #define OUT_PIN_REG (1 << SIGNAL_T)
|
|
|
|
static const char *TAG = "example";
|
|
|
|
bool send_signal = false;
|
|
bool read_signal = false;
|
|
extern bool semAdcData;
|
|
extern bool semBroadcastData;
|
|
extern bool startConnect;
|
|
|
|
int right_deadzone_x = 564;
|
|
int left_deadzone_x = 579;
|
|
int right_deadzone_y = 575;
|
|
int left_deadzone_y = 1023;
|
|
|
|
int adc_raw[2][2];
|
|
|
|
void getdeadzone(adc_oneshot_unit_handle_t adc_handle)
|
|
{ // 初始化摇杆的起始值
|
|
int adc_test = 0;
|
|
for (int i = 0; i < ADCRATE; i++)
|
|
{
|
|
ESP_ERROR_CHECK(adc_oneshot_read(adc_handle, RIGHT_SENSOR_X, &adc_test));
|
|
right_deadzone_x += adc_test;
|
|
vTaskDelay(pdMS_TO_TICKS(20));
|
|
}
|
|
for (int i = 0; i < ADCRATE; i++)
|
|
{
|
|
ESP_ERROR_CHECK(adc_oneshot_read(adc_handle, RIGHT_SENSOR_Y, &adc_test));
|
|
right_deadzone_y += adc_test;
|
|
vTaskDelay(pdMS_TO_TICKS(20));
|
|
}
|
|
for (int i = 0; i < ADCRATE; i++)
|
|
{
|
|
ESP_ERROR_CHECK(adc_oneshot_read(adc_handle, LEFT_SENSOR_X, &adc_test));
|
|
left_deadzone_x += adc_test;
|
|
vTaskDelay(pdMS_TO_TICKS(20));
|
|
}
|
|
for (int i = 0; i < ADCRATE; i++)
|
|
{
|
|
ESP_ERROR_CHECK(adc_oneshot_read(adc_handle, LEFT_SENSOR_Y, &adc_test));
|
|
left_deadzone_y += adc_test;
|
|
vTaskDelay(pdMS_TO_TICKS(20));
|
|
}
|
|
right_deadzone_x >>= ADCCOMP;
|
|
right_deadzone_y >>= ADCCOMP;
|
|
left_deadzone_x >>= ADCCOMP;
|
|
left_deadzone_y >>= ADCCOMP;
|
|
// ESP_LOGI(TAG, "right_deadzone_x:%d right_deadzone_y:%d left_deadzone_x:%d left_deadzone_y:%d", right_deadzone_x, right_deadzone_y, left_deadzone_x, left_deadzone_y);
|
|
}
|
|
|
|
void adc_read(void *pvParameters)
|
|
{
|
|
gpio_config_t button_gpio_conf = {
|
|
.pin_bit_mask = INPUT_PIN_REG,
|
|
.mode = GPIO_MODE_INPUT,
|
|
.pull_up_en = 0,
|
|
.pull_down_en = 0,
|
|
.intr_type = GPIO_INTR_DISABLE,
|
|
};
|
|
gpio_config(&button_gpio_conf);
|
|
|
|
// gpio_config_t output_gpio_conf = {
|
|
// .pin_bit_mask = OUT_PIN_REG,
|
|
// .mode = GPIO_MODE_OUTPUT,
|
|
// .pull_up_en = 0,
|
|
// .pull_down_en = 0,
|
|
// .intr_type = GPIO_INTR_DISABLE,
|
|
// };
|
|
|
|
// gpio_config(&output_gpio_conf);
|
|
|
|
adc_oneshot_unit_handle_t adc1_handle;
|
|
adc_oneshot_unit_init_cfg_t init_config1 = {
|
|
.unit_id = ADC_UNIT_1,
|
|
};
|
|
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle));
|
|
|
|
//-------------ADC1 Config---------------//
|
|
adc_oneshot_chan_cfg_t config = {
|
|
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
|
.atten = ADC_ATTEN_DB_12,
|
|
};
|
|
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, RIGHT_SENSOR_X, &config));
|
|
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, RIGHT_SENSOR_Y, &config));
|
|
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, LEFT_SENSOR_X, &config));
|
|
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, LEFT_SENSOR_Y, &config));
|
|
// vTaskDelay(pdMS_TO_TICKS(1000));
|
|
getdeadzone(adc1_handle);
|
|
while (1)
|
|
{
|
|
int rightxmid = 0;
|
|
int rightymid = 0;
|
|
int leftxmid = 0;
|
|
int leftymid = 0;
|
|
for(int i=0;i<ADCRATE;i++){
|
|
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, RIGHT_SENSOR_X, &adc_raw[0][0]));
|
|
rightxmid += adc_raw[0][0];
|
|
}
|
|
for(int i=0;i<ADCRATE;i++){
|
|
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, RIGHT_SENSOR_Y, &adc_raw[0][1]));
|
|
rightymid += adc_raw[0][1];
|
|
}
|
|
for(int i=0;i<ADCRATE;i++){
|
|
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, LEFT_SENSOR_X, &adc_raw[1][0]));
|
|
leftxmid += adc_raw[1][0];
|
|
}
|
|
for(int i=0;i<ADCRATE;i++){
|
|
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, LEFT_SENSOR_Y, &adc_raw[1][1]));
|
|
leftymid += adc_raw[1][1];
|
|
}
|
|
|
|
// 对于左摇杆处理
|
|
uint16_t rightx = (rightxmid >> ADCCOMP);
|
|
uint16_t righty = (rightymid >> ADCCOMP);
|
|
uint16_t leftx = (leftxmid >> ADCCOMP);
|
|
uint16_t lefty = (leftymid >> ADCCOMP);
|
|
// ESP_LOGI(TAG, "rightx:%d righty:%d leftx:%d lefty:%d", rightx, righty, leftx, lefty);
|
|
// ESP_LOGI(TAG, "right_deadzone_x:%d right_deadzone_y:%d left_deadzone_x:%d left_deadzone_y:%d", right_deadzone_x, right_deadzone_y, left_deadzone_x, left_deadzone_y);
|
|
|
|
while(semAdcData == false){
|
|
vTaskDelay(pdMS_TO_TICKS(10));
|
|
}
|
|
memset(&adcData,0,sizeof(struct adcData_t));
|
|
semAdcData = false;
|
|
// 马达油门控制 thrust
|
|
if (left_deadzone_y - lefty > 40)
|
|
{
|
|
adcData.fowardA =50 + 1.0*(left_deadzone_y - lefty)/left_deadzone_y*205; //设置向前
|
|
// ESP_LOGI(TAG, "forwardA:%d", adcData.fowardA);
|
|
}
|
|
else if (lefty - left_deadzone_y > 40)
|
|
{
|
|
adcData.backA =50 + 1.0*(lefty - left_deadzone_y)/left_deadzone_y*205; //设置向后
|
|
// ESP_LOGI(TAG, "backA:%d", adcData.backA);
|
|
}
|
|
|
|
// 前进方向控制
|
|
if ( righty - right_deadzone_y > 40 )
|
|
{
|
|
adcData.fowardB = 50 + 1.0*(righty - right_deadzone_y)/right_deadzone_y*205; //设置向前
|
|
// ESP_LOGI(TAG, "forwardB:%d", adcData.fowardB);
|
|
}
|
|
else if (right_deadzone_y - righty >40 )
|
|
{
|
|
adcData.backB = 50 + 1.0*(right_deadzone_y - righty)/right_deadzone_y*205; //设置向后
|
|
// ESP_LOGI(TAG, "backB:%d", adcData.backB);
|
|
}
|
|
|
|
// 左右方向控制
|
|
if (leftx - left_deadzone_x > 40 )
|
|
{
|
|
adcData.leftA = 1.0*(leftx - left_deadzone_x)/left_deadzone_x*255; //设置向左
|
|
// ESP_LOGI(TAG, "leftA:%d", adcData.leftA);
|
|
}
|
|
else if (left_deadzone_x - leftx > 40)
|
|
{
|
|
adcData.rightA = 1.0*(left_deadzone_x - leftx)/left_deadzone_x*255; //设置向右
|
|
// ESP_LOGI(TAG, "rightA:%d", adcData.rightA);
|
|
}
|
|
|
|
// 旋转控制
|
|
if (right_deadzone_x - rightx > 40)
|
|
{
|
|
adcData.leftB = 1.0*(right_deadzone_x - rightx)/right_deadzone_x*255; //设置向左
|
|
// ESP_LOGI(TAG, "rightB:%d", adcData.rightB);
|
|
}
|
|
else if (rightx - right_deadzone_x > 40)
|
|
{
|
|
adcData.rightB = 1.0*(rightx - right_deadzone_x)/right_deadzone_x*255; //设置向右
|
|
// ESP_LOGI(TAG, "leftB:%d", adcData.leftB);
|
|
}
|
|
// ESP_LOGI(TAG, "adcData: forwardA:%d forwardB:%d leftA:%d leftB:%d rightA:%d rightB:%d backA:%d backB:%d",adcData.fowardA,adcData.fowardB,adcData.leftA,adcData.leftB,adcData.rightA,adcData.rightB,adcData.backA,adcData.backB);
|
|
if (gpio_get_level(BUTTON_A) == 0)
|
|
{
|
|
ESP_LOGI(TAG,"BUTTONA PREED");
|
|
vTaskDelay(10);
|
|
if (gpio_get_level(BUTTON_A) == 0)
|
|
{
|
|
adcData.A = 1;
|
|
}
|
|
}
|
|
else if (gpio_get_level(BUTTON_B) == 0)
|
|
{
|
|
ESP_LOGI(TAG,"BUTTONB PREED");
|
|
bool buttonB_bool = false;
|
|
for (int i = 0; i < 30; i++)
|
|
{
|
|
vTaskDelay(pdMS_TO_TICKS(100));
|
|
if (gpio_get_level(BUTTON_B) == 0)
|
|
{
|
|
if (i == 29)
|
|
{
|
|
ESP_LOGI(TAG,"pree %d",i);
|
|
if (is_broadcast == true)
|
|
{
|
|
startConnect = true;
|
|
}
|
|
delete_mac_from_nvs("storage", "mac");
|
|
while (gpio_get_level(BUTTON_B) == 0)
|
|
{
|
|
vTaskDelay(pdMS_TO_TICKS(10));
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
break;
|
|
}
|
|
// vTaskDelay(pdMS_TO_TICKS(3000)); // 按三秒就可以
|
|
// if (gpio_get_level(BUTTON_B) == 0)
|
|
// {
|
|
// ESP_LOGI(TAG,"BUTTONB IS PREED");
|
|
// if (is_broadcast == true)
|
|
// {
|
|
// startConnect = true;
|
|
// }
|
|
// delete_mac_from_nvs("storage", "mac");
|
|
// while(1){
|
|
// vTaskDelay(pdMS_TO_TICKS(20));
|
|
// if (gpio_get_level(BUTTON_B) == 1){
|
|
// ESP_LOGI(TAG,"BUTTONB IS on");
|
|
// break;
|
|
// }
|
|
// }
|
|
// }
|
|
}
|
|
else if (gpio_get_level(BUTTON_C) == 0)
|
|
{
|
|
ESP_LOGI(TAG,"BUTTONC PREED");
|
|
vTaskDelay(10);
|
|
if (gpio_get_level(BUTTON_C) == 0)
|
|
{
|
|
adcData.C = 1;
|
|
}
|
|
}
|
|
semAdcData = true;
|
|
vTaskDelay(pdMS_TO_TICKS(10));
|
|
}
|
|
|
|
vTaskDelete(NULL);
|
|
}
|
|
|
|
|
|
// void ButtonHandle(void *pvParameters){
|
|
// gpio_config_t button_gpio_conf = {
|
|
// .pin_bit_mask = INPUT_PIN_REG,
|
|
// .mode = GPIO_MODE_INPUT,
|
|
// .pull_up_en = 0,
|
|
// .pull_down_en = 0,
|
|
// .intr_type = GPIO_INTR_DISABLE,
|
|
// };
|
|
// gpio_config(&button_gpio_conf);
|
|
// while(1){
|
|
|
|
// vTaskDelay(20);
|
|
// }
|
|
|
|
|
|
// vTaskDelete(NULL);
|
|
// }
|