Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: Arduino v1.0.1 not compatible with some Shields on: August 12, 2012, 08:45:03 pm
Well, that does the trick.  If you are very careful about when you releae the reset button then it will successfully upload a sketch that way.  Since this is not a practical solution for out in the field, is there a way to fix this behavior so that it works reliably?
2  Development / Suggestions for the Arduino Project / Possible problem with 1.0.1 sketch uploads with some shields on: August 11, 2012, 02:07:20 pm
Let me apologize right now for cross-posting this, but I wasn't getting the attention of any of the crew that develops the IDE in the other location.  1.0 and 1.0.1 will not upload a sketch to a Mega board if the shield is installed.  The same sketch uploads fine in 22 and 23.  I think this is a possible problem and will surely be a hindrance to acceptance and newbies trying to get going in the Arduino world.  Read on:
http://arduino.cc/forum/index.php/topic,113749.0.html
3  Using Arduino / Programming Questions / Re: Arduino Mega Uploading Error: avrdude: stk500_getsync(): not in sync: resp=0x00 on: August 11, 2012, 01:59:23 pm
Hello?  No response to any of this from someone in the Arduino environment development group?  Nobody cares that these problems are happening?
4  Using Arduino / Programming Questions / Re: Arduino v1.0.1 not compatible with some Shields on: August 11, 2012, 01:56:36 pm
OK, yes, I can unplug the shield each time I want to upload a sketch, but that is cumbersome and against the main idea of the simplicty of Arduino...

I tried changing the Com port to Com 6.  Same result.

Also, I think it is telling that this problem ONLY occurs with the newer environment, not the older 22 or 23 ones.  So,I think something should be fixed in the environment rather than making a bunch of existing shields not be usable!
5  Using Arduino / Programming Questions / Arduino v1.0.1 not compatible with some Shields on: July 10, 2012, 06:58:20 pm
OK, I have mentioned this before, but I decided to elevate it to its own thread. so here goes:

I believe that there is a problem in the v1.0.1 Arduino code that prevents it from working with some shields.  While I have been able to find some anecdotal evidence of several different shields having issues, internet reports are often sketchy and vague, so I am not sure it is a widespread problem or not.  However, in particular, the popular RAMPS shield from the RepRap 3D printer world does have issues.  Using Arduino v1.0 or v1.0.1 with a Mega 2560 and the RAMPS shield will cause the compiler to compile just fine but fail to ever complete the upload.

So, here is the setup I have:  Arduino Mega 2560, RAMPS v1.4, Windows Vista Home 64.  If I try to program the Mega 2560 without the shield on, it will work fine with any version of Arduino environment (v.0.22, v0.23, v1.0, v1.0.1).  I can compile either something simple like Blink or complex and it will compile and upload quickly.
Put the shield on and everything works fine in the 0.22 or 0.23 environments, but stalls before completing upload with either v1.0 or 1.0.1, again with either Blink or something more complex.

This seems like a pretty serious problem to me, so I thought I would elevate it to have those more knowledgeable than I work on solving it.  For the moment, I will just stick with the older Arduino environment.

Here is the verbose output from the system when it tries to upload.
Code:
Binary sketch size: 1,632 bytes (of a 258,048 byte maximum)
C:\Users\Paul\Documents\Arduino\arduino-1.0.1\hardware/tools/avr/bin/avrdude -CC:\Users\Paul\Documents\Arduino\arduino-1.0.1\hardware/tools/avr/etc/avrdude.conf -v -v -v -v -patmega2560 -cstk500v2 -P\\.\COM15 -b115200 -D -Uflash:w:C:\Users\Paul\AppData\Local\Temp\build7138735795154356900.tmp\Blink.cpp.hex:i

avrdude: Version 5.11, compiled on Sep  2 2011 at 19:38:36
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "C:\Users\Paul\Documents\Arduino\arduino-1.0.1\hardware/tools/avr/etc/avrdude.conf"

         Using Port                    : \\.\COM15
         Using Programmer              : stk500v2
         Overriding Baud Rate          : 115200
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]

6  Using Arduino / Programming Questions / Re: Arduino Mega Uploading Error: avrdude: stk500_getsync(): not in sync: resp=0x00 on: July 10, 2012, 12:17:44 pm
I agree that there MAY be a problem with the latest release of the Arduino program.  I too purchased a RAMPS 1.4 board mated to a Mega 2560 (not R3) and am programming it from a Windows Vista Home 64 computer.  If I try to upload ANY program with the RAMPS board installed it will hang up forever.  If I uninstall the board then it loads just fine.  Before you are too quick to blame the shield however, kno0w that this shield works FINE on older versions of Arduino.  I tested it out with several; on -022 or -023 then the program (Blink) compiles just fine and uploads, on v1.0 or v1.0.1 then the program compiles but never finished uploading.  A verbose output on the upload gives this:
Code:
Binary sketch size: 1602 bytes (of a 258048 byte maximum)
C:\Users\Paul\Documents\Arduino\arduino-1.0\hardware/tools/avr/bin/avrdude -CC:\Users\Paul\Documents\Arduino\arduino-1.0\hardware/tools/avr/etc/avrdude.conf -v -v -v -v -patmega2560 -cstk500v2 -P\\.\COM15 -b115200 -D -Uflash:w:C:\Users\Paul\AppData\Local\Temp\build3592782014608887820.tmp\Blink.cpp.hex:i

avrdude: Version 5.11, compiled on Sep  2 2011 at 19:38:36
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2009 Joerg Wunsch

         System wide configuration file is "C:\Users\Paul\Documents\Arduino\arduino-1.0\hardware/tools/avr/etc/avrdude.conf"

         Using Port                    : \\.\COM15
         Using Programmer              : stk500v2
         Overriding Baud Rate          : 115200
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer
         AVR Part                      : ATMEGA2560
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    10     8    0 no       4096    8      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           flash         65    10   256    0 yes    262144  256   1024  4500  4500 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : STK500V2
         Description     : Atmel STK500 Version 2.x firmware
         Programmer Model: Unknown
avrdude: Send: . [1b] . [01] . [00] . [02] . [0e] . [03] . [90] . [85]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer
avrdude: Send: . [1b] . [01] . [00] . [02] . [0e] . [03] . [90] . [85]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: Send: . [1b] . [01] . [00] . [01] . [0e] . [01] . [14]
avrdude: Recv:

I think there may be a problem with the new version of Arduino and some shields.  Research on the internet shows that several people are having this same problem, and not all of them with the RAMPS board.  More knowledgeable minds than I need to look into this.
For now my solution is to simply use an old version of the Arduino code.
7  Using Arduino / Motors, Mechanics, and Power / Pre-built Adafruit Motorshields? on: June 05, 2012, 01:21:49 pm
Help!  I have a client who has burned out one or two Adafruit Motorshields on a piece of equipment that I built for him.  Problem is, I don't have any more in my personal stash, and they need finished boards RIGHT AWAY! (Big demo in front of a major car manufacturer just days away...)
Does anyone know of a source for pre-assembled Motorshield boards? (And, yes, they have to be identical to the Adafruit Motorshields as all the code is written and we drive two stepper motors per shield, etc, etc.)
PM or email me, tnx!
8  Using Arduino / Motors, Mechanics, and Power / Re: Adafruit motor shield - Sparkfun stepper wiring? on: May 03, 2011, 01:34:41 pm
Just a suggestion, but try setting your setMaxSpeed(700); setSpeed(700); and most importantly setAcceleration(1000).  An accellof 15 is soooo slow and I am sure you never really get to speed before you reach your final position!

Also, make sure you are using the latest AccelStepper library as I have found some significant changes in the different version.

Finally, it is common and recommended to drive a stepper motor system with 4 to 6 times the rated voltage of the coils.  I believe this has to do with the pulsed nature of the driving circuitry (good description of all matters Stepper Motor related at http://www.geckodrive.com/ark-2/support.html ), but in any case, if the coils are rated 12V try driving it with as high a voltage as the Motor Shield will take, I think 36VDC.

-Paul
9  Forum 2005-2010 (read only) / Syntax & Programs / Re: Passing Arrays into a function on: November 17, 2010, 02:08:37 pm
You want to elaborate on what you did to make it work?  I am curious as I am just starting to play with this same piece of hardware....
10  Forum 2005-2010 (read only) / Syntax & Programs / Re: subString on: April 28, 2009, 11:49:34 am
Hmmm... is there a write up of all the commands in WString and how to use them?
11  Forum 2005-2010 (read only) / Syntax & Programs / subString on: April 27, 2009, 03:12:10 pm
I am trying to get the substring method to work.  The online documentation claims that is included in the string.h library, but I cannot find any reference to substring in that library.  From what I have read online the syntax should be something like

Str2=Str1.subString(2,6)  Which ought to return the characters in the 2nd through 6th positions of Str1.

Anybody know how I can fix this?  Is there a line or three I can add to the string.h file?  Or is there a better way to handle this?
12  Forum 2005-2010 (read only) / Syntax & Programs / Re: Deprecated methods on: April 24, 2009, 02:53:27 pm
Oh.  Sorry.  Wasn't trying to be a troublemaker.  Just was pointing out what I found to be the biggest problem I am having as a complete beginner:
No definitive place to go to read HOW to get the code to do certain things.  
Which leads to you scouring the net for other projects that are sort of like what you want to do...
But... then they no longer work because something has been changed, but there is no definitive place to see what those changes are and how to do them now...

Anyway, I love the hardware and the concept of this tool, just need a bit more definition for what works....
13  Forum 2005-2010 (read only) / Syntax & Programs / Deprecated methods on: April 24, 2009, 10:40:24 am
Why are the deprecated syntax/methods not listed anywhere on the Arduino References section?  I recently bought a Duemilanove (328) and was trying to use some code I found at the Arduino Playground that used the method printByte(b).  Kept getting an error when I tried to compile/upload. Searching for printByte on the main Arduino page just told about the command.  NOT that it is discontinued/deprecated and what method should be used in place of it.  Couldn't find it on this forum either. Actually, the fact that I COULD find it in a search implied to me that it was still good.  This forum contained one post that implied that it might be discontinued.
For those of us like me that are complete NOOBs at this and trying to teach ourselves, it is simply too frustrating to have missing pieces of information like this!
Any chance of someone putting together a deprecated list with the correct work-arounds?
14  Forum 2005-2010 (read only) / Interfacing / Re: NMEA serial sentence converter on: November 05, 2010, 03:54:14 pm
Well, I wanted to post the updated version of this code.  Originally, it didn't work quite as planned and the system would really do bad things when one or more sentences would get mangled for whatever reason, so I went back in and did some improvements to the code to be sure that the values were valid numbers and the like.
My previous comments about my programming skills still apply so take this for what it is worth (probably exactly what you paid for it...)
Code:
/* Final Version, need to get working with NMEA hardware

Code intended to listen to NMEA data stream and convert any sentence that is in the MWV format to the VWR format

Currently Is:                                            Should Be:                                
$--VWR,x.x,a,x.x,N,x.x,M,x.x,K*hh                        $--MWV,x.x,a,x.x,a*hh                    
1) Wind direction magnitude in degrees 0-180            1) Wind Angle, 0 to 360 degrees          
2) Wind direction Left/Right of bow                      2) Reference, R = Relative, T = True    
3) Speed                                                 3) Wind Speed                            
4) N = Knots                                             4) Wind Speed Units, K/M/N                
5) Speed                                                 5) Status, A = Data Valid                
6) M = Meters Per Second                                 6) Checksum
7) Speed                                        
8) K = Kilometers Per Hour                                        
9) Checksum                                        

Code written by Paul Carroll, pogcarr at yahoo.com, 9-25-2010
Liberally borrowed from code found on Arduino Playground:
   Parallax GPS interpreter by igor.gonzalez.martin@gmail.com 05-04-2007

 */
 #include <string.h>
 #include <ctype.h>

 int ledPin = 13;                  // LED test pin (insert where needed to test code)
 int rxPin = 0;                    // RX PIN
 int txPin = 1;                    // TX PIN
 int byteNMEA=-1;                  // a byte of the NMEA stream cominig in on serial port
 char linea[300] = "";             // a string buffer that we write each byteNMEA to
 char HdrStr[5] = "VWR";           // the header of the sentence we are trying to find to convert
 char Str1[10] = "";               // string for use in manipulating the wind heading info
 char Str2[10] = "";               // string for use in manipulating the wind velocity info
 char OldWindSpd[10] = "";          //variable to keep previous known wind speed
 char CSSentence[85] = "" ;        //string for sentance assembly so we can checksum it
 int count=0;
 int check=0;
 int counta=0;
 int n=0;
 int m=0;
 int index[10];
 int relhdg = 0;                    // relative heading in degrees
 int lastHDG = 0;                         // variable to hold last known heading value
 int cs ;                            //checksum




 void setup() {
   pinMode(ledPin, OUTPUT);       // Initialize LED pin
   pinMode(rxPin, INPUT);
   pinMode(txPin, OUTPUT);
   Serial.begin(4800);
   for (int i=0;i<300;i++){       // Initialize a buffer for received data
     linea[i]=' ';
   }  
 }

 void loop() {
   digitalWrite(ledPin, HIGH);    // Program got to here (loop began)
   byteNMEA=Serial.read();         // Read a byte of the serial port
   if (byteNMEA == -1) {           // See if the port is still empty
     delay(100);
   } else {
     linea[counta]=byteNMEA;        // If there is serial port data, it is put in the buffer
     counta++;
//     Serial.print(byteNMEA,BYTE);  //  comment out to just add in corrected statements, best to hook output of this into input channel of NMEA multiplexer.
//     if left in, echo mode if you will, we send each byte on down the serial line.  However, I don't think there is anything implemented to prevent crashes with data we will generate, so need to test.
     if (byteNMEA==36){            // If the received byte is = to 36 ($), beginning of transmission
       digitalWrite(ledPin, LOW); // Program found a beginning of transmission character in datastream
       counta=1;
       count=0;
       check=0;
         linea[0]=byteNMEA;
       for (int i=3;i<6;i++){     // Check characters of string to verify if the received command starts with $xxVWR
/*             Serial.print(i);
  //           Serial.print("  linea = ");      //This section was used for debugging
    //         Serial.print(linea[i]);
      //       Serial.print("   HdrStr = ");
        //     Serial.println(HdrStr[i-3]);
*/
         if (linea[i]==HdrStr[i-3]){
           check++;                            //increment a flag for every correct character in the string
//             Serial.println(check);
         }
       }
          if(check==3){                         // If we reached 3 it is a match, continue and process the data
         for (int j=0;j<strlen(linea);j++){    //now we need to figure out how to parse the string
           if (linea[j]==','){                // check for the position of the  "," separators
             index[count]=j;
             count++;
           }
           if (linea[j]=='*'){                // ... and the "*"
             index[8]=j;
             count++;
           }
         }
//Now that we know there the break points in the sentance are, we need to convert the numerical portion to numbers
//Relative heading is between indices0-1 and velocity in knots between indecise2-3
         m=0;
         n=0;
        strncpy(Str1,linea+index[0]+1,index[1]-index[0]-1);      //copy the section between the indices to Str1
//          Serial.println(Str1);                               //Debugging checkpoint
        for (int k=0;k<strlen(Str1);k++){
            if (!isdigit(Str1[k])){            //walk through Str1 and check that each chacter is a numeric digit
               n++;                                //if not, set a flag
            }
          }

          if(linea[index[1]+1]!='R'&& linea[index[1]+1]!='L'){
            n++;                                                  //set a flag if the relative heading flag is not either an R or an L
          }
 
          if (atoi(Str1)<0 || atoi(Str1)>180){                        //This checks to be sure the heading number is a reasonable one
              n++;
            }
          
          if (n!=0){                                                // if the flag go set, then we will simply use the last known good value
           relhdg=lastHDG;
          } else {
            
             Str1[strlen(Str1)+1]='\0';                          // add the pesky \0 to keep C programming language happy        
            relhdg= atoi(Str1);                                    // if we got to here things are looking good, so now we need to be able to manipulate the numbers so convert to integer
        
            if(linea[index[1]+1]=='L'){                  //if the hdg indicator is L for measured from left bow, switch it to R
            relhdg = 360 - relhdg;
          }
            lastHDG=relhdg;                            //And we will reset the lastHDG variable since we got a new good one
        }
          itoa(relhdg,Str1,10);                       //converting  integer to ascii text (base 10)

        strncpy(Str2,linea+index[2]+1,index[3]-index[2]-1);   //copy section between these vertices for Str2  (wind velocity) which we assume is coming in in knots!!!
        for (int k=0;k<strlen(Str2);k++){
            if (isdigit(Str2[k]) || Str2[k]=='.'){            // similarly, we need to walk through the wind speed string to make sure it only has numerics and a period
            } else {
              m++;                                                // set a flag if it has anything else
 //             Serial.println("non-numeric");
            }
          }
          if (m!=0){
            strcpy(Str2,OldWindSpd);                          // if there were bad characters, copy OldWindSpd to the Str2 string
          } else {
            strcpy(OldWindSpd,Str2);                          //otherwise, reset the OldWindStr to the new good value
          }

        Str2[strlen(Str2)+1]='\0';                          //add the pesky /0

        
      //Now assemble a sentance of the various parts so that we can calculate the proper checksum
          strcpy(CSSentence,"IIMWV,");                    //The string could be kept the same as what it came in as, but I chose to use the P (prefix for all proprietary devices) and I for instrument
          strcat(CSSentence,Str1);
          strcat(CSSentence, ".0,R,");                      //the atoi command truncated the decimals, might as well stick .0 back on in case something is expecting this field to have a decimal
          strcat(CSSentence,Str2);
          strcat(CSSentence, ",N,A*");                  //Our instruments get the wind velocity in knots, we will be putting it back out in knots
          
        //Now that we have a string fully assembled, we can walk through it and calculate the checksum that will be required for any NMEA sentance
        
         cs=0;                                            //clear any old checksum
         for (int n=0; n < strlen(CSSentence); n++) {
          cs ^= CSSentence[n];                            //calculates the checksum
         }
           Serial.print('$');                             // Assemble the final message and send it out the serial port
           Serial.print(CSSentence);
           Serial.print(cs, HEX);
           Serial.println();
//             delay(1000);                      // not really sure what value this brought and it slowed the program a bunch
           clearStr(CSSentence);            //clears the strings for reuse
           clearStr(Str1);
           clearStr(Str2);
           strcpy(linea,"");

         }
       }        
     }  
 }
 
 // Function to clear a string
void clearStr (char* str) {
   int len = strlen(str);
   for (int c = 0; c < len; c++) {
      str[c] = 0;
   }
}
15  Forum 2005-2010 (read only) / Interfacing / NMEA serial sentence converter on: April 28, 2009, 03:53:16 pm
I have a sailboat with a rather old wind instrument package on it.  The instrument processor talks to the display instruments using NMEA codes.  I recently bought a new autopilot and it accepts NMEA codes and can use them to do things like steer to a relative wind heading.  However, the autopilot was expecting a different format than the old instrument package puts out.  Thus, I needed to make a translator that could listen to the data going to the instruments, and when it saw the right sentence, convert it and send it back out in the new format.

All that said, there are NMEA multiplexers out there already that will do this (like the one from Brookhouse) but I had wanted to play with something like an Arduino for some time.  

Please note that I am NOT a programmer of any sort.  I hack at what others have done until it sort of does what I want it to do.  So I am sure that there were better ways of doing what I did.  Somebody has probably written a library that will do this in three function calls, but there you go.  At least I can read my code and figure out what it does.

If you have suggestions for how I could have made this better, please feel free to post improvements or let me know!

Code:

 /*

Code intended to listen to NMEA data stream and convert any sentance that is in the MWV format to the VWR format

Currently Is:                                            Should Be:                                
$--VWR,x.x,a,x.x,N,x.x,M,x.x,K*hh                        $--MWV,x.x,a,x.x,a*hh                    
1) Wind direction magnitude in degrees 0-180            1) Wind Angle, 0 to 360 degrees          
2) Wind direction Left/Right of bow                      2) Reference, R = Relative, T = True    
3) Speed                                                 3) Wind Speed                            
4) N = Knots                                             4) Wind Speed Units, K/M/N                
5) Speed                                                 5) Status, A = Data Valid                
6) M = Meters Per Second                                 6) Checksum                              
7) Speed                                        
8) K = Kilometers Per Hour                                        
9) Checksum                                        

Code written by Paul Carroll, pogcarr at yahoo.com, 4-28-2009
Liberally borrowed from code found on Arduino Playground:
   Parallax GPS interpreter by igor.gonzalez.martin@gmail.com 05-04-2007

 */
 #include <string.h>
 #include <ctype.h>

 int ledPin = 13;                  // LED test pin (insert where needed to test code)
 int rxPin = 0;                    // RX PIN
 int txPin = 1;                    // TX PIN
 int byteNMEA=-1;                  // a byte of the NMEA stream cominig in on serial port
 char linea[300] = "";             // a string buffer that we write each byteNMEA to
 char HdrStr[5] = "VWR";           // the header of the sentence we are trying to find to convert
 char Str1[10] = "";               // string for use in manipulating the wind heading info
 char Str2[10] = "";               // string for use in manipulating the wind velocity info
 char CSSentence[85] = "" ;        //string for sentance assembly so we can checksum it
 int count=0;
 int check=0;
 int counta=0;
 int index[10];
 int relhdg = 0;                    // relative heading in degrees
 int cs ;                            //checksum




 void setup() {
   pinMode(ledPin, OUTPUT);       // Initialize LED pin
   pinMode(rxPin, INPUT);
   pinMode(txPin, OUTPUT);
   Serial.begin(4800);
   for (int i=0;i<300;i++){       // Initialize a buffer for received data
     linea[i]=' ';
   }  
 }

 void loop() {
   digitalWrite(ledPin, HIGH);    // Program got to here (loop began)
   byteNMEA=Serial.read();         // Read a byte of the serial port
   if (byteNMEA == -1) {           // See if the port is empty yet
     delay(100);
   } else {
     linea[counta]=byteNMEA;        // If there is serial port data, it is put in the buffer
     counta++;
//     Serial.print(byteNMEA,BYTE);  //  comment out to just add in corrected statements, best to hook output of this into input channel of NMEA multiplexer.
//     if left in, echo mode if you will, we send each byte on down the serial line.  However, I don't think there is anything implemented to prevent crashes with data we will generate, so need to test.
     if (byteNMEA==13){            // If the received byte is = to 13 (CR), end of transmission
       digitalWrite(ledPin, LOW); // Program found an end of transmission character in datastream
       counta=0;
       count=0;
       check=0;
       for (int i=4;i<7;i++){     // Check characters of string to verify if the received command starts with $VWMWV
         if (linea[i]==HdrStr[i-4]){
           check++;
         }
       }
          if(check==3){                         // If we reached 3 it is a match, continue and process the data
         for (int j=0;j<strlen(linea);j++){    //now we need to figure out how to parse the string
           if (linea[j]==','){                // check for the position of the  "," separators
             index[count]=j;
             count++;
           }
           if (linea[j]=='*'){                // ... and the "*"
             index[8]=j;
             count++;
           }
         }
//Now that we know there the break points in the sentance are, we need to convert the numerical portion to numbers
//Relative heading is between indices0-1 and velocity in knots between indecise2-3


        strncpy(Str1,linea+index[0]+1,index[1]-index[0]-1);      //copy the section between the indices to Str1
        Str1[strlen(Str1)+1]='\0';                                // add the pesky \0 to keep C programming language happy
        relhdg= atoi(Str1);
        if(linea[index[1]+1]=='L'){                  //if the hdg indicator is L for measured from left bow, switch it to R
          relhdg = 360 - relhdg;
        }
        itoa(relhdg,Str1,10);                       //converting  integer to ascii text (base 10)

          
        strncpy(Str2,linea+index[2]+1,index[3]-index[2]-1);   //copy section between these vertices for Str2  (wind velocity) which we assume is coming in in knots!!!
        Str2[strlen(Str2)+1]='\0';

        
      //Now assemble a sentance of the various parts so that we can calculate the proper checksum
          strcpy(CSSentence,"PIMWV,");                    //The string could be kept the same as what it came in as, but I chose to use the P (prefix for all proprietary devices) and I for instrument
          strcat(CSSentence,Str1);
          strcat(CSSentence, ".0,R,");                      //the atoi command truncated the decimals, might as well stick .0 back on in case something is expecting this field to have a decimal
          strcat(CSSentence,Str2);
          strcat(CSSentence, ",N,A*");                  //Our instruments get the wind velocity in knots, we will be putting it back out in knots
          
        //Now that we have a string fully assembled, we can walk through it and calculate the checksum that will be required for any NMEA sentance
        
         cs=0;                                            //clear any old checksum
         for (int n=0; n < strlen(CSSentence); n++) {      
          cs ^= CSSentence[n];                            //calculates the checksum
         }
           Serial.print('$');                             // Assemble the final message and send it out the serial port
           Serial.print(CSSentence);
           Serial.print(cs, HEX);
           Serial.println();
           strcpy(CSSentence,"");            //clears the strings for reuse
           strcpy(Str1,"");
           strcpy(Str2,"");
           strcpy(linea,"");

         }
       }        
     }  
 }
Pages: [1] 2