基于MSP430送药小车 ----- 拓展篇【2021年全国电赛(F题)】


该篇文章并非完全按照赛题要求完成,仅当做个人休闲娱乐产品!!!!

衔接上篇 基于MSP430送药小车 ----- 基础篇

该篇的主要内容为赛题的拓展部分,所以这里我就在完成基础部分的前提下继续介绍拓展部分。

本篇代码在上篇代码中做了许些修改(本人有点强迫症,看见bug就像修,修着修着就把许多代码改动了),但本质逻辑处理上都是一致的,所以想要追求完美,本人更推荐参考该篇的代码部分。

一、赛题分析

  • 两个小车协同运送药品到同一指定的中部病房。小车1识别病房号装载药品后开始运送,到达病房后等待卸载药品;然后,小车2识别病房号装载药品后启动运送,到达自选暂停点后暂停,点亮黄色指示灯,等待小车1卸载;小车1卸载药品,开始返回,同时控制小车2熄灭黄色指示灯并继续运送。要求从小车2启动运送开始,到小车1返回到药房且小车2到达病房的总时间(不包括小车2黄灯亮时的暂停时间)越短越好,超过60s 计0分。

  • 两个小车协同到不同的远端病房送、取药品,小车1送药,小车2取药。小车1识别病房号装载药品后开始运送,小车2于药房处识别病房号等待小车1的取药开始指令;小车1到达病房后卸载药品,开始返回,同时向小车2发送启动取药指令;小车2收到取药指令后开始启动,到达病房后停止,亮红色指示灯。要求从小车1返回开始,到小车1返回到药房且小车2到达取药病房的总时间越短越好,超过60s计0分。

  • 其他。

从上面赛题中可知,拓展任务需要俩辆车同时进行工作,车1和车2需将药品送到相同药房,中途相撞、暂停点距离过短、时间超过60s扣分,越快越好等等。

所以在该题中,俩辆车是需要有交互的,也就是通信,所以需要外加通信模块(蓝牙、Zigbee、LORA、WiFi等等),以上模块都可通过串口实现,且都可在预先在上位机配置连接后直接透传,以下我选择的是蓝牙进行通信,具体配置可参考“ HC-05 蓝牙模块之间的通信配置 ”。其次在第二辆车硬件方面,与其第一辆车相比,可以不用视觉模块(OpenMV、K210等等),具体送至哪个药房通过车1发送的指令获得信息,其他硬件、排线与车1保持一致即可。在软件上,代码也与车1大致相同,改动的地方为:外加串口初始化函数,用作蓝牙通信使用,其中相比上篇蓝牙收发消息体现在(以中端病房为例),当车1在去往中端药房的途中路径第一个十字路口(一共途径2个十字路口)时,发送消息给车2,这时车2接收到消息,开始装药 启动出发,在车1成功停靠在病房门口时,车2紧随其后,相继停靠在对面的中端药房,等待车1的下一步指令,当车1卸完药品后,途径回程中的第一个十字路口时,发送第二个指令给车2,车2接到指令立即原地调头(一开始停靠时车2的车头是面向病房的),然后开往目的药房,卸药回城。

Tips: 以下推荐几篇上述几种通信模块简介,仅供参考!

  1. HC-05 蓝牙
  2. ESP8266WiFi
  3. LORA 无线通信
  4. Zigbee 无线通信

二、逻辑处理分析

1. 近端病房

如上赛题分析所说,拓展部分相比较于基础部分,不大相同的地方为:由原来的一辆车送药变为俩辆车一起送药,小车之间通过无线串口通信模块进行数据传输,其余地方基本无太大变化。

车2设计如下:
在车1启动之后,车2在药房门前保持就绪状态,当车1途径第一个十字路口(也就是第一个转向口)时,发送启动指令给车2,人工装药后启动,车1会在病房前停止等待人工卸药,而车2会去往另一个近端病房,避免俩车会车(相撞),当车1卸药完成后,回程途径第一个十字路口时,发送第二个指令给车2,车2接收到指令立即回转直奔目的病房,随后按照以往操作,卸药完成后返回药房。

在近端送药时与一辆车相比,其实就单单多了俩条接收语句,简单来说,当车2接收到出发指令,将其原来在第一个十字路口左拐的指令改成右拐,且又继续等待车1的指令,当车1再一次发送指令过来时,车2调头途径十字路口时无需做任何操作,按照默认的循迹即可,那么当途径十字路口段时该怎么处理呢?即在接收到第二条指令的同时设立标志位,用作无视第一个十字路口段,当成功将药品送到指令病房后,又将标志位设成原来的值,即按照最开始的单量小车送药模式执行。

