Ultrasonic_RADAR.exe
0.01MB

 

 

#소스코드
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO.Ports; 

namespace Ultrasonic_RADAR 
{ 
    public partial class Form1 : Form 
    { 
        // 시리얼 관련 변수 
        string dataIN;  //시리얼 읽기 
        double number1;   //문자 -> 부동소수점으로 변경 

        int measure_flag = 0; 
        // 레이더 그리기 
        Timer t = new Timer(); 
        int width = 400, height = 400, Hand = 200; 
        int u;  
        int cx, cy;  
        int x, y;  
        int tx, ty, lim = 20; 

        Bitmap bmp; // 픽셀 
        Pen p;      // 선 
        Graphics g; 
         
        public Form1() 
        { 
            InitializeComponent(); 
        } 

        private void Form1_Load_1(object sender, EventArgs e) 
        { 
            // 시리얼 포트 초기화 
            string[] ports = SerialPort.GetPortNames(); 
            cBoxCOMPORT.Items.AddRange(ports); 

            // 레이더 그리기 초기화 
            bmp = new Bitmap(width + 1, height + 1); 
            this.BackColor = Color.Gray;  

            //center  
            cx = width / 2;  
            cy = height / 2;  

            //initial degree of HAND  
            u = 0;  

            //timer              
            //t.Interval = 5; //in millisecond  
            //t.Tick += new EventHandler(this.t_TICK);  
            //t.Start();          
        } 
         
        private void t_TICK(object sender, EventArgs e)  
        {  
            //pen  
            p = new Pen(Color.Green, 1f);  

            //graphics  
            g = Graphics.FromImage(bmp);  

            //calculate x, y coordinate of HAND  
            int tu = (u - lim) % 360;  

            if (u >= 0 && u <= 180)  
            {  
                //right half  
                //u in degree is converted into radian.  

                x = cx + (int)(Hand * Math.Sin(Math.PI * u / 180));  
                y = cy - (int)(Hand * Math.Cos(Math.PI * u / 180));  
            }  
            else  
            {  
                x = cx - (int)(Hand * -Math.Sin(Math.PI * u / 180));  
                y = cy - (int)(Hand * Math.Cos(Math.PI * u / 180));  
            }  


            if (tu >= 0 && u <= 180)  
            {  
                tx = cx + (int)(150 * Math.Sin(Math.PI * tu / 180));  
                ty = cy - (int)(150 * Math.Cos(Math.PI * tu / 180));  
            }  
            else  
            {  
                tx = cx - (int)(150 * -Math.Sin(Math.PI * tu / 180));  
                ty = cy - (int)(150 * Math.Cos(Math.PI * tu / 180));  
            }  

            // draw circle  
            g.DrawEllipse(p, 0, 0, width, height); //bigger circle  
//            g.DrawEllipse(p, 80, 80, width - 160, height - 160); //smaller circle  

            // draw perpendicular line  
//            g.DrawLine(p, new Point(cx, 0), new Point(cx, height)); // UP-DOWN  
//            g.DrawLine(p, new Point(cy, 0), new Point(cy, width)); //LEFT-RIGHT  

            //draw HAND  
//            g.DrawLine(new Pen(Color.White, 1f), new Point(cx, cy), new Point(tx, ty));  
            g.DrawLine(p, new Point(cx, cy), new Point(x, y)); 
            if (serialPort1.IsOpen) 
            { 
                g.DrawLine(new Pen(Color.Red, 1f), new Point(x-1, y-1), new Point(x, y)); 
            } 
            // load bitmap in picturebox1  
             

            // 해제 
            p.Dispose();  
            g.Dispose(); 

            //update              
            if (u == 360)             
            { 
                u = 0; 
                measure_flag = 0; 
                bmp = new Bitmap(width + 1, height + 1); 
                serialPort1.Write("0"); 
            }else 
                pictureBox1.Image = bmp;  
        } 

        private void btnOpen_Click(object sender, EventArgs e) 
        { 
            try 
            { 
                serialPort1.PortName = cBoxCOMPORT.Text; 
                serialPort1.BaudRate = Convert.ToInt32(cBoxBaudRate.Text); 
                serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived); 

                serialPort1.Open(); 
                progressBar1.Value = 100; 
            } 

            catch (Exception err) 
            { 
                MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
            } 
        } 

        private void btnCLose_Click_1(object sender, EventArgs e) 
        { 
            if (serialPort1.IsOpen) 
            { 
                serialPort1.Close(); 
                progressBar1.Value = 0; 
            } 
        } 
        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)     //시리얼 데이터 읽기 
        { 
            if (measure_flag == 1) 
            { 
                dataIN = serialPort1.ReadExisting(); 
                try 
                { 
                    number1 = System.Convert.ToDouble(dataIN); 
                } 
                catch (FormatException) 
                { 
                } 
                catch (OverflowException) 
                { 
                } 
                Hand = (int)number1;    //그리기-값                    
                 
                serialPort1.Write("1"); 

                this.Invoke(new EventHandler(ShowData)); 
                this.Invoke(new EventHandler(t_TICK)); 
                 
                u++; 
            } 

        } 
        private void ShowData(object sender, EventArgs e)   //Data 출력 
        {             
            this.label3.Text = dataIN;                         
        } 

        private void Measure_Click(object sender, EventArgs e) 
        { 
            measure_flag = 1; 
            serialPort1.Write("1"); 
        } 
    } 
}

 

#define H 1
#define L 0
#define trigpin 11
#define echopin 12
#define delay_time 10

int Motor[4] = {4,5,6,7};
int M_En[2] = {8,9};
int i, deg = 0;
int flag = 0;
float duration, distance;  

void setup() {
  for (i=0;i<4;i++){
    pinMode(Motor[i], OUTPUT);
  }
  for (i=0;i<2;i++){
    pinMode(M_En[i], OUTPUT);
  }  
  pinMode(trigpin, OUTPUT);   //Trig
  pinMode(echopin, INPUT);   //Echo 
  Serial.begin(115200); //보레이트 설정
}

void M_Enable(){
  for (i=0;i<2;i++){
    digitalWrite(M_En[i], H);
  }  
}

void M_Disable(){
  for (i=0;i<2;i++){
    digitalWrite(M_En[i], L);
  } 
}

void UltraSonic_command(){
/////////////////////////UltraSonic 동작 및 거리계산
////////////////////////////////////////////////// 대충 검색해서 퍼옴 - 구지... 이해할필요까지.. 없어보임.
  digitalWrite(trigpin, H);
  delay(10);
  digitalWrite(trigpin, L);
  duration = pulseIn(echopin, H);
  distance = ((float)(340*duration) / 10000) /2 ;
/////////////////////////시리얼로 프린트? TX 구문인지... 
  //Serial.print("distance : ");
  Serial.println(distance);
}

// 한스텝 동작 -> 거리측정 -> Serial Send
void M_move(int a, int b, int c, int d){
  M_Enable();
  digitalWrite(4, a);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(5, b);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(6, c);   // turn the LED on (HIGH is the voltage level)
  digitalWrite(7, d);   // turn the LED on (HIGH is the voltage level) 
  UltraSonic_command();
  M_Disable();  
}

// 오른쪽 동작
void r_Stepping(){  
  M_move(H,L,L,H);  
  delay(delay_time);                       // wait for a second
  M_move(H,H,L,L);  
  delay(delay_time);                       // wait for a second
  M_move(L,H,H,L);  
  delay(delay_time);                       // wait for a second
  M_move(L,L,H,H);  
  delay(delay_time);                       // wait for a second
}

// 왼쪽 동작
void l_Stepping(){  
  M_move(L,L,H,H);  
  delay(delay_time);                       // wait for a second
  M_move(L,H,H,L);    
  delay(delay_time);                       // wait for a second
  M_move(H,H,L,L);  
  delay(delay_time);                       // wait for a second
  M_move(H,L,L,H);  
  delay(delay_time);                       // wait for a second
}

// 오른쪽 360도 -> 왼쪽 360도 
void loop() { 
  if(Serial.available()>0){
    flag = Serial.read();
  }  
  if(flag){
    l_Stepping();  
    if(deg < 52)    
      deg = deg + 1; 
    else {
      flag = 0;          
      deg = 0;
    }
  }
}

+ Recent posts