Shop OBEX P1 Docs P2 Docs Learn Events
Recording and playing back 20 sensor readings. — Parallax Forums

Recording and playing back 20 sensor readings.

jonerikjonerik Posts: 2
edited 2014-09-04 16:57 in Learn with BlocklyProp
I finished my CC robotics class using the activitybot, but had trouble with the SD card section in C. I did not have any problem writing a string value to the SD card. However, I have not been able to do the section on my own where it asks you to write a program to record and log 20 sensor readings. I used the PING sensor, but my program only seems to hold one data value, which makes me think that my program is writing over all the prior values. I would appreciate some sample code.

Comments

  • edited 2014-09-04 14:23
    Hey, joneric, I'm sorry about that. We changed the information in that activity, but did not update the Your Turn. It's not really a fair assignment because there are too many things you haven't been told. We'll change that.

    There are much better ways to do this, but here is an example that tries to stay close to what you might know at that point. With this, I'm hoping you'll be able to modify to add Ping))) readings without too much trouble.
    #include "simpletools.h"                      // Include simpletools header    
    
    int DO = 22, CLK = 23, DI = 24, CS = 25;      // SD card pins on Propeller BOE
    
    int main(void)                                // main function
    {
      sd_mount(DO, CLK, DI, CS);                  // Mount SD card
    
      FILE* fp = fopen("test.txt", "w");          // Open a file for writing
      
      int val;                                    // Declare 
      for(int i = -5; i < 15; i++)                // Repeat 20x (-5...14)
      {
        val = i * 10;                             // Emulate sensor reading
        fwrite("i = ", 5, 1, fp);                 // Write string i = 
        fwrite(&i, 4, 1, fp);                     // Write value of i
        fwrite("val = ", 7, 1, fp);               // Write string val = 
        fwrite(&val, 4, 1, fp);                   // Write value of val
        fwrite("\n", 2, 1, fp);                   // Newline
      }  
      
      fclose(fp);                                 // Close file
      
      fp = fopen("test.txt", "r");                // Reopen file for reading
    
      val = 0;                                    // Clear val
      int index;                                  // Variable for holding read i
      char istr[5];                               // String to hold i = 
      char valstr[7];                             // String to hold val = 
      char nlstr[2];                              // String to hold \n
      for(int i = -5; i < 15; i++)                // go back through
      {
        fread(istr, 5, 1, fp);                    // i string -> istr
        fread(&index, 4, 1, fp);                  // i value -> index
        fread(valstr, 7, 1, fp);                  // val = -> valstr
        fread(&val, 4, 1, fp);                    // val value -> val
        fread(&nlstr, 2, 1, fp);                  // Get newline
        // Display all
        print("%s%d,  %s%d%s", istr, i, valstr, val, nlstr); 
      }  
    
      fclose(fp);                                 // Close the file
    }
    
  • edited 2014-09-04 14:40
    The code had a couple bugs, but is fixed as of now.

    Notes:

    In fwrite("i = ", 5, 1, fp) the string has 4 characters with a hidden 0, that's why we use 5 as the number of bytes to write. Likewise with char istr[5].

    The fields are fixed width, and the reading has to stay in sync with the writing (in terms of number of bytes).

    This will not display well if you try to open it as a file on your computer because the values are binary. Next post will have another way to save and retrieve the data, but also have it be readable in Notepad.
  • jonerikjonerik Posts: 2
    edited 2014-09-04 15:01
    Thanks Andy,

    I appreciate the code. We now use your activity-bot and C tutorial in our robotics course at North Seattle Community College, which I took this summer. Previously, NSCC used the Boe-bot kit in its robotics course, which I also took. Is there a book or other resource that you would recommend to learn more about the propeller chip and C? I felt a bit frustrated this past quarter with the Parallax C tutorial and the activitybot because it just did not seem as well documented as the Boe-bot kit. The above exercise was one in which I and other students spent a lot of time on but could never figure it out. I went to the OBEX files for inspiration, but virtually everything there is in Spin. I ended up checking out a copy of The C Programming Language, which is good for learning more about C, but doesn't help with microprocessors and memory usage. I thought about purchasing Programming and Customizing the Multicore Propeller Microcontroller: The Official Guide, but it seems to be based on Spin. Similarly, there is a C for Arduino book, but of course it is Arduino, not the Propeller. What would you recommend? [h=1][/h]
  • edited 2014-09-04 15:34
    Funny you should mention that, I just got assigned to write a book like the one you mentioned after finishing the one I'm half way through. If you're interested in reviewing current and forthcoming draft material, please PM me your email address.
  • edited 2014-09-04 16:57
    Alright, so here is an example where the data is saved in a Windows Notepad-friendly format. This program also retrieves the character data and converts it back to variable values, so that a program could use it too. It could be shorter and sweeter with fprintf and fscanf, but we avoid using stdio printf functions because they use too much memory when the switch is made from int to float variable types. So, instead, this program makes each line a fixed length of 32 characters.

    The program uses memset to set most of each line to spaces, and then sets the last two characters to Windows Notepad's carriage return. Converting variable values to formatted strings with sprint is a lot like sending it to the terminal with print. The difference is that sprint puts the result in a character array, not on the terminal. So, like print("i = %d , val = %d ", i, val) would display two values on the terminal, sprint(s, "i = %d , val = %d ", i, val) stores them in a string with the same format.

    The sscan function also does the same thing as scan, taking the characters from an array, and storing string represented values in variables if the format string tells it to do so. One gotcha, sscan does not treat a comma as a valid terminating character for a string represented value. So, the characters "1234," would not work, but "1234 ," would work. That's why I add spaces then commas after the %d flags instead of just commas. The commas should still work for spreadsheet imports where comma delimited values are used.
    #include "simpletools.h"                      // Include simpletools header    
    
    int DO = 22, CLK = 23, DI = 24, CS = 25;      // SD card pins on Propeller BOE
    
    int val;                                      // Declare value variable
    char s[32];                                   // 32 byte array
    
    int main(void)                                // main function
    {
      sd_mount(DO, CLK, DI, CS);                  // Mount SD card
    
      FILE* fp = fopen("test.txt", "w");          // Open a file for writing
      
      for(int i = -5; i < 15; i++)                // Repeat 20x (-5...14)
      {
        memset(s, ' ', 30);                       // 30 spaces into string
        s[30] = '\r';                             // Newline for Windows notepad
        s[31] = '\n';                             // String terminator
        val = i * 10;                             // Emulate sensor reading
        sprint(s, "i = %d , val = %d ", i, val);  // Data to s array as characters
        fwrite(s, 1, 32, fp);                     // Write line to file
      }  
      
      fclose(fp);                                 // Close file
      
      fp = fopen("test.txt", "r");                // Reopen file for reading
    
      int idx;
      for(int i = -5; i < 15; i++)                // go back through
      {
        memset(s, '\0', 32);                      // All characters to zero
        fread(s, 1, 32, fp);                      // Read the string
        sscan(s, "i = %d , val = %d ", &idx, &val);  // String values -> variables
        print("idx = %d, val = %d \n", idx, val);    // Print variables
      }  
    
      fclose(fp);                                 // Close the file
    }
    
Sign In or Register to comment.