2. 中端病房

中端病房相比较于近端病房,从赛题中就可以注意到只是多了一个岔路口,也就是近端病房在第一个岔路口转向,而中端病房在第二个岔路口转向,具体设计如下。

车2设计如下:
当车1识别目的病房后启动,在途径第一个岔路口的时候就可以给车2发送出发指令,因为车1、车2的循迹速度都是保持一致的,且路况和其他因素也相差无几,所以车1在这时发送指令驱动车2送药,距离和时间上都是安全的,车1和车2都通过设立标志位忽略第一条岔路口,保持巡线状态,直到俩车都到达对应的停靠点,车1卸药完成后立即原路返回,车2则继续去往指定病房,随即同样安装车1的轨迹回程。

3. 远端病房

远端病房则相比较于近端、中端稍难一点,不过没关系,只要功夫深,铁杵都能磨成针,所以不要急,请听我一一道来。

下面我以5号病号为例(假如它位于赛题左上方处)
首先设定俩个标志位flag1和flag2,用作去程和回程的路向判断,每去往病房经过一个十字路口时flag1变换状态,当flag1第一次变换时车1发送第一条指令给车2,车2接收到消息马上装药启程,按照同车1一样的速度向目的药房行驶,当flag1变换到第三种状态时车1向左继续行驶,且标志位继续变化,再当标志位发生变化时向右行驶,随即到达指定药房处,而当flag1发生第四次变化时车2理当向右行驶,但考虑到车1卸完操作并未完成,所以选择在5号对面的药房作为临时停靠点,即在flag1发生第四次变化时车2向左行驶,等待车1第二个指令的到来。当车1卸完药品后回程途径第一个岔路口发送第二条指令给车2,车2接收到指令立即调头直行抵达目的病房,后面再安照以往操作正常回程。相比病房5,其他远端病房6,7,8都与5号在逻辑设计上一毛一样,只是路段的转向不同。

Tips: 在实现多功能情况下做项目开发时,切记不要开启低功耗模式(除非在功耗方面上有过多要求),不然会导致程序阻塞,反应迟钝等问题;在实现单功能情况下,如果对功耗也没过多要求,像我一样的新手可以关闭低功耗,直接使用,简单粗暴。

三、程序设计

1. 近端送药

① 车1

void judge1(void)
{
    
    
    xunji();

    if(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3))
    {
    
    
        while(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3));
        if(flag1==-100)
        {
    
    
            UCA1TXBUF='a';
            right();
            SetPwm_Init(24,1000,700);//左边
            SetPwm_Init(25,1000,700);//右边
            delay_us(300000);
        }
        flag1++;
        if(flag1==1)
        {
    
    
            left();
            SetPwm_Init(24,1000,700);//左边
            SetPwm_Init(25,1000,700);//右边
            delay_us(300000);
        }
        while(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3));
    }

    if(((P6IN&BIT1)!=BIT1) && ((P6IN&BIT2)!=BIT2) && ((P6IN&BIT3)!=BIT3) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)!=BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)==BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)!=BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)))
    {
    
    
        if( pill==0 && ((P7IN&BIT4)==BIT4) )     //拿走药品
        {
    
    
            pill=1;
            back();
            SetPwm_Init(24,1000,400);//左边
            SetPwm_Init(25,1000,400);//右边
            delay_us(700000);
            left();
            SetPwm_Init(24,1000,550);//左边
            SetPwm_Init(25,1000,650);//右边
            delay_us(700000);
            flag1=-100;
            while(((P6IN&BIT1)!=BIT1) && ((P6IN&BIT2)!=BIT2) && ((P6IN&BIT3)!=BIT3) && (((P7IN&BIT0)!=BIT0)||((P6IN&BIT5)!=BIT5)) && (((P6IN&BIT0)!=BIT0)||((P6IN&BIT4)!=BIT4)));

        }
        else
        {
    
    
            stop();
            SetPwm_Init(24,1000,0);//左边
            SetPwm_Init(25,1000,0);//右边
        }
    }

}

② 车2

