Saturday, June 22, 2013

Faster RcTank Gear Ratios and Code









Been playing around with gear ratios. Currently the ratio is 12 : 29 teeth. The Leonardo can use up to 20 volts upper limit so a 14.8volt 4s 50C 4400mAh lithium battery would suit this well. The current battery is a 12V lead acid battery 1.3Ah. The RCTank could be under powered a little. I have had to make the main track rotor larger having 10 sides.May increase the main track rotor to 13 sides and use 11:33 as the gear ratio.

Leonardo Code


#include <avr/interrupt.h>
//#define DEBUG


boolean rise1;
uint32_t result1;
uint32_t ulStart1;  
uint32_t count1;
unsigned int result_array1[20];
boolean rise2;
uint32_t result2;
uint32_t ulStart2;  
uint32_t count2;
unsigned int result_array2[20];
void setup(void)
{
    pinMode(0, INPUT);
    digitalWrite(0, HIGH);    // Enable pullup resistor
    pinMode(1, INPUT);
    digitalWrite(1, HIGH);    // Enable pullup resistor

 pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin

  pinMode(13, OUTPUT); //Initiates Motor Channel B pin
  pinMode(8, OUTPUT);  //Initiates Brake Channel B pin


 rise1=false;
 rise2=false;
 result1=0;
 result2=0;
  count1=0;count2=0;

  #ifdef DEBUG
  Serial.begin(115200);   
  #endif

#ifdef DEBUG
    while(!Serial);
    Serial.println("Setup Finished");
    delay(500);
#endif


   
      
      digitalWrite(12, HIGH); //Establishes forward direction of Channel A 
    digitalWrite(9, LOW);   //Disengage the Brake for Channel A
      analogWrite(3,0);   //Spins the motor on Channel A at full speed
   
      digitalWrite(13, HIGH); //Establishes forward direction of Channel A 
    digitalWrite(8, LOW);   //Disengage the Brake for Channel B
      analogWrite(11,0);   //Spins the motor on Channel A at full speed
      delay(1000);

  attachInterrupt(2, trigger1,CHANGE);
  attachInterrupt(3, trigger2,CHANGE);
}
                              //
void loop(void)
{


noInterrupts();
//result1=result1/count1;
 //result2=result2/count2;



  if(count1>20)count1=20;
  if(count2>20)count2=20;
  result1=0;
  result2=0;
  for(int i=1;i<count1;i++){
  if(result_array1[i]>940 && result_array1[i]<2010){
      if(result1<result_array1[i])result1=result_array1[i];
      #ifdef DEBUG  
      Serial.print(result_array1[i]);
      Serial.print(",");
      #endif
} }
#ifdef DEBUG  
Serial.println();
#endif

    for(int i=1;i<count2;i++){
      if(result_array2[i]>940 && result_array2[i]<2010){
  if(result2<result_array2[i])result2=result_array2[i];
    #ifdef DEBUG 
    Serial.print(result_array2[i]);Serial.print(",");
    #endif
  }}

      //result1=result1/(count1-1);
      //result2=result2/(count2-1);
      
     // Serial.print("[H"); // cursor to home
   #ifdef DEBUG  
    Serial.print("t1:");Serial.println(result1);
    Serial.print("t2:");Serial.println(result2);
   #endif
   
 delay(10);

 count1=0;count2=0;


  int throttle1=(result2-1500)/2;//(result2-1500)/2+(result1-1500)/4;
  int throttle2=(result2-1500)/2;//(result2-1500)/2-(result1-1500)/4;
  
  if(throttle1>0){
    if (throttle1>235)throttle1=255;
    if (throttle1<50)throttle1=0;
    
      digitalWrite(12, HIGH); //Establishes forward direction of Channel A 
      analogWrite(3,throttle1);   //Spins the motor on Channel A at full speed
    
  
  }
  else {
      throttle1=abs(throttle1);
    if (throttle1>235)throttle1=255;
    if (throttle1<50)throttle1=0;
      digitalWrite(12, LOW); //Establishes forward direction of Channel A 
      analogWrite(3,throttle1);   //Spins the motor on Channel A at full speed
    }

  if(throttle2>0){
  
    if (throttle2>235)throttle2=255;
    if (throttle2<150)throttle2=0;
      digitalWrite(13, HIGH); //Establishes forward direction of Channel A 
      analogWrite(11,throttle2);   //Spins the motor on Channel A at full speed
    
  
  }
  else {
      throttle2=abs(throttle2);
      if (throttle2>235)throttle2=255;
      if (throttle2<150)throttle2=0;
  digitalWrite(13, LOW); //Establishes forward direction of Channel A 
      analogWrite(11,throttle2);  //Spins the motor on Channel A at full speed
    }
 // result1=0;
// result2=0;

 interrupts();
 delay(100);

}
                              //
// Interrupt Service Routine attached to INT0 vector

void trigger1()
{


  if(digitalRead(0) )
  {
    rise1=true;
    ulStart1 = (uint32_t)micros();

  }
  else 
  
  if(rise1)
  {
 if(count1<20)result_array1[count1]=(unsigned int)((uint32_t)(micros()) - ulStart1);
    count1++;
     


    rise1=false;
  }
  
}

void trigger2()
{
   
  if(digitalRead(1) )
  {
    rise2=true;
    ulStart2 = (uint32_t)micros();

  }
  else 
  
  if(rise2)
  {
 if(count2<20)result_array2[count2]=(unsigned int)((uint32_t)(micros()) - ulStart2);
    count2++;
    rise2=false;
  }
}

3 comments: