Shop OBEX P1 Docs P2 Docs Learn Events
Terminal Question — Parallax Forums

Terminal Question

/* Fidget Spinner Motor
   Auto time version


  
  Pin 19 is on sensor input
  Pin 18 is off sensor input
  Pin 17 is magnet controller output
  Pin 16 is timing light output
  Pin 15 is timing advance input
  Pin 14 is timing retard input
  Pin 13-8 is binary timing output
  
*/
#include "simpletools.h"            // Include simple tools

void button_time();

void time_it();

static volatile int delay, on_sensor, off_sensor; //all global

int main()                          // Main function

{
 clkset(0b01101111, 80000000); 
 cog_run(button_time,128);          // Push button control runs in other cog
 cog_run(time_it,128);              // Times magnet on sensor to magnet off sensor
 
 on_sensor = 1 << 19;
 off_sensor = 1 << 18;
 int magnet = 1 << 17;
 int t_light = 1 << 16;
 
 
  DIRA &= ~(1 << 19);
  DIRA &= ~(1 << 18);
  DIRA |= (magnet);
  DIRA |= (t_light);
  
  
  while(1)
  {
   
    if ((INA & on_sensor) == on_sensor) // If on sensor high
    {
      OUTA |= (magnet);             // Turn magnet on
    }
    
    if ((INA & off_sensor) == off_sensor) // If off sensor high
    {
      pause(delay);                 // Delay is assigned in button_time
     
      OUTA &= ~(magnet);            // Turn magnet off             
      OUTA |= (t_light);            // Timing light on
      pause(1);             
      OUTA &= ~(t_light);           // Timing light off
    }            
  }  
}




void button_time()
{
  int lights = 0b111111 << 8;       //Timing delay LED's
  int advance = 1 << 15;
  int retard = 1 << 14;
  DIRA &= ~(advance);
  DIRA &= ~(retard);
  DIRA |= (lights);
  
  int t = 20;
  
  
  while(1)
  {
    
     if ((INA & retard) == retard)   // If retard button high
     {
       t++ ;
       
       if (t > 64)                   // Don't allow t to exceed 64
       {
         t = 64;
       }
                 
     }
     
     if ((INA & advance) == advance) // If advance button high
     {
       t-- ;
       
       if (t < 1)                    // Keep t 1 or larger
       {
         t = 1;
       }
               
     }
     
     delay = t;                      // Pass delay to main function
     
                 
        OUTA = (OUTA & ~(lights)) | (t << 8 ); // Put t value on output lights
        pause(300);               
    
  
  }
}      


 
void time_it()



{
  
unsigned int uS, t_initial, t_final;
int pass_number = 0;
int total_passes = 10;
int total = 0; 
uS = CLKFREQ/1000000;
int elapsed;

  while(1)
  {
    print("main loop test");
    while(pass_number <= total_passes)
    {
      waitpeq(on_sensor, on_sensor);        // Wait for on sensor high
      print("on_sensor test");
      
        t_initial = CNT;
      
      
      waitpeq(off_sensor, off_sensor);      // Wait for off sensor high
      print("off_sensor test"); 
                  
        t_final = CNT;
      
       
      total = total + (t_final - t_initial); // Accumulate time for total amount of passes(in ticks)
      pass_number++;
      
    }    
    
      elapsed = (total * uS)/total_passes;  // Calculate average pass time in uS
      
      print("Elapsed Time = %d\n", elapsed);
      pass_number = 0;
      total = 0; 
          
          
  }    

}

This is the fidget spinner motor code so far. The motor runs, but I get no output from time_it on terminal.
The Flip and all magnet control hardware run on a LIPO battery pack. Is it a code problem or a power conflict
between the LIPO and the USB power when using terminal? It could also be an Apple problem since I have found
at least one incompatibility between Apple and Simple-IDE.

Comments

  • Does it seem like it will work? I think the cog should be running printing something. I will break out my windows PC and try it on there.
  • So I copied the code to a PC and got the same result. Is there some reason why I can't print from another COG?
  • Thankyou. :smile:
  • Beavis3215Beavis3215 Posts: 229
    edited 2018-08-12 12:25
    void time_it()
    
    
    
    {
      
    unsigned int uS, t_initial, t_final;
    int pass_number = 0;
    int total_passes = 20;
    int total = 0; 
    uS = CLKFREQ/1000000;
    int elapsed;
    
      while(1)
      {
        simpleterm_open();
        
        while(pass_number <= total_passes)
        {
          waitpeq(on_sensor, on_sensor);        // Wait for on sensor high
          
            t_initial = CNT;
          
          
          waitpeq(off_sensor, off_sensor);      // Wait for off sensor high 
                      
            t_final = CNT;
          
           
          total = total + (t_final - t_initial); // Accumulate time for total amount of passes(in ticks)
          pass_number++;
          
        }    
        
          elapsed = (total / uS)/total_passes;  // Calculate average pass time in uS
          print("Elapsed Time = %d\n", elapsed);
          pass_number = 0;
          total = 0; 
              
              
      }    
    
    }
    

    I have two Hall sensors oriented around a turning fidget spinner. One sensor is on_sensor, the other is off_sensor. time_it is meant to time the small area where one spinner weight passes on_sensor, and shortly after the next clockwise weight passes off_sensor. The output is close to what I expect; however, you can plainly see that the time alternates higher, lower, higher, lower, by about a millisecond or so. I think that the code is good. Could this variation be due to what the propeller hub does? I have 3 routines running in different hubs, but this one runs alone, or is not meant to interact with the others.
  • What does the time difference (or oscillation) equal in RPM? Could the difference in readings be in the noise of the measurement? or due to round off in the average calculation?
  • Time_on to time_off is 30 degrees of rotation. According to my calculations RPM = elapsed * 5,000,000. Im programming it in now. The fidget spinner motor works best with the hall sensors as far from the spinner as possible, because the magnet in the sensor is significant drag to the spinner. I play with the sensor gap to maximize rpm. For now I will bring them in closer to make sure I don't have a "miss".
  • Beavis3215Beavis3215 Posts: 229
    edited 2018-08-12 21:22
    The above math is close. With elapsed in uS, an integer value of 4000 and larger, rpm = 1/12 * (elapsed/60,000,000). It is apparent that I don't know how to perform this calculation in C without overflow.
    rpm = 1/12* (elapsed/60,000,000)
    

    gives a negative number in its present form.

    I know roughly what the problem is but don't fully understand, could you please help?
Sign In or Register to comment.