养生 装修 购物 美食 感冒 便秘 营销 加盟 小吃 火锅 管理 创业 搭配 减肥 培训 旅游

如何用c#编写关于全局定位实时显示地图的上位机

时间:2024-10-28 13:13:53

这是一个基于某省机器人设计大赛的环境下做出的一个小功能,可以用于测试机器人的性能,以及相关自检和决策。这里只是小车在场上的位置显示。

工具/原料

上位机C#的编程环境和arduino编程环境

方法/步骤

1、上位机程序namesp瞢铍库祢ace全局定位1{publicpartialclassForm1多唉捋胝:Form{Stopwatchsw=newStopwatch();//PID暂存doublex=0.0;doubley=0.0;doublep=0.0;booldrawornot=false;publicForm1(){InitializeComponent();//获取端口列表pictureBox2.Image=Image.FromFile("车.PNG");pictureBox1.Image=Image.FromFile("比赛场地.PNG");string[]ports=SerialPort.GetPortNames();//得到接口名字//将端口列表添加到comboBoxthis.comboBox2.Items.AddRange(ports);serialPort1.DataReceived+=DataReceivedHandler;sw.Start();Control.CheckForIllegalCrossThreadCalls=false;//防止跨线程出错//定时器中断时间timer1.Interval=100;timer2.Interval=100;timer3.Interval=300;}staticintbuffersize=18;//十六进制数的大小(假设为9Byte,可调整数字大小)byte[]buffer=newByte[buffersize];//创建缓冲区privatevoidForm1_Load(objectsender,EventArgse){comboBox1.Items.Add("1200");comboBox1.Items.Add("2400");comboBox1.Items.Add("4800");comboBox1.Items.Add("9600");comboBox1.Items.Add("14400");comboBox1.Items.Add("19200");comboBox1.Items.Add("28800");comboBox1.Items.Add("38400");//常用的波特率try{string[]ports=SerialPort.GetPortNames();//得到接口名字//将端口列表添加到comboBoxthis.comboBox2.Items.AddRange(ports);///设置波特率serialPort1.BaudRate=Convert.ToInt32(comboBox1.Text);}catch(Exceptionex){}}privatevoidpictureBox1_Click(objectsender,EventArgse)//用于显示地图{}privatevoidbutton2_Click(objectsender,EventArgse)//接收/暂停数据按钮{if(serialPort1.IsOpen)////(更新)如果按下按钮之前串口是开的,就断开//如果按下按钮之前flag的内容是false按下之后内容改成true然后打开串口{serialPort1.Close();button2.Text="连接";timer3.Stop();}else{//要打开串口要看波特率串口等有没有设置对boolno_error_flag=true;try{serialPort1.BaudRate=Convert.ToInt32(comboBox1.SelectedItem);}catch(ArgumentExceptione1){this.errorProvider1.SetError(this.comboBox1,"不能为空");no_error_flag=false;}try{serialPort1.PortName=Convert.ToString(comboBox2.SelectedItem);}catch(ArgumentExceptione2){this.errorProvider1.SetError(this.comboBox2,"不能为空");no_error_flag=false;}try{serialPort1.Open();}catch{MessageBox.Show("端口错误","警告");no_error_flag=false;}if(no_error_flag){button2.Text="断开连接";}}}privatevoidtimer3_Tick(objectsender,EventArgse)//第三定位器{if(serialPort1.IsOpen){textBox1.Text=data_warehouse;textBox1.SelectionStart=textBox1.TextLength;//光标定位到文本最后textBox1.ScrollToCaret();UpdateQueueValue();}if(drawornot){MessageBox.Show("数据接收失败!","系统提示");}}//串口接收完成事件——接收所有数据stringdata_warehouse="";publicvoidDataReceivedHandler(objectsender,SerialDataReceivedEventArgse){//读取串口中的所有数据stringreadfromport=serialPort1.ReadExisting();data_warehouse+=readfromport;}//更新队列程序string[]separators={"A:","B:","X:","Y:","P:"};intpos=0;privatevoidUpdateQueueValue(){while(data_warehouse.Length>35){//第一个换行符的位置pos=data_warehouse.IndexOf('\n');//断句第一个换行符Debug.WriteLine(""+pos);strings=data_warehouse.Remove(pos).ToUpper();//删掉读到的第一句话data_warehouse=data_warehouse.Remove(0,pos+1);//开始断句string[]words=s.Split(separators,StringSplitOptions.RemoveEmptyEntries);//读取断句中的数据try{//floatd1=Convert.ToSingle(words[0]);//floatd2=Convert.ToSingle(words[1]);x=Convert.ToDouble(words[2]);y=Convert.ToDouble(words[3]);p=Convert.ToDouble(words[4]);Debug.WriteLine(words[3]);}catch(Exceptione){Debug.WriteLine(e.ToString());}}}privatevoidbutton4_Click(objectsender,EventArgse)//开始画图按钮{this.pictureBox2.Image.RotateFlip(RotateFlipType.Rotate90FlipXY);//这里我需要得到陀螺仪给的角度p,然后用if语句判断角//度p来执行这条语句,实现90度旋转this.pictureBox2.Refresh();if(data_warehouse.Length>0){MessageBox.Show("数据接收成功!","系统提示");}else{MessageBox.Show("数据接受失败!","警告");}timer1.Start();timer2.Start();timer3.Start();

2、arduino程序#include<SPI.h>#include<Wire.h>#include<Adafruit_GFX.h>#include<Adafruit_SSD1306.h>#defineOLED_RESET4Adafruit_SSD1306display(OLED_RESET);constintX_dir=9;constintY_dir=10;doubleX_juli,Y_juli;constbyteX_interruptPin=2;constbyteY_interruptpin=3;volatilebytexstate=HIGH;volatilebyteystate=HIGH;doublex=0,y=0;voidsetup(){display.begin(SSD1306_SWITCHCAPVCC);Serial.begin(9600);pinMode(X_dir,INPUT_PULLUP);pinMode(X_interruptPin,INPUT_PULLUP);pinMode(Y_dir,INPUT_PULLUP);pinMode(Y_interruptpin,INPUT_PULLUP);attachInterrupt(digitalPinToInterrupt(X_interruptPin),xblink,FALLING);attachInterrupt(digitalPinToInterrupt(Y_interruptpin),yblink,FALLING);}voidloop(){X_juli=1.0*x/256*118.3;Y_juli=1.0*y/256*118.3;Serial.print(X_juli);Serial.print("");Serial.println(Y_juli);/*display.setTextSize(1);display.setTextColor(WHITE);display.setCursor(0,0);display.clearDisplay();display.print("x=");display.println(X_juli);display.print("y=");display.println(Y_juli);display.display();*/delay(1);}voidxblink(){xstate=digitalRead(X_dir);if(xstate==HIGH)x--;elsex++;}voidyblink(){ystate=digitalRead(Y_dir);if(ystate==HIGH)y++;elsey--;}

© 一点知识