Engine control program with 3 engines Needs to make a case for each simultaneous engine setting, because of the WAIT after each number of bits have been sent.

Dependencies:   mbed-rtos mbed PinDetect

Fork of FinalMotorControl by Robot Bachelor

main.cpp

Committer:
mjhaugsdal
Date:
2016-05-11
Revision:
24:24c91a6ae6ba
Parent:
23:ad08a8eabc24
Child:
25:321b970eb3ff

File content as of revision 24:24c91a6ae6ba:

#include "mbed.h"
#include "rtos.h"
#include "PinDetect.h"

Serial pc(USBTX, USBRX); // tx, rx
PinDetect pb(A5);

//Analog Pins
//First engine
DigitalOut IN1(A0);
DigitalOut IN2(A1);
DigitalOut IN3(A2);
DigitalOut IN4(A3);
//Second engine
DigitalOut IN5(D3);
DigitalOut IN6(D4);
DigitalOut IN7(D5);
DigitalOut IN8(D6);
//Third Engine
DigitalOut IN9(D8);
DigitalOut IN10(D9);
DigitalOut IN11(D10);
DigitalOut IN12(D11);


static int speed = 1500;
static char m_cmd = 'x';
static bool e1 = true;
static bool e2 = true;
static bool e3 = true;

static int steps1 = 0;
static int steps2 = 0;
static int steps3 = 0;


//Methods to set pins to control direction.
void stepAllRight()
{
    //engine 1
    IN1=0;
    IN2=1;
    IN3=0;
    IN4=1;
    //engine 2
    IN5=0;
    IN6=1;
    IN7=0;
    IN8=1;
    //engine 3
    IN9=0;
    IN10=1;
    IN11=0;
    IN12=1;
    wait_us(speed); //legg som global variabel "fart" 
    //engine 1
    IN1=0;
    IN2=1;
    IN3=1;
    IN4=0;
    //engine 2
    IN5=0;
    IN6=1;
    IN7=1;
    IN8=0;
    //engine 3
    IN9=0;
    IN10=1;
    IN11=1;
    IN12=0;
    wait_us(speed);
    //engine 1
    IN1=1;
    IN2=0;
    IN3=1;
    IN4=0;
    //engine 2
    IN5=1;
    IN6=0;
    IN7=1;
    IN8=0;
    //engine 3
    IN9=1;
    IN10=0;
    IN11=1;
    IN12=0;
    wait_us(speed);
    //engine 1
    IN1=1;
    IN2=0;
    IN3=0;
    IN4=1;
    //engine 2
    IN5=1;
    IN6=0;
    IN7=0;
    IN8=1;
    //engine 3
    IN9=1;
    IN10=0;
    IN11=0;
    IN12=1;
    wait_us(speed);
}
void stepAllLeft()
{
    //engine 1
    IN1=1;
    IN2=0;
    IN3=0;
    IN4=1;
    //engine 2
    IN5=1;
    IN6=0;
    IN7=0;
    IN8=1;
    //engine 3
    IN9=1;
    IN10=0;
    IN11=0;
    IN12=1;
    wait_us(speed);
    //engine 1
    IN1=1;
    IN2=0;
    IN3=1;
    IN4=0;
    //engine 2
    IN5=1;
    IN6=0;
    IN7=1;
    IN8=0;
    //engine 3
    IN9=1;
    IN10=0;
    IN11=1;
    IN12=0;
    wait_us(speed);
    //engine 1
    IN1=0;
    IN2=1;
    IN3=1;
    IN4=0;
    //engine 2
    IN5=0;
    IN6=1;
    IN7=1;
    IN8=0;
    //engine 3
    IN9=0;
    IN10=1;
    IN11=1;
    IN12=0;
    wait_us(speed);
    //engine 1
    IN1=0;
    IN2=1;
    IN3=0;
    IN4=1;
    //engine 2
    IN5=0;
    IN6=1;
    IN7=0;
    IN8=1;
    //engine 3
    IN9=0;
    IN10=1;
    IN11=0;
    IN12=1;
    wait_us(speed);
}