void judge1(void)
{
    
    
    xunji();

    if(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3))
    {
    
    
        while(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3));
        if(flag1==-100)
        {
    
    
            right();
            SetPwm_Init(24,1000,700);//左边
            SetPwm_Init(25,1000,700);//右边
            delay_us(300000);
        }
        flag1++;
        if(flag1==1)
        {
    
    
            right();
            SetPwm_Init(24,1000,700);//左边
            SetPwm_Init(25,1000,700);//右边
            delay_us(300000);
        }
        while(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3));
    }

    if(((P6IN&BIT1)!=BIT1) && ((P6IN&BIT2)!=BIT2) && ((P6IN&BIT3)!=BIT3) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)!=BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)==BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)!=BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)))
    {
    
    

        if( pill==0 && ((P7IN&BIT4)==BIT4) )     //拿走药品
        {
    
    
            pill=1;
            back();
            SetPwm_Init(24,1000,400);//左边
            SetPwm_Init(25,1000,400);//右边
            delay_us(700000);
            left();
            SetPwm_Init(24,1000,550);//左边
            SetPwm_Init(25,1000,650);//右边
            delay_us(700000);
            flag1=-100;
            while(((P6IN&BIT1)!=BIT1) && ((P6IN&BIT2)!=BIT2) && ((P6IN&BIT3)!=BIT3) && (((P7IN&BIT0)!=BIT0)||((P6IN&BIT5)!=BIT5)) && (((P6IN&BIT0)!=BIT0)||((P6IN&BIT4)!=BIT4)));
        }
        else
        {
    
    
            stop();
            SetPwm_Init(24,1000,0);//左边
            SetPwm_Init(25,1000,0);//右边
            if(((P6IN&BIT1)!=BIT1) && ((P6IN&BIT2)!=BIT2) && ((P6IN&BIT3)!=BIT3) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)!=BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)==BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)!=BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)))
            {
    
    
                UCA1IE |= UCRXIE;
            }
            else
            {
    
    
                UCA1IE &= ~UCRXIE;
            }
        }
    }

}

2. 中端送药

① 车1

void judge3(void)
{
    
    
    xunji();

    if(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3))
    {
    
    
        while(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3));
        if(flag3==-100)
        {
    
    
            right();
            SetPwm_Init(24,1000,700);//左边
            SetPwm_Init(25,1000,700);//右边
            delay_us(300000);
            UCA1TXBUF='a';
        }
        flag3++;
        if(flag3==1)    
		{
    
    
			delay_us(150000);
		}
        if(flag3>=2&& flag3<=6)
        {
    
    
            flag3=10;
            left();
            SetPwm_Init(24,1000,700);//左边
            SetPwm_Init(25,1000,700);//右边
            delay_us(300000);
        }
        while(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3));
    }

    if(((P6IN&BIT1)!=BIT1) && ((P6IN&BIT2)!=BIT2) && ((P6IN&BIT3)!=BIT3) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)!=BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)==BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)!=BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)))
    {
    
    
        if( pill==0 && ((P7IN&BIT4)==BIT4) )     //拿走药品
        {
    
    
            pill=1;
            back();
            SetPwm_Init(24,1000,400);//左边
            SetPwm_Init(25,1000,400);//右边
            delay_us(700000);
            left();
            SetPwm_Init(24,1000,550);//左边
            SetPwm_Init(25,1000,650);//右边
            delay_us(700000);
            flag3=-100;
            while(((P6IN&BIT1)!=BIT1) && ((P6IN&BIT2)!=BIT2) && ((P6IN&BIT3)!=BIT3) && (((P7IN&BIT0)!=BIT0)||((P6IN&BIT5)!=BIT5)) && (((P6IN&BIT0)!=BIT0)||((P6IN&BIT4)!=BIT4)));
        }
        else
        {
    
    
            stop();
            SetPwm_Init(24,1000,0);//左边
            SetPwm_Init(25,1000,0);//右边
        }
    }

}

② 车2

void judge3(void)
{
    
    
    xunji();

    if(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3))
    {
    
    
        while(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3));
        if(flag3==-100)
        {
    
    
            right();
            SetPwm_Init(24,1000,700);//左边
            SetPwm_Init(25,1000,700);//右边
            delay_us(300000);
        }
        flag3++;
        if(flag3==1)    delay_us(150000);
        if(flag3>=2&& flag3<=6)
        {
    
    
            flag3=10;
            right();
            SetPwm_Init(24,1000,700);//左边
            SetPwm_Init(25,1000,700);//右边
            delay_us(300000);
        }
        while(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3));
    }

    if(((P6IN&BIT1)!=BIT1) && ((P6IN&BIT2)!=BIT2) && ((P6IN&BIT3)!=BIT3) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)!=BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)==BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)!=BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)))
    {
    
    
        if( pill==0 && ((P7IN&BIT4)==BIT4) )     //拿走药品
        {
    
    
            pill=1;
            back();
            SetPwm_Init(24,1000,400);//左边
            SetPwm_Init(25,1000,400);//右边
            delay_us(700000);
            left();
            SetPwm_Init(24,1000,550);//左边
            SetPwm_Init(25,1000,650);//右边
            delay_us(700000);
            flag3=-100;
            while(((P6IN&BIT1)!=BIT1) && ((P6IN&BIT2)!=BIT2) && ((P6IN&BIT3)!=BIT3) && (((P7IN&BIT0)!=BIT0)||((P6IN&BIT5)!=BIT5)) && (((P6IN&BIT0)!=BIT0)||((P6IN&BIT4)!=BIT4)));
        }
        else
        {
    
    
            stop();
            SetPwm_Init(24,1000,0);//左边
            SetPwm_Init(25,1000,0);//右边
            if(((P6IN&BIT1)!=BIT1) && ((P6IN&BIT2)!=BIT2) && ((P6IN&BIT3)!=BIT3) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)!=BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)==BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)!=BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)))
            {
    
    
                UCA1IE |= UCRXIE;
            }
            else
            {
    
    
                UCA1IE &= ~UCRXIE;
            }
        }
    }

}

3. 远端送药

① 车1

void judge6(void)
{
    
    
    xunji();

    if(flag66!=0)
    {
    
    
        if(((P6IN&BIT5)==BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)!=BIT0) && (((P6IN&BIT2)==BIT2)||((P6IN&BIT1)==BIT1))&&flag66==1)
        {
    
    
            while(((P6IN&BIT5)==BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)!=BIT0) && (((P6IN&BIT2)==BIT2)||((P6IN&BIT1)==BIT1)));
            left();
            SetPwm_Init(24,1000,700);//左边
            SetPwm_Init(25,1000,700);//右边
            delay_us(300000);
            UCA1TXBUF='a';
            flag66++;
        }
        if(((P6IN&BIT5)==BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)!=BIT0) && (((P6IN&BIT2)==BIT2)||((P6IN&BIT1)==BIT1))&&flag66==2)
        {
    
    
            while(((P6IN&BIT5)==BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)!=BIT0) && (((P6IN&BIT2)==BIT2)||((P6IN&BIT1)==BIT1)));
            left();
            SetPwm_Init(24,1000,700);//左边
            SetPwm_Init(25,1000,700);//右边
            delay_us(300000);
            flag66=0;
        }
    }

    if(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3))
    {
    
    
        while(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3));
        flag6++;
        if(flag6==1 || flag6==2)    delay_us(150000);
        if(flag6>=3&& flag6<=6)
        {
    
    
            flag6=10;
            right();
            SetPwm_Init(24,1000,700);//左边
            SetPwm_Init(25,1000,700);//右边
            delay_us(300000);
        }
        if(flag6>10)
        {
    
    
            flag6=-100;
            right();
            SetPwm_Init(24,1000,700);//左边
            SetPwm_Init(25,1000,700);//右边
            delay_us(300000);
        }
        while(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3));

    }

    if(((P6IN&BIT1)!=BIT1) && ((P6IN&BIT2)!=BIT2) && ((P6IN&BIT3)!=BIT3) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)!=BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)==BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)!=BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)))
    {
    
    
        if( pill==0 && ((P7IN&BIT4)==BIT4) )     //拿走药品
        {
    
    
            pill=1;
            back();
            SetPwm_Init(24,1000,400);//左边
            SetPwm_Init(25,1000,400);//右边
            delay_us(700000);
            left();
            SetPwm_Init(24,1000,550);//左边
            SetPwm_Init(25,1000,650);//右边
            delay_us(700000);
            while(((P6IN&BIT1)!=BIT1) && ((P6IN&BIT2)!=BIT2) && ((P6IN&BIT3)!=BIT3) && (((P7IN&BIT0)!=BIT0)||((P6IN&BIT5)!=BIT5)) && (((P6IN&BIT0)!=BIT0)||((P6IN&BIT4)!=BIT4)));
            flag66=1;
        }
        else
        {
    
    
            stop();
            SetPwm_Init(24,1000,0);//左边
            SetPwm_Init(25,1000,0);//右边
        }
    }
}


② 车2

void judge6(void)
{
    
    
    xunji();

    if(flag66!=0)
    {
    
    
        if(((P6IN&BIT5)==BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)!=BIT0) && (((P6IN&BIT2)==BIT2)||((P6IN&BIT1)==BIT1))&&flag66==1)
        {
    
    
            while(((P6IN&BIT5)==BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)!=BIT0) && (((P6IN&BIT2)==BIT2)||((P6IN&BIT1)==BIT1)));
            left();
            SetPwm_Init(24,1000,700);//左边
            SetPwm_Init(25,1000,700);//右边
            delay_us(300000);
            flag66++;
        }
        if(((P6IN&BIT5)==BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)!=BIT0) && (((P6IN&BIT2)==BIT2)||((P6IN&BIT1)==BIT1))&&flag66==2)
        {
    
    
            while(((P6IN&BIT5)==BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)!=BIT0) && (((P6IN&BIT2)==BIT2)||((P6IN&BIT1)==BIT1)));
            left();
            SetPwm_Init(24,1000,700);//左边
            SetPwm_Init(25,1000,700);//右边
            delay_us(300000);
            flag66=0;
        }
    }

    if(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3))
    {
    
    
        while(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3));
        flag6++;
        if(flag6==1 || flag6==2)    delay_us(150000);
        if(flag6>=3&& flag6<=6)
        {
    
    
            flag6=10;
            right();
            SetPwm_Init(24,1000,700);//左边
            SetPwm_Init(25,1000,700);//右边
            delay_us(300000);
        }
        if(flag6>10)
        {
    
    
            flag6=-100;
            left();
            SetPwm_Init(24,1000,700);//左边
            SetPwm_Init(25,1000,700);//右边
            delay_us(300000);
        }
        while(((P6IN&BIT1)==BIT1)  && ((P6IN&BIT2)==BIT2) && ((P6IN&BIT3)==BIT3));

    }

    if(((P6IN&BIT1)!=BIT1) && ((P6IN&BIT2)!=BIT2) && ((P6IN&BIT3)!=BIT3) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)!=BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)==BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)!=BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)))
    {
    
    
        if( pill==0 && ((P7IN&BIT4)==BIT4) )     //拿走药品
        {
    
    
            pill=1;
            back();
            SetPwm_Init(24,1000,400);//左边
            SetPwm_Init(25,1000,400);//右边
            delay_us(700000);
            left();
            SetPwm_Init(24,1000,550);//左边
            SetPwm_Init(25,1000,650);//右边
            delay_us(700000);
            while(((P6IN&BIT1)!=BIT1) && ((P6IN&BIT2)!=BIT2) && ((P6IN&BIT3)!=BIT3) && (((P7IN&BIT0)!=BIT0)||((P6IN&BIT5)!=BIT5)) && (((P6IN&BIT0)!=BIT0)||((P6IN&BIT4)!=BIT4)));
            flag66=1;
        }
        else
        {
    
    
            stop();
            SetPwm_Init(24,1000,0);//左边
            SetPwm_Init(25,1000,0);//右边
            if(((P6IN&BIT1)!=BIT1) && ((P6IN&BIT2)!=BIT2) && ((P6IN&BIT3)!=BIT3) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)) || (((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)!=BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0))||(((P6IN&BIT5)!=BIT5)  && ((P6IN&BIT0)==BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)!=BIT2) &&((P6IN&BIT3)==BIT3) &&((P6IN&BIT4)!=BIT4)&&((P7IN&BIT0)==BIT0))||(((P6IN&BIT5)==BIT5)  && ((P6IN&BIT0)!=BIT0) &&((P6IN&BIT1)==BIT1)&&((P6IN&BIT2)==BIT2) &&((P6IN&BIT3)!=BIT3) &&((P6IN&BIT4)==BIT4)&&((P7IN&BIT0)!=BIT0)))
            {
    
    
                UCA1IE |= UCRXIE;
            }
            else
            {
    
    
                UCA1IE &= ~UCRXIE;
            }
        }
    }
}

4. 通信处理

①蓝牙

  • 车1
void blue_init(void){
    
    
    P4SEL |=BIT4+BIT5 ;                             // P4.5 P4.4 = USCI_A1 TXD/RXD
    UCA1CTL1 |= UCSWRST;                      // **Put state machine in reset**
    UCA1CTL1 |= UCSSEL_2;                     // SMCLK
    UCA1BR0 = 9;                              // 1MHz 115200 (see User's Guide)
    UCA1BR1 = 0;                              // 1MHz 115200
    UCA1MCTL |= UCBRS_1 + UCBRF_0;            // Modulation UCBRSx=1, UCBRFx=0
    UCA1CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
    UCA1IE &= ~UCRXIE;                         // 关闭UART1的接收中断
    __enable_interrupt(); // 开启全局中断
}
  • 车2
#pragma vector=USCI_A1_VECTOR
__interrupt void USCI_A1_ISR(void)
{
    
    
  switch(__even_in_range(UCA1IV,4))
  {
    
    

  case 0:     
      break;                             // Vector 0 - no interrupt
  case 2:                                   // Vector 2 - RXIFG  接受中断

   while (!(UCA1IFG&UCTXIFG));    // USCI_A1 TX buffer ready?   UCTXIFG(USCI Transmit Interrupt Flag)
                                   //等待数据发送完成 完成UCTXIFG置1 跳出循环
   rx_flag++;
   if(rx_flag==11)
   {
    
    
       UCA1IE &= ~UCRXIE;   //关闭蓝牙接收
       back();
       SetPwm_Init(24,1000,400);//左边
       SetPwm_Init(25,1000,400);//右边
       delay_us(700000);
       left();
       SetPwm_Init(24,1000,550);//左边
       SetPwm_Init(25,1000,650);//右边
       delay_us(700000);
   }
   if(rx_flag==2)
   {
    
    
       rx_flag=10;
       if(UCA1RXBUF=='2'){
    
    
           find_num=2;
       }
       if(UCA1RXBUF=='4'){
    
    
           find_num=4;
       }
       if(UCA1RXBUF=='6'){
    
    
           find_num=6;
       }
       if(UCA1RXBUF=='8'){
    
    
           find_num=8;
       }
       if(UCA1RXBUF=='5'){
    
    
           find_num=5;
       }
       if(UCA1RXBUF=='1'){
    
    
           find_num=1;
       }
       if(UCA1RXBUF=='3'){
    
    
           find_num=3;
       }
       if(UCA1RXBUF=='7'){
    
    
           find_num=7;

       }
       UCA1IE &= ~UCRXIE;   //关闭蓝牙接收
   }

   break;
  case 4:
      break;                             // Vector 4 - TXIFG  发送中断

  default: break;
  }
}

②OpenMV

void openmv_init(void){
    
    
    P3SEL |=BIT3+BIT4 ;                             // P4.5 P4.4 = USCI_A1 TXD/RXD
    UCA0CTL1 |= UCSWRST;                      // **Put state machine in reset**
    UCA0CTL1 |= UCSSEL_2;                     // SMCLK
    UCA0BR0 = 9;                              // 1MHz 115200 (see User's Guide)
    UCA0BR1 = 0;                              // 1MHz 115200
    UCA0MCTL |= UCBRS_1 + UCBRF_0;            // Modulation UCBRSx=1, UCBRFx=0
    UCA0CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
    UCA0IE |= UCRXIE;                         // Enable USCI_A1 RX interrupt
    __enable_interrupt(); // 开启全局中断
}

#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
    
    

  switch(__even_in_range(UCA0IV,4))
  {
    
    

  case 0:     
      break;                             // Vector 0 - no interrupt
  case 2:                                   // Vector 2 - RXIFG  接受中断

   while (!(UCA0IFG&UCTXIFG));    // USCI_A1 TX buffer ready?   UCTXIFG(USCI Transmit Interrupt Flag)
                                   //等待数据发送完成 完成UCTXIFG置1 跳出循环

   if(UCA0RXBUF=='2'){
    
    
       find_num=2;
       UCA1TXBUF='2';
   }
   if(UCA0RXBUF=='4'){
    
    
       find_num=4;
       UCA1TXBUF='4';
   }
   if(UCA0RXBUF=='6'){
    
    
       find_num=6;
       UCA1TXBUF='6';
   }
   if(UCA0RXBUF=='8'){
    
    
       find_num=8;
       UCA1TXBUF='8';
   }
   if(UCA0RXBUF=='5'){
    
    
       find_num=5;
       UCA1TXBUF='5';
   }
   if(UCA0RXBUF=='1'){
    
    
       find_num=1;
       UCA1TXBUF='1';
   }
   if(UCA0RXBUF=='3'){
    
    
       find_num=3;
       UCA1TXBUF='3';
   }
   if(UCA0RXBUF=='7'){
    
    
       find_num=7;
       UCA1TXBUF='7';
   }

   UCA0IE &= ~(UCRXIE); 
   break;
  case 4:
      break;                       
  default: break;
  }
}

猜你喜欢

转载自blog.csdn.net/Dustinthewine/article/details/131152571