机器谱 发表于 2023-10-23 13:27:25

如何用WiFi实现无线定位

本帖最后由 机器谱 于 2023-10-23 13:27 编辑

一、WiFi主从模块设置
1. 实验器材
https://28846868.s21i.faiusr.com/4/ABUIABAEGAAg3IWPqAYo7c_o4QcwvQw4jAM!600x600.png.webp
2. 实验步骤
      ① 给控制板刷一套空的程序。
https://28846868.s21i.faiusr.com/4/ABUIABAEGAAgkIePqAYosYj6vwIw5gM40QQ!400x400.png.webp
② 将Esp8266模块连接到Bigfish扩展板上,并将扩展板插到控制板上。
https://28846868.s21i.faiusr.com/4/ABUIABAEGAAgnIiPqAYowpLQswQwwQc4qAM!600x600.png.webp
③ 在arduino的Seiral Monitor中,输入AT指令集,观察模块的相应应答。
https://28846868.s21i.faiusr.com/4/ABUIABAEGAAgn4mPqAYokaqYTDDeBTi1BQ!400x400.png.webp
3. 常用指令
      ① 基础AT指令。
https://28846868.s21i.faiusr.com/4/ABUIABAEGAAgyo_PqAYomdft8QQw4go4hgI!600x600.png.webp
      ② WiFi功能AT指令。
https://28846868.s21i.faiusr.com/4/ABUIABAEGAAg-Y_PqAYowNrE6gMw6wk4hAM!600x600.png.webp
      ③ TCP/IP 工具箱 AT 命令。
https://28846868.s21i.faiusr.com/4/ABUIABAEGAAgtJCPqAYo0qDmtQEwpQk47AM!600x600.png.webp
二、WiFi定位1. 实验器材
https://28846868.s21i.faiusr.com/4/ABUIABAEGAAgnJGPqAYouPXepwcwpg04_AQ!600x600.png.webp
2. 定位算法
      Rssi三点定位算法。
https://28846868.s21i.faiusr.com/4/ABUIABAEGAAg9pGPqAYozdr-vwMwwgY49QU!300x300.png.webp
3. 实验操作
       ① 使用文末资料中的“ESP8266调试工具”将三个模块设置为AP模式,并记录其NAME,用于存储在程序中,按照下面方法连接ESP8266模块和Basra主控板。
https://28846868.s21i.faiusr.com/4/ABUIABAEGAAg_ZKPqAYo0Y-g8wMw9gY4-gI!600x600.png.webp
       ② 使用“ESP8266调试工具”将与主控板连接的WiFi模块设置为Statino模式,按照下面方法连接ESP8266模块和BigFish主控板,OLED显示屏使用四芯输入排线与BigFish进行连接。
https://28846868.s21i.faiusr.com/4/ABUIABAEGAAg0ZOPqAYoh7S2_AQwvAc44wI!600x600.png.webp
       ③ 将实验例程(ESP8266RSSIPosition.ino)下载到主控板中。
/*------------------------------------------------------------------------------------

版权说明:Copyright 2023 Robottime(Beijing) Technology Co., Ltd. All Rights Reserved.

         Distributed under MIT license.See file LICENSE for detail or copy at

         https://opensource.org/licenses/MIT

         by 机器谱 2023-09-12 https://www.robotway.com/

------------------------------*/

/*将与主控板相连接的wifi模块使用“esp8266调试工具软件”设置为Station模式

将其余三个模块设置为AP模式,并记录其NAME,存储在程序中*/

//#define DEBUG

#define ESP_AP_NUMBER 3

#include <SoftwareSerial.h>

#include <RssiPositionComputer.h>

#include <Wire.h>

#include <MultiLCD.h>

SoftwareSerial myESP(2,3);

RssiPositionComputer myPositionComputer;

Point2D master_point;

LCD_SSD1306 lcd;

char esp_ap_name = {"ESP826601","ESP826602","ESP826603"};

int   rssi;

float distance;

void setup()

{

delay(1000);

Serial.begin(115200);

myESP.begin(9600);

#ifdef DEBUG

Serial.println("begin");

#endif

while(myESP.available()&&myESP.read());

while(!myESP.available())

{

myESP.println("AT");

delay(1000);

}

while(myESP.available()&&myESP.read());

#ifdef DEBUG

Serial.println("Resonse ok");

#endif

lcd.begin();

    lcd.clear();

    lcd.setCursor(30,4);

    lcd.print("Hello, world!");

}

void loop()

{

int n = searchESPAP(esp_ap_name,rssi);

for(int i=0;i<ESP_AP_NUMBER;i++)

{

distance = rssiToDistance(rssi);

Serial.print(distance);

Serial.print('\t');

}

Serial.print(n);

Serial.print('\t');

if(myPositionComputer.distanceToPoint(distance,distance,distance,&master_point)==true)

    {

      Serial.print(master_point.x);

      Serial.print('\t');

      Serial.print(master_point.y);

      Serial.print('\t');

      Serial.println("position okok");

      lcd.clear();

      lcd.setCursor(30,2);

      lcd.printLong(master_point.x*100,FONT_SIZE_LARGE);   //按厘米输出

      lcd.setCursor(30,5);

      lcd.printLong(master_point.y*100,FONT_SIZE_LARGE);

    }

    else

    {

    lcd.clear();

    lcd.setCursor(30,4);

    lcd.print("position ERROR!");

      Serial.println("position ERROR");   

    }

}

int nameToNumber(char in[],char name[])

{

for(int i=0;i<3;i++)

{

for(int j=0;j<9;j++)

{

if(in != name)

break;

if(j==8)

return(i);

}

}

return(-1);

}

byte searchESPAP(char name[], int rs[])

{

byte search_result_number = 0;

int state = 0;

int n;

int ap_n;

char name_string;

char rssi_string;

while(myESP.available()&&myESP.read());

myESP.println("AT+CWLAP");

delay(100);

while(myESP.available()&&myESP.read());

unsigned long t = millis();

while(!myESP.available())

{

if(millis()-t<3000)

delay(5);

else

return(0);

}

#ifdef DEBUG

Serial.println("received........");

#endif

t = millis();

while(myESP.available()||(millis()-t<3000))

{

if(!myESP.available())

continue;

char in_char = myESP.read();

#ifdef DEBUG

Serial.print(in_char);

#endif

if( (state == 0)&&(in_char=='(') )

{

state = 1;

n = -4;

}

else if(state == 1)

{

n++;

if(n>=0)

name_string = in_char;

if(n == 8)

{

n = -3;

ap_n = nameToNumber(name_string,name);

if(ap_n != -1)

{

state = 2;

#ifdef DEBUG

Serial.print('\n');

Serial.print("ap_n:");Serial.println(ap_n);

#endif

}

else

{

state = 0;

}

}

}

else if(state==2)

{

n++;

if(n>=0)

rssi_string = in_char;

if(n == 2)

{

rs = atof(rssi_string);

state = 0;

search_result_number++;

#ifdef DEBUG

Serial.print('\n');

Serial.print("rssi["+String(ap_n)+"]:");Serial.println(rs);

#endif

}

}

}

return(search_result_number);

}

float rssiToDistance(int rssi)

{

float dis = 0;

dis = pow(10.0,((abs(rssi)-47)/10.0/2.212));

return dis;

}



④ 将不同模块按照三点定位摆放开(在1m×1m的空间内),如下图所示,观察实验效果。
https://28846868.s21i.faiusr.com/4/ABUIABAEGAAgxZaPqAYorOXm2AUw8gw41Qk!300x300.png.webp https://28846868.s21i.faiusr.com/4/ABUIABAEGAAg4JaPqAYo28qsBDDyDDjVCQ!300x300.png.webp

4. 资料下载
资料内容:
①程序源代码
②ESP8266调试工具
③ESP8266相关学习资料
资料下载地址:如何用WiFi实现无线定位 https://www.robotway.com/h-col-267.html
页: [1]
查看完整版本: 如何用WiFi实现无线定位