void stepEngine1Left()
{
    //engine 1
    IN1=1;
    IN2=0;
    IN3=0;
    IN4=1;
    wait_us(speed);
    IN1=1;
    IN2=0;
    IN3=1;
    IN4=0;
    wait_us(speed);
    IN1=0;
    IN2=1;
    IN3=1;
    IN4=0;
    wait_us(speed);
    IN1=0;
    IN2=1;
    IN3=0;
    IN4=1;
    wait_us(speed);
}

void stepEngine1Right()
{
    //engine 1
    IN1=0;
    IN2=1;
    IN3=0;
    IN4=1;
    wait_us(speed);
    IN1=0;
    IN2=1;
    IN3=1;
    IN4=0;
    wait_us(speed);
    IN1=1;
    IN2=0;
    IN3=1;
    IN4=0;
    wait_us(speed);
    IN1=1;
    IN2=0;
    IN3=0;
    IN4=1;
    wait_us(speed);
}

void stepEngine2Right()
{
    //engine 1
    IN5=0;
    IN6=1;
    IN7=0;
    IN8=1;
    wait_us(speed);
    IN5=0;
    IN6=1;
    IN7=1;
    IN8=0;
    wait_us(speed);
    IN5=1;
    IN6=0;
    IN7=1;
    IN8=0;
    wait_us(speed);
    IN5=1;
    IN6=0;
    IN7=0;
    IN8=1;
    wait_us(speed);
}

void stepEngine2Left()
{
    //engine 1
    IN5=1;
    IN6=0;
    IN7=0;
    IN8=1;
    wait_us(speed);
    IN5=1;
    IN6=0;
    IN7=1;
    IN8=0;
    wait_us(speed);
    IN5=0;
    IN6=1;
    IN7=1;
    IN8=0;
    wait_us(speed);
    IN5=0;
    IN6=1;
    IN7=0;
    IN8=1;
    wait_us(speed);
}

void stepEngine3Right()
{
    //engine 1
    IN9=0;
    IN10=1;
    IN11=0;
    IN12=1;
    wait_us(speed);
    IN9=0;
    IN10=1;
    IN11=1;
    IN12=0;
    wait_us(speed);
    IN9=1;
    IN10=0;
    IN11=1;
    IN12=0;
    wait_us(speed);
    IN9=1;
    IN10=0;
    IN11=0;
    IN12=1;
    wait_us(speed);
}

void stepEngine3Left()
{
    //engine 1
    IN9=1;
    IN10=0;
    IN11=0;
    IN12=1;
    wait_us(speed);
    IN9=1;
    IN10=0;
    IN11=1;
    IN12=0;
    wait_us(speed);
    IN9=0;
    IN10=1;
    IN11=1;
    IN12=0;
    wait_us(speed);
    IN9=0;
    IN10=1;
    IN11=0;
    IN12=1;
    wait_us(speed);
}


void step1()
{
    //engine 1
    IN1=1;
    IN2=0;
    IN3=0;
    IN4=1;
}

void step2()
{
    //engine 1
    IN1=0;
    IN2=1;
    IN3=0;
    IN4=1;
}

void wait()
{
    wait_us(speed);
    
}

//Method to release all engines.
void stopAll()
{
    IN1=0;
    IN2=0;
    IN3=0;
    IN4=0;
    IN5=0;
    IN6=0;
    IN7=0;
    IN8=0;
    IN9=0;
    IN10=0;
    IN11=0;
    IN12=0;
}

//Methods to release single engines.
void stopE1()
{
    IN1=0;
    IN2=0;
    IN3=0;
    IN4=0;
    
}
void stopE2()
{
    IN5=0;
    IN6=0;
    IN7=0;
    IN8=0;
    
}
void stopE3()
{
    IN9=0;
    IN10=0;
    IN11=0;
    IN12=0;
    
}







void input(void const *args)
{
    while(true)
    {

        if(pc.readable()) 
        { m_cmd=pc.getc(); 
        
        }
         
        Thread::wait(100);
        //pc.printf("%d", steps);
        
    }
}

//SPECIFY WHICH BUTTON IS PRESSED. NEED ONE CALLBACK FOR EACH BUTTON.
void pb_hit_callback (void) 
{
    e1 = false;
    //m_cmd = 'x';
    /*
    
    
    */
    

}


int main()
{

        pb.mode(PullUp);
        pb.attach_deasserted(&pb_hit_callback);
        pb.setSampleFrequency();

        //Thread 1 has constant feed from usb
        Thread t1(input);
        //static char global_direction;
        printf("Started");

        
        /*
        
        BOOT UP AND SYNCHRONIZE
        while (true)
        {
            should move one engine at a time. If not, alot of work has to be done in if/else statements as well as new engine methods.. 
            while(moving bottom part 180 degrees to one side  / steps < 0)
            {
                step bottom engine in one direction.
                callback will fire and stop the engine
            }
            while(moving middle part 180 degrees to one side  / steps < 0)
            {
                step middle engine in one direction
                callback fires and stops the engine
            }  
            while(moving middle part 180 degrees to one side  / steps < 0)
            {
                step middle engine in one direction
                callback fires and stops the engine
            }
            //Break the while loop (FALSE)
            break;
            
            
        }
        
        */
        
        // Move motor in one direction
        while (true)
        {
            speed = 2000;
            stepEngine1Left();
            if (e1 == false) 
            {
                Thread::wait(1000);
                int stepsToCenter = 242;
                
                while (stepsToCenter >= 0)
                {
                    stepEngine1Right();
                    stepsToCenter --;
                }
                break;
            }//end if
            
        }//end while
        /*
        while (true)
        {
            stepEngine2Right();
            if (e2 == false) 
            {
                Thread::wait(1000);
                int stepsToCenter = 242;
                
                while (stepsToCenter >= 0)
                {
                    stepEngine2Left();
                    stepsToCenter --;
                }
                break;
            }//end if
        }
        while (true)
        {
            stepEngine2Left();
            if (e3 == false) 
            {
                Thread::wait(1000);
                int stepsToCenter = 242;
                
                while (stepsToCenter >= 0)
                {
                    stepEngine3Right();
                    stepsToCenter --;
                }
                break;
            }//end if
        }

        */

        while (true) 
        {   
            speed = 1500;
            

            if(m_cmd == 'a')
            {
                stepAllRight();
            }         
            
            else if (m_cmd == 'b')
            {
                stepAllLeft();

            }
            
   /*
            else if (m_cmd == 'k')
            {
                int steps = 242;
                
                
                //Steps = 242 gives a 60' rotation to the left. 
                while (steps >= 0)
                {
                    stepAllLeft();
                    steps--;
                }
                m_cmd = 'x';
            } */

            
            //Release all motors / Set all pins to 0
            if (m_cmd == 'z')
            {
                stopAll();
            } 
            //Make all engines halt and hold still. 
            if (m_cmd == 'x')
            {
            }
  
            //Controlling each motor seperately.
            //ENGINE 1
            if(m_cmd == '7' && steps3 > -242)
            {
                
                steps3 --;
                stepEngine3Left();
            }
            else if (m_cmd == '9' && steps3 < 242)
            { 
                stepEngine3Right();
                steps3 ++;
            }
            //ENGINE 2
            else if (m_cmd == '4' && steps2 > -242)
            {
                steps2 --;
                stepEngine2Left();
            }
            else if (m_cmd == '6' && steps2 < 242)
            {
                steps2 ++;
                stepEngine2Right();
            }
            //ENGINE 3
            else if (m_cmd == '1' && steps1 > -242)
            {
                steps1--;
                stepEngine1Left();
            }
            else if (m_cmd == '3' && steps1 < 242)
            {
                steps1++;
                stepEngine1Right();
            }
            
            else if (m_cmd == 'y')
            {
                int a = 121;
                int b = 60;
                int c = 60;
                
                while (a >= 0)
                {
                    stepEngine1Left();
                    a--;
                }
                while (b >= 0)
                {
                    stepEngine2Right();
                    b--;
                }
                while (c >= 0)
                {
                    stepEngine3Left();
                    c--;
                }
                
                m_cmd = 'x';

            }
            /*
            new_pb = pb;
            if (new_pb == '1')
            {
                m_cmd ='x';
            }
            */
        //Thread::wait(100);
        //pc.printf("%d", steps3);


        }  //END WHILE TRUE
            
} //END Main