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 / 1829
1828  |  1830
Subject: 
Help me convert this train program to use events (NQC)
Newsgroups: 
lugnet.robotics, lugnet.robotics.rcx, lugnet.robotics.rcx.nqc, lugnet.trains
Followup-To: 
lugnet.robotics.rcx.nqc
Date: 
Mon, 28 Nov 2005 05:32:49 GMT
Viewed: 
231 times
  
For Christmas I’m setting up a LEGO train on top of a narrow wall, so it has to operate like a shuttle -- stop at each end and reverse motor direction. My first attempt had a train regulator attached to a polarity switch, with the switch being turned by a stepper motor I rigged up controlled by an RCX. When the train reached a “station” it would hit a touch sensor and the RCX would pulse the stepper motor, reversing the track polarity. This version works as a prototype, but I don’t have enough cords to reach the touch sensors I would have put at each “station” in the full layout.

So now I’m going to power the train motor directly from an RCX that rides along as one of the cars. I’ll use light sensors on each end of the train to look for white tiles placed between the track rails at each “station.”

Here’s my current program, which still uses touch sensors.

#define STATION1 SENSOR_1
#define STATION2 SENSOR_3
#define MOTOR OUT_B
#define __NOTETIME 8
#define __WAITTIME 10

task main()
{
    #ifdef __RCX
    SetSensor(STATION1, SENSOR_TOUCH);
    SetSensor(STATION2, SENSOR_TOUCH);

    // Only send a pulse of power to the  motor
    SetPower(MOTOR, OUT_HALF);
    #endif

    int count = 0;

    OnFwd(MOTOR);

    while(true)
    {
        if(STATION1 || STATION2)
        {
            reverseTrain();
            count++;
            if(count % 10 == 0)     // Play a song every ten station stops
                start playSong;
        }

    }
}

void reverseTrain()
{
    Float(MOTOR);
    Wait(100);              // 1 second
    Toggle(MOTOR);
    On(MOTOR);
}

task playSong()             // Jingle Bells
{
    PlayTone(330,4*__NOTETIME);
    Wait(4*__WAITTIME);
    PlayTone(330,4*__NOTETIME);
    Wait(4*__WAITTIME);
    PlayTone(330,8*__NOTETIME);
    Wait(8*__WAITTIME);
    PlayTone(330,4*__NOTETIME);
    Wait(4*__WAITTIME);
    PlayTone(330,4*__NOTETIME);
    Wait(4*__WAITTIME);
    PlayTone(330,8*__NOTETIME);
    Wait(8*__WAITTIME);
    PlayTone(330,4*__NOTETIME);
    Wait(4*__WAITTIME);
    PlayTone(392,4*__NOTETIME);
    Wait(4*__WAITTIME);
    PlayTone(262,6*__NOTETIME);
    Wait(6*__WAITTIME);
    PlayTone(294,2*__NOTETIME);
    Wait(2*__WAITTIME);
    PlayTone(330,16*__NOTETIME);
    Wait(16*__WAITTIME);

    // I removed the rest of the song to save space

}

It works fine as it is, but I want to use light sensors. The quick way is to adjust the if-statement to look at sensor percentage values, but I’d like to learn to use RCX events while I’m at it.

Here’s a new version of main(), but it’s untested.

#define STATION1 SENSOR_1
#define STATION2 SENSOR_3
#define MOTOR OUT_B
#define __NOTETIME 8
#define __WAITTIME 10
#define STATION 0         // event: reach a station
#define LOWERLIMIT 50
#define UPPERLIMIT 80

task main()
{
    #ifdef __RCX
    SetSensor(STATION1, SENSOR_LIGHT);
    SetSensor(STATION2, SENSOR_LIGHT);

    // Only send a pulse of power to the  motor
    SetPower(MOTOR, OUT_HALF);

    SetEvent(STATION, STATION1 + STATION2, EVENT_TYPE_HIGH);
    SetLowerLimit(STATION, LOWERLIMIT);
    SetUpperLimit(STATION, UPPERLIMIT);
    #endif

    int count = 0;

    OnFwd(MOTOR);

    monitor(EVENT_MASK(STATION))
    {
        while(true) {}
    }
    catch
    {
        reverseTrain();
        count++;
        if(count % 10 == 0)     // Play a song every ten station stops
            start playSong;
    }
}

Is this going to work? Also, is there anything better to put in the monitor block other than a while-loop that does nothing?

Thanks!



Message has 3 Replies:
  Re: Help me convert this train program to use events (NQC)
 
Jordan Bradford wrote: > Is this going to work? One of the most important things with light sensors is calibrating the threshold level. What works the first time you try it will likely fail when you run it at different times of the day and with room (...) (19 years ago, 28-Nov-05, to lugnet.robotics)
  Re: Help me convert this train program to use events (NQC)
 
(...) If the rcx is on the train why not mount touch sensors on the underside looking down. You could then attach small ramps to the track which would trigger the sensor. If you put the sensors on a spring loaded mounting you shouldn't have problems (...) (19 years ago, 28-Nov-05, to lugnet.robotics.rcx.nqc, FTX)
  Re: Help me convert this train program to use events (NQC)
 
(...) Speaker wire and small alligator clips will make a dandy solution for extending the basic wires - after all, you really only need one very long wire here (to the "remote station"), and yoiu have a working set-up. (...) Have you built and (...) (19 years ago, 28-Nov-05, to lugnet.robotics.rcx.nqc, FTX)

16 Messages in This Thread:






Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    
Active threads in Robotics

 
Verified and Trusted Team of Hackers
20 hours ago
Custom Search

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