Wednesday, May 27, 2015

Garden Robot Update 1

Okay...first update.
Couple mechanical pictures:

I spent quite a long time looking for a valve we had sitting around, and Mom found one in about 5 seconds.  In its former glory, this faded yellow device connected to a hose water spigot, allowing for the fill of water balloons as regulated by the yellow lever on the close side.  I used a 5:1 gear reduction and a single NXT motor to operate the valve (Yes, this is the first time ever I have glued a personal LEGO.) You can't see it in the picture, but the black hose is from a drip irrigation system I got years ago, and is currently connected to a crudely-cut water bottle and sealed with my mediocre hot-gluing skills.  The water bottle acts as the water reservoir and feeds via gravity through the valve.  By opening the valve for different amounts of time, I can adjust how much water is fed to the plant.

 Here is my current test setup.  Three containers of dirt (thankfully "science experiments" are encouraged in my house) and two nails for use as probes.  They are used in a basic "touch sensor" setup, in series with a 2.2kOhm resistor and connected to pins 1 and 2/3 of a sensor port.  More conductivity means more water present, and a lower raw  sensor value read by the NXT.



After a long, multi-day drama (see below), I'm currently at this code:

//Macros for moisture thresholds (raw probe values)
#define HIGH 500
#define MED 300

//Macros for watering times (ms)
#define HIGHWAIT 2000
#define MEDWAIT 1000

//Macros for moisture test wait time (ms)
#define TESTWAIT 5000

int MoistureLevel;

sub MoistureTest()
    {
int raw;
//Apparently the following 2 lines are necessary with updated firmware.
     SetSensorType(S1, SENSOR_TYPE_CUSTOM); //Set the sensor type.
     ResetSensor(S1); //Necessary following SetSensorType
     int data[10]; //Create a data vector with 10 elements
     for(int i=0; i<11 10="" epeat="" i="" p="" times="">             {
             raw=SensorRaw(S1); //Set "raw" variable equal to probe reading
             data[i]=raw; //Use the loop counter as index to save raw in vector
             ClearScreen(); //Don't forget this
             TextOut(0,LCD_LINE1,"Current value = ");
             NumOut(0,LCD_LINE2,raw); //Display current value
             Wait(100); //Wait 1/10 a second
             }
     MoistureLevel=ArrayMean(data,NA,NA); //Reduce noise by averaging vector
     }

sub Water(int WaterLevel)
    {
    if (WaterLevel==0) //Do nothing for an argument 0
       {
       }
    else //Open valve, wait the time given by the argument (>0) and close valve
        {
        RotateMotorEx(OUT_A,100,-470,NA,false,true);
        Wait(WaterLevel);
        RotateMotorEx(OUT_A,100,470,NA,false,true);
        }
    }


task main()
{
     while (true)
{
Wait(TESTWAIT); //Wait for a while before testing moisture level
           MoistureTest();
           PlayTone(262,400); //Play Tone to assert completion of subroutine
           ClearScreen();
           TextOut(0,LCD_LINE1,"Mean value = ");
           NumOut(0,LCD_LINE2,MoistureLevel); //Display this average value
           //This structure displays LOW, MEDIUM or HIGH on screen
           //and passes a watering time argument to the Water subroutine
           if (MoistureLevel <=MED)
       {
       TextOut(0,LCD_LINE3,"LOW");
       Water(0);
       }
           else
          {
               if (MoistureLevel >MED && MoistureLevel<=HIGH)
                  {
                  TextOut (0,LCD_LINE3,"MEDIUM");
                  Water(MEDWAIT);
                  }
               else
                   {
                   TextOut (0,LCD_LINE3,"HIGH");
                   Water(HIGHWAIT);
                   }
               }
           }
}


The NXC Drama:

  1. My first thought:" I should have upgraded to the enhanced NXC/NBC firmware ages ago.  It has support for up to 4D matrices, which is awesome." Unfortunately, it's not that easy.  
  2. Setting up BricxCC was a pain.  I didn't have the MINDSTORMS software already installed on my computer, so I needed the Fantom Drivers from this LEGO page (which they actually still host, much to my shock).
  3. But I got a weird .ini file error when running the autorun.  I found someone else with the same issue, and instead used the drivers from this page (as linked by the second post in the thread).
  4. With the drivers installed, BricxCC was able to connect to my NXT (I had some stock firmware already on it).  For a while I was stuck because an array command I was trying to use wasn't recognized.  I later figured out it was because the compiler will recognize the firmware you are using in your NXT and throw an error if you use commands intended for the enhanced firmware.  So I was off to find the enhanced firmware...
  5. I first tried to flash the firmware linked on this page onto the NXT using BricxCC's Firmware Download feature.  It worked perfectly, but when I ran my program, I kept on getting a "0" raw value back from the sensor, which couldn't be right.  I assumed it was a firmware issue, so I looked elsewhere for a different firmware.
  6. I made the mistake of getting a firmware binary from this page and attempting to use BricxCC's Firmware Download feature to flash it to my NXT...oops.  I had no idea what I was doing, and it totally failed, bringing my brick to "quiet clicking" model.  If I had read more closely, I would have been able to tell that I needed to compile it first, which I didn't.
  7. I then tried to revert back to my earlier firmware using the same feature...it didn't work at all, but BricxCC didn't throw an error.  I was perplexed, because my NXT was reduced to quiet clicking, and even though I downloaded the stock LEGO firmware, it wouldn't respond.
  8. Getting a bit worried, I booted up my old computer and started up NXT-G 1.1  Thankfully, I was able to flash the stock firmware onto my quiet clicking NXT.  After repeating the whole "firmware binary" episode a second time, I read the page and realized my mistake.
  9. This made me realize that my first firmware flash must have been correct, and the problem was in my code.  After some searching through the documentation, I eventually stumbled upon the weird reality that you must first use SetSensorType to define the sensor, and then use ResetSensor, and you can then use SensorRaw to read it as before.
  10. After some more screwing around with the firmware versions (the flash contained two different versions of the enhanced firmware, and only one worked), I settled upon using the "lms_arm_nbcnxc_132.rfw" version.
TL;DR:  Use this Fantom Driver and the "lms_arm_nbcnxc_132.rfw" file from the enhanced firmware linked on this page.  Don't use BricxCC's Firmware Download feature when restoring a clicking NXT, use NXT-G's instead.