找回密码
 马上注册

QQ登录

只需一步,快速开始

查看: 4203|回复: 5

求助:用两个触动来控制程序的问题

[复制链接]
发表于 2013-7-1 10:41:43 | 显示全部楼层 |阅读模式
想编写一个程序,实现目的如下:机器人有两个触动传感器,分别是S1和S4,有数个子程序,当按动S1时程序开始运行,并且当第一个子程序运行完毕时按动S1运行下一个子程序(此目的已经实现),当某个子程序在运行中时,按动S4程序停止,如果此时再按动S1重复此前被停止的程序!我试过几种方法但可以停止马达但无法从相应子程序中跳出。
程序如下:
#pragma config(Sensor, S1,     touch_right,    sensorTouch)
#pragma config(Sensor, S2,     light,          sensorLightActive)
#pragma config(Sensor, S4,     touch_left,     sensorTouch)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
int n;
void test_0()
{
        while(nMotorEncoder[motorB]<1000)
        {
        motor[motorB]=50;
        motor[motorC]=50;
  }

        motor[motorB]=0;
        motor[motorC]=0;
}
void test_1()
{
        motor[motorB]=50;
        motor[motorC]=0;
        wait1Msec(1000);
        motor[motorB]=0;
        motor[motorC]=0;
}
void test_2()
{
        motor[motorB]=0;
        motor[motorC]=50;
        wait1Msec(1000);
        motor[motorB]=0;
        motor[motorC]=0;
}
void test_3()
{
        motor[motorB]=50;
        motor[motorC]=-50;
        wait1Msec(1000);
        motor[motorB]=0;
        motor[motorC]=0;
}
//****************************************************************************//
task sp0()
{
        n=0;
        while(true)
        {
                if(SensorValue(S1)==1)
                {
                n=n+1;
                switch (n)
                {
                        case 1:
                        {
                                wait1Msec(100);
                                test_0();
                                break;

                        }
                        case 2:
                        {
                                wait1Msec(100);
                                test_1();
                                break;
                        }
                        case 3:
                        {
                                wait1Msec(100);
                                test_2();
                                break;
                        }
                        case 4:
                        {
                                wait1Msec(100);
                                test_3();
                                break;
                        }
           }
                }
                else
                {
                        wait1Msec(1);
                }
        }
}
task sp1()
{
        while(true)
        {
                while(SensorValue(S4)==1)
                {
                motor[motorB]=0;
                motor[motorC]=0;


                }
        }
}
task main()
{
        StartTask(sp0);
        StartTask(sp1);

        while(true)
        {

        }
}

















如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
发表于 2013-7-1 11:27:19 | 显示全部楼层
修改你的test1、2、3代码,需要时间长的地方设置轮询flag比如你的等待时间,等待角度。
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

 楼主| 发表于 2013-7-1 13:10:16 | 显示全部楼层
本帖最后由 fish 于 2013-7-1 13:14 编辑

TESE 1,2,3只是实验,实际中不可能那样简单,如果是十分复杂的一个程序我担心连程序都跳不出来!而且task sp1 ()这个程序也有问题,就是说它可以把马达停止 ,但也就是停止马达而不是跳出程序,我试过用stoptask()这个命令结果是把task sp0 ()给跳出来了,这不是我的目的,目的是跳出重新判断!
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

 楼主| 发表于 2013-7-5 21:30:15 | 显示全部楼层
现在倒是可以跳出了,但无法到刚刚跳出的位置重新开始,很是不解!
程序如下:
#pragma config(Sensor, S1,     touch_right,    sensorTouch)
#pragma config(Sensor, S2,     light,          sensorLightActive)
#pragma config(Sensor, S4,     touch_left,     sensorTouch)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//
int n,i,m;

void test_0()
{
        nMotorEncoder[motorB]=0;
        nMotorEncoder[motorC]=0;
        while(nMotorEncoder[motorB]<1000)
        {
        motor[motorB]=50;
        motor[motorC]=50;
  }

        motor[motorB]=0;
        motor[motorC]=0;
}
void test_1()
{
        nMotorEncoder[motorB]=0;
        nMotorEncoder[motorC]=0;
        while(nMotorEncoder[motorB]<1000)
        {
        motor[motorB]=50;
        motor[motorC]=0;
   }
        motor[motorB]=0;
        motor[motorC]=0;
}
void test_2()
{
        motor[motorB]=0;
        motor[motorC]=50;
        wait1Msec(3000);
        motor[motorB]=0;
        motor[motorC]=0;
}
void test_3()
{
        motor[motorB]=50;
        motor[motorC]=-50;
        wait1Msec(3000);
        motor[motorB]=0;
        motor[motorC]=0;
}
//****************************************************************************//
task sp0()
{

        while(true)
        {
                if(SensorValue(S1)==1)
                {
                n=n+1;

                switch (n)
                {
                        case 1:
                        {
                                wait1Msec(100);
                                test_0();
                                break;

                        }
                        case 2:
                        {
                                wait1Msec(100);
                                test_1();
                                break;
                        }
                        case 3:
                        {
                                wait1Msec(100);
                                test_2();
                                break;
                        }
                        case 4:
                        {
                                wait1Msec(100);
                                test_3();
                                break;
                        }
           }
                }
                else
                {
                        wait1Msec(1);
                }
        }
}
//**********************************************************//
task sp1()
{

        while(true)
        {
        i=0;
                if(SensorValue(S4)==1)
                {
                i++;
    m++;
          }

        if(i==1)
        {
                motor[motorB]=0;
   motor[motorC]=0;

    StopTask(sp0);
    wait1Msec(10);
    n=n-1;
           StartTask(sp0);

}


        }
}
//****************************************************//
task main()
{
n=0;m=0;
        StartTask(sp0);
        StartTask(sp1);

        while(true)
        {

        }
}
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

 楼主| 发表于 2013-7-5 21:32:07 | 显示全部楼层
就是想用N来控制具体到那个位置能停和开始!不过好像不大好使
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

发表于 2013-7-16 17:17:07 | 显示全部楼层
你是想做个暂停吗?
如果您觉得我的帖子对您有用,请不吝给我一个“赞”!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|中文乐高 ( 桂ICP备13001575号-7 )

GMT+8, 2024-5-2 01:41 , Processed in 0.102281 second(s), 19 queries .

Powered by Discuz! X3.5

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表