Shop OBEX P1 Docs P2 Docs Learn Events
Badge base program — Parallax Forums

Badge base program

Yes, I got sick and tired of trying to figure out if the Badge is on or off. This is my concept for a base starting program, you would burn it to the EEPROM, immediately. That way when you are just starting with some programming, at least you know if your Badge is on or off, and you have a visual for the actual battery voltage.

I use my "universal" RCT value, but you may want to use a volt meter to check what your actual voltage reading is compared to what the voltage is that is displayed.

I think that on the next Badge model, an LED should be placed below the OSH button, that way the power issue could be associated with that button.

So, the way this works, the below program would be burned to the EEPROM, then the below code could be the starting point for your next great Badge program.

Ray
/*
  onoff_Base.c
  Jan 20, 2016
  * Badge start up program in EEPROM
  * Turns on blue LED next to P25
  * when Badge is turned on. Also
  * displays the battery voltage on
  * the bootom line of oled.
  * 
*/
#include "simpletools.h"
#include "badgetools.h"

/* Voltage gauge*/
float calcVt(float batteryVolts, int decayMicros);
float batVolts(float vt);

float RC = 360000.0 * (1.0 / 100000000.0);
float vt, vbat;
/****************/

/* Function prototypes */
void BatUpdate(void);

int main()
{
  // Add startup code here.
  badge_setup();
  pause(250);
  led(2,ON);   // LED - next to P25.
  /* Voltage RCT value. */
  vt = calcVt(4.17, 1453);  // Universal RCT value.
/******************/

 
  while(1)
  {
    // Add main loop code here.
    BatUpdate();
    pause(500);
  }  
}
/******************/

/* Functions */
/* Battery voltage readout. */
void BatUpdate(void)
{
  text_size(SMALL);
  cursor(0,7);
  // Voltage
  vbat = batVolts(vt);
  pause(100);
  oledprint("%1.2f V", vbat);
}

/* Battery voltage calculations. */
float calcVt(float voltsBattery, int microsDecay)
{
  float voltsThreshold, tDecay;
  tDecay = ((float) microsDecay) / 1000000.0;
  voltsThreshold = voltsBattery * (1 - exp(-tDecay / RC));
  return voltsThreshold;
}
  
float batVolts(float voltsThreshold)
{
  float voltsBattery, microsDecay;
  low(0);
  pause(1);
  microsDecay = ((float) rc_time(0, 0)) / 1000000.0;
  voltsBattery = voltsThreshold / (1 - exp(-microsDecay / RC));
  return voltsBattery;
}
/******************/

Comments

  • PublisonPublison Posts: 12,366
    edited 2016-01-20 16:49
    Ray, I get an error when I try to build.
    C:/Users/Jim/Documents/SimpleIDE/My Projects/library/libbadgetools/badgetools.h:1498:3: error: conflicting declaration 'typedef volatile struct screen screen'
    C:/Users/Jim/Documents/SimpleIDE/My Projects/library/libbadgetools/badgetools.h:1481:25: error: 'struct screen' has a previous declaration as 'struct screen'
    

    On line 1498 ofn badgetools.h

    [code]typedef volatile struct screen {
    volatile int cog;
    volatile int command;
    volatile int CS;
    volatile int DC;
    volatile int DATA;
    volatile int CLK;
    volatile int RST;
    volatile int vccstate;
    volatile int displayWidth;
    volatile int displayHeight;
    volatile int displayType;
    volatile int AutoUpdate;
    volatile uint8_t buffer[LCD_BUFFER_SIZE_BOTH_TYPES];
    volatile int charSize;
    volatile int crsrX;
    volatile int crsrY;
    } screen; <*****************************************************-

    What version of badgetools.h are you using?

    What are you using for build option, (board type, Compiler Type, Memory Model?)
  • What version of badgetools.h are you using?

    What are you using for build option, (board type, Compiler Type, Memory Model?)
    20000-Badge-Propeller-C-Examples-Library-151023.zip, I think that this is the latest one. I am using SimpleIDE for Linux, it is still RC1. You should probably use a generic board selection, and CMM memory model. Might as well get use to CMM, I believe that will be the only choice in the next version of SimpleIDE. Funny you should ask what compiler type.

    I am really surprised that you got all of those errors, what is your set up?

    Ray
  • For the casual lookers, that stumble onto this thread, SimpleIDE can be found at 'learn.parallax.com'. The latest Badge examples zip file, using PropGCC, can be found in the Parallax store, in the download area for the Badge boards. Hmmm, I guess you have to do some searching and digging to put the tools together.

    The SimpleIDE versions, as far as I can tell, the version for Windows and Linux run the same. The version for the Raspberry Pi is quite a bit behind the others. I only use that version in a very casual circumstance, in other words, no serious programming. I was going to try putting together a Raspberry Pi Propeller HAT with some IR hardware, but since the Raspberry Pi SimpleIDE version will not work with the HAT, that experiment will be put off, for who knows how long.

    So, why do I use SimpleIDE PropGCC and not Spin? As it pertains to working with the Hackable Badge, I find PropGCC to be better suited to deal with creating menus and such, for the Badge. I tried both Spin and PropGCC, PropGCC wins, hands down. I am not locked into PropGCC, but at the moment, PropGCC is much better suited to work with the Badge, for me, your mileage may vary.

    Their have been some rumors floating around that PropBasic may rise from the dead, but those are only rumors. The only reason I bring this up is, I am wondering how that would be able to handle programming the Badge? If the code overhead could be lowered by using PropBasic, and you could get around the language quirkiness, that could be a reasonable choice to work with the Badge.

    Well, why the Hackable Badge? Of all the boards offered by Parallax, I find the Hackable Badge to be a better bang for the buck, IMHO. And believe me, I have tried them all. Unfortunately, at the moment, there does not seem to be a whole lot of people that share my opinion. And I read somewhere, a suggestion for the use of a Raspberry Pi Zero, make a mobile Badge, WOW. I guess, it is not just Parallax, that has their eye on that area. Just wondering, if you could somehow attach one of those ESP WiFi doodads to the Hackable Badge...

    Ray

  • Made a slight enhancement to the program, the voltage readout function is now a COG, which still provides a continuous readout. This opens up the main() for other program development.

    Ray
    /*
      onoff_Base.c
      Jan 20, 2016
      * Badge start up program in EEPROM
      * Turns on blue LED next to P25
      * when Badge is turned on. Also
      * displays the battery voltage on
      * the bootom line of oled.
      Jan 22, 2016
      * Move voltage display to a COG
    */
    #include "simpletools.h"
    #include "badgetools.h"
    
    /* Voltage gauge*/
    float calcVt(float batteryVolts, int decayMicros);
    float batVolts(float vt);
    
    float RC = 360000.0 * (1.0 / 100000000.0);
    float vt, vbat;
    /****************/
    
    /* Function prototypes */
    void BatUpdate(void);  // COG function
    
    int main()
    {
      // Add startup code here.
      badge_setup();
      pause(250);
      led(2,ON);   // LED - next to P25.
      /* Voltage RCT value. */
      vt = calcVt(4.17, 1453);  // Universal RCT value.
    /******************/
    
    /* Make the battery voltage function
       into a stand a lone operation
       running in its own COG.
     */
      cog_run(BatUpdate,128);
    
     
      while(1)
      {
        // Add main loop code here.
        // 
       // pause(500);
      }  
    }
    /******************/
    
    /* Functions */
    /* Battery voltage readout.
       Make this a COG function.
       Use line 7, on oled, for 
       system readout.
     */
    void BatUpdate(void)
    {
      while(1)
      {
        text_size(SMALL);
        cursor(0,7);
      // Voltage
        vbat = batVolts(vt);
        pause(100);
        oledprint("%1.2f V", vbat);
      }  
    }
    
    /* Battery voltage calculations. */
    float calcVt(float voltsBattery, int microsDecay)
    {
      float voltsThreshold, tDecay;
      tDecay = ((float) microsDecay) / 1000000.0;
      voltsThreshold = voltsBattery * (1 - exp(-tDecay / RC));
      return voltsThreshold;
    }
      
    float batVolts(float voltsThreshold)
    {
      float voltsBattery, microsDecay;
      low(0);
      pause(1);
      microsDecay = ((float) rc_time(0, 0)) / 1000000.0;
      voltsBattery = voltsThreshold / (1 - exp(-microsDecay / RC));
      return voltsBattery;
    }
    /******************/
    
  • So, I went ahead and added a very crude but functional example of a limited usefulness stop watch. This should provide an example as to how you could expand on your base program.

    The program below can probably be enhanced and expanded quite a bit, I leave that up your imagination and determination. Now, when you go to the next ActivityBot competition, you can actually time your daughter Jane's Bot entry when it does erco's figure 8 competition.

    Now, if Jane has her own Badge, she could probably program the Badge to not only time the run, but maybe even start/stop the Bot with her Badge.

    Ray
    /*
      onoff_Base.c
      Jan 20, 2016
      * Badge start up program in EEPROM
      * Turns on blue LED next to P25
      * when Badge is turned on. Also
      * displays the battery voltage on
      * the bootom line of oled.
      Jan 22, 2016
      * Move voltage display to a COG
      Jan 23, 2016
      * Added a crude stop watch example.
    */
    #include "simpletools.h"
    #include "badgetools.h"
    #include "datetime.h"
    
    datetime dt = {2016, 1, 1, 0, 0, 0};  // Initial value
    
    
    /* Voltage gauge*/
    float calcVt(float batteryVolts, int decayMicros);
    float batVolts(float vt);
    
    float RC = 360000.0 * (1.0 / 100000000.0);
    float vt, vbat;
    /****************/
    
    volatile int myLock;
    
    /* Function prototypes */
    void BatUpdate();  // COG function
    void NewClear();
    void bLine();
    void SW_menu();
    
    int main()
    {
      // Add startup code here.
      badge_setup();
      pause(250);
      dt_run(dt);
      pause(250);
      led(2,ON);   // LED - next to P25.
      /* Voltage RCT value. */
      vt = calcVt(4.17, 1453);  // Universal RCT value.
    /******************/
    myLock=locknew();
    lockclr(myLock);
    
    /* Make the battery voltage function
       into a stand a lone operation
       running in its own COG.
     */
      cog_run(BatUpdate,128);
    
      while(lockset(myLock));
      text_size(LARGE);
      cursor(0,0);
      string("STOP");
      cursor(0,1);
      string("WATCH");
      pause(3000);
      NewClear();
      lockclr(myLock);
      
      SW_menu();
      while(1)
      {
        // Add main loop code here.
        if(button(4) == 1)  // Start
        {
          dt.s = 0;   // Clock second set to 0.
          dt_set(dt); // Reset the clock.
          while(1)
          { 
            while(lockset(myLock));
            text_size(LARGE);       
            cursor(0,0);
            dt = dt_get();   // Get the clock.
            oledprint(":%2d",dt.s);  // Show the seconds.      
            lockclr(myLock);
            if(button(1) == 1)
            {         
              while(lockset(myLock));
              cursor(0,4);
              bLine();
              cursor(0,5);
              bLine();
              text_size(LARGE);
              cursor(0,0);
              oledprint(" %2d",dt.s);
              text_size(SMALL);
              cursor(8,1);
              string("secs");
              cursor(7,2);
              string("elapsed");
              lockclr(myLock);
              break;
            }          
          }              
        }      
        if(button(6) == 1)
        {
          while(lockset(myLock));
          NewClear();
          lockclr(myLock);
        }      
      }  
    }
    /******************/
    
    /* Functions */
    void SW_menu()
    {
    
      text_size(SMALL);
      while(lockset(myLock));  
      cursor(0,4);
      string("P16-START");
      cursor(8,5);
      string("STOP-P26");
      cursor(4,6);
      string("OSH-Done");
      lockclr(myLock);
    }  
    /******************/
    /* Battery voltage readout.
       Make this a COG function.
       Use line 7, on oled, for 
       system readout.
     */
    void BatUpdate(void)
    {
      while(1)
      {
        while(lockset(myLock));
        text_size(SMALL);
        cursor(0,7);
      // Voltage
        vbat = batVolts(vt);
        pause(100);    
        oledprint("%1.2f V", vbat);
        lockclr(myLock);
      }  
    }
    /******************/
    /* Battery voltage calculations. */
    float calcVt(float voltsBattery, int microsDecay)
    {
      float voltsThreshold, tDecay;
      tDecay = ((float) microsDecay) / 1000000.0;
      voltsThreshold = voltsBattery * (1 - exp(-tDecay / RC));
      return voltsThreshold;
    }
    /******************/  
    float batVolts(float voltsThreshold)
    {
      float voltsBattery, microsDecay;
      low(0);
      pause(1);
      microsDecay = ((float) rc_time(0, 0)) / 1000000.0;
      voltsBattery = voltsThreshold / (1 - exp(-microsDecay / RC));
      return voltsBattery;
    }
    /******************/
    /* To be used for clearing lines 0 - 6 */
    void NewClear()
    {
      cursor(0,0);
      bLine();
      cursor(0,1);
      bLine();
      cursor(0,2);
      bLine();
      cursor(0,3);
      bLine();
      cursor(0,4);
      bLine();
      cursor(0,5);
      bLine();
      cursor(0,6);
      bLine();
      cursor(0,0);
    }
    /******************/
    /* Blanks a line. */
    void bLine()
    {
      oledprint("                ");
    }
    /******************/
    
Sign In or Register to comment.