To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.robotics.rcx.nqcOpen lugnet.robotics.rcx.nqc in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / RCX / NQC / 598
597  |  599
Subject: 
Re: NQC 2.2 a1 available for public test
Newsgroups: 
lugnet.robotics.rcx.nqc
Date: 
Mon, 8 May 2000 17:26:27 GMT
Viewed: 
1678 times
  
Working code using a counter.

/*
*  lstop8.nqc - Line and Wall avoider
*
*  Copyright (C) 2000 James Pritchard
*
*  Date: 08-May 2000
*
*
*  The contents of this file are subject to the Mozilla Public License
*  Version 1.0 (the "License"); you may not use this file except in
*  compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
*
*  Software distributed under the License is distributed on an "AS IS"
*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
*  License for the specific language governing rights and limitations
*  under the License.
*
*  Thanks to Dave Baum for creating such an excelent compiler and language.
*
*  All Rights Reserved.
*/

/*
* This program avoides dark lines and walls. Everytime the program
* encounters a line or wall the counter is increased. I can then setup an
* event to monitor the counter, so that it doesn't go on for ever.
*/


void Sleep(const int m)  { asm { 0xB1, m }; } // Inactive time out after m
minutes 0=never


#define sens 10
#define nsens -10
#define speed 7
#define reversetime 150
#define turntime 200

#define COUNTER_1 0

int count;  // We must initate the first global varible as the counter or
else nqc will use the counter as a varible. We only need int one counter
because we only use the first count 0.
int rl1;
int rl2;
int rlavg;
int orlavg;
int statlight;
int stattouch;
int whr;

task main()
{
        Sleep(0);  // Never turn off
        ClearCounter(COUNTER_1);
        SetUserDisplay(Counter(COUNTER_1), 0);
        SelectDisplay(DISPLAY_USER);
        SetSensorType(SENSOR_1, SENSOR_TYPE_TOUCH);
        SetSensorMode(SENSOR_1, SENSOR_MODE_BOOL);
        SetSensorType(SENSOR_2, SENSOR_TYPE_TOUCH);
        SetSensorMode(SENSOR_2, SENSOR_MODE_BOOL);
        SetSensorType(SENSOR_3, SENSOR_TYPE_LIGHT);
        SetSensorMode(SENSOR_3, SENSOR_MODE_RAW);
        SetDirection(OUT_A + OUT_C, OUT_FWD);
        SetPower(OUT_A + OUT_C, speed);
        SetOutput(OUT_A + OUT_C, OUT_ON);
        start actionlight;
        start actiontouch;
start mlight;
        start monitorleft;
        start monitorright;
until (Counter(COUNTER_1) > 10);
int freq;
freq = 4000;
while (freq > 300) {
  freq -= 200;
  PlayTone(freq, 5);
}
stop actionlight;
stop actiontouch;
stop mlight;
stop monitorleft;
stop monitorright;
StopRCX();
}

task mlight()
{
       SetPriority(10);
       int temp;
temp = 1;
       while (true)
{
              rl2 = rl1;
              rl1 = SENSOR_3;
              rlavg = rl2;
              rlavg + rl1;
              rlavg / 2;
  if (temp == 1) {
   temp = 0;
   } else {
                        if (rlavg - orlavg > sens) { PlayTone(1100,5);
statlight = 0; } // On Dark
                        if (rlavg - orlavg < nsens) { PlayTone(990,5);
statlight = 1; } // On Light
                        }
  orlavg = rlavg;
}
}

task actionlight()
{
        SetPriority(40);
        while(true)
        {
                until (statlight == 0); // Wait until on dark
                    IncCounter(COUNTER_1);
                    Rev(OUT_A + OUT_C);
                until (statlight == 1); // Wait until on light
                    Wait(reversetime/2);
                    if (Random(10) > 5) {
                       Fwd(OUT_A);} else {
                       Fwd(OUT_C);}
                    Wait(turntime);
                    Fwd(OUT_A + OUT_C);
        }
}

task monitorleft()
{
        SetPriority(20);
        while(true)
        {
                until (SENSOR_1 == 1); // Wait until Sensor1 Pushed LEFT
                stattouch |= 1;
                IncCounter(COUNTER_1);
                until (SENSOR_1 == 0); // Wait until unpressed on LEFT
                stattouch &= 2;
        }
}

task monitorright()
{
        SetPriority(20);
        while(true)
        {
                until (SENSOR_2 == 1); // Wait until Sensor1 Pushed RIGHT
                stattouch |= 2;
                IncCounter(COUNTER_1);
                until (SENSOR_2 == 0); // Wait until unpressed on RIGHT
                stattouch &= 1;
        }
}

task actiontouch()
{
        SetPriority(30);
        while(true)
        {
                if (stattouch == 3) {
                   // Both pressed
                   PlayTone(880,5);
                   whr = 2;
                   backup();
                }
                if (stattouch == 1) {
                   // Left only pressed
                   PlayTone(440,5);
                   whr = 0;
                   backup();
                }
                if (stattouch == 2) {
                   // Right only pressed
                   PlayTone(660,5);
                   whr = 1;
                   backup();
                }
        }
}

sub backup()
{
  Rev(OUT_A + OUT_C);
  Wait(reversetime);
  if (whr == 0)
  {
  Fwd(OUT_A);
  Wait(turntime);
  }
  if (whr == 1)
  {
  Fwd(OUT_C);
  Wait(turntime);
  }
  if (whr == 2)
  {
    if (Random(10) > 5)
    {
        Fwd(OUT_A);
        Wait(Random(200));
    } else {
        Fwd(OUT_C);
        Wait(Random(200));
    }
  }
  Fwd(OUT_A + OUT_C);
}

sub StopRCX()
{
  Off(OUT_A + OUT_C);
  Fwd(OUT_A + OUT_C);
  Float(OUT_A + OUT_C);
}

James Pritchard <dungeon@madasafish.com> wrote in message
news:Fu8y2L.I1t@lugnet.com...
There is still a problem when nqc uses the first three global variables if
you also want to use a counter.
The current solution is to make the first int lines reflect the fact that
they will be used as counters.

#Define COUNTER_1 0
int count1
int count2
int count3
int varible1

varible1 = Counter(COUNTER_1)



Sorry if this sounds a bit strange I'll post some code when I find it.

Dave Baum <dbaum@spambgoneenteract.com> wrote in message
news:dbaum-5971D6.01155106052000@lugnet.com...
Preliminary NQC support for RCX 2.0 is now available in NQC version
2.2a1.  Its in the beta test area of the NQC site:
http://www.enteract.com/~dbaum/nqc

There have been a lot of changes internal to the compiler itself, and
RCX 2.0 support is only partially complete at the moment, but I figured
an early release would be better than nothing.

Dave Baum

--
reply to: dbaum at enteract dot com





Message is in Reply To:
  Re: NQC 2.2 a1 available for public test
 
There is still a problem when nqc uses the first three global variables if you also want to use a counter. The current solution is to make the first int lines reflect the fact that they will be used as counters. #Define COUNTER_1 0 int count1 int (...) (24 years ago, 8-May-00, to lugnet.robotics.rcx.nqc)

22 Messages in This Thread:









Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR