changes to motor library

Dependents:   pid-car-example

Fork of motor by Lawrence Harlow

motor.cpp

Committer:
lh14g13
Date:
2017-01-11
Branch:
motorupdate
Revision:
28:9d4042b05640
Parent:
27:98aecf1889ed
Child:
29:d3efef939c18

File content as of revision 28:9d4042b05640:

#include "mbed.h"
#include "TFC.h"
#include <math.h> 
#include "motor.h"
DigitalOut myled(LED1);





//Variables
int cornerPwmControl;



// uncomment for testing motor functions.



//need a function for calcu;lating the angle
//need a function for converting w1 to delta. or do i?
 
//----------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------------This is for Motor Set up------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------------------  

 void   runMotor() // This simply sets the PWM and turns the motors off whent he button is pushed
    {
        
        
        TFC_SetMotorPWM(0.4,0.4);
        
        while(1)
        {
            if(TFC_ReadPushButton(0)>0)
            {
                TFC_SetMotorPWM(0.0,0.0);
                
                DefaultMode();
                
            }
        }
        return;
    }
// This works for starting the car using the buttons on the bridge 
void DefaultMode()
    {
        TFC_Init();
        while(1)
        {
            TFC_HBRIDGE_ENABLE;
            if(TFC_ReadPushButton(1)>0)
               {
                    runMotor();
                }
            
            else if(TFC_ReadPushButton(0)>0)
               {
                    //this will be a debug mode
                }
            
               
        }
        TFC_HBRIDGE_DISABLE;
    return;
    }
//-----------------------------------------------------------------------------------------------------    
//------------------------ this is for speed control---------------------------------------------------
//-----------------------------------------------------------------------------------------------------
// This simply sets the duty cycle.
// its a rudementary motor control.
float setDutyCycle(float dutyC, int w,int targetW)
{
    
    
    if(true)
    {
        if(w<targetW)   
        {
            dutyC+=0.1;        
        
        }  
        
        else
        {
            dutyC-=0.1;   
        }
        
    }
    
    
    else if(false)
    {
        
    }
    
 
    return dutyC;
}
    

 //----------------------------------------------------------------------------------------------------------------------------   
 //------------------------------------------------Cornering Control-----------------------------------------------------------   
 //----------------------------------------------------------------------------------------------------------------------------   
    
    
    
// This is a function which works off of the duty cycle. NO SENSOR REQUIREMENT

// this function works off the actual value rather than the change in angle. therefore need to have a variabe which stores where the value is.
void dutyCycleCorner( float speed, float theta)
{
        bool leftOrRight;
        // sets if the car is going left or right
        if(theta<0)
        {
            leftOrRight=true;
            theta=theta*-1;   
            
        }
 
        float deltaW = ((0.1f*tan((theta/0.02222f)* (3.14f / 180.0f)))/0.2f)*speed;
        
        //TFC_SetMotorPWM(w2,w1);
        // this sets the speeds of each motor (outer wheel goes faster than inner wheel)
        if(leftOrRight){
        TFC_SetMotorPWM(speed+ deltaW,speed- deltaW);
        }
        else{
            TFC_SetMotorPWM(speed- deltaW,speed+ deltaW);
        }
        
        
        
 return;       
}
        
        
void sensorCorner(float &w1,float &w2,float theta,float speed)
{   // when cornering left the left motor slows down more than the right hand side
   // this is the ED for when the car is running off of the sensors rather than a set PWM
   
    bool leftOrRight = false;
    float tune = 1.2;
    // makes theta positive and sets if the car is turning left or right
    if(theta<0)
        {
            leftOrRight=true;
            theta=theta*-1;   
            
        }
        
    if(theta>0.5)
    {
     theta = 0.5;   
        
    }
    
    
    
    
    //These equations set the angular speeds of the motors 
    //there are two equations for testing purposes
    if(true){
    // calculates the difference in the speed
    float deltaW = ((0.1f*tan((theta/0.02222f)* (3.14f / 180.0f)))/0.2f)*speed;
    // 
    if(leftOrRight){
          w1= speed+ deltaW * tune;
          w2= speed -deltaW * tune;
         
          
          }
    else{
            w1= speed- deltaW*tune;
            w2= speed +deltaW*tune;
            
        }
    }
    
    else{
        //calulates the speed in eack wheel.
        float vin = speed(2 - 0.1/tan((theta/0.022222)*(3.14f / 180.0f));
        float vout = speed(2 + 0.1/tan((theta/0.022222)*(3.14f / 180.0f));
        if(leftOrRight){
          
          w1=vout;
          w2=vin;
          
          }
        else{
               
                w1=vin;
                w2=vout;
            }
        
      }  
    return;   
}