F15: Quadcopter by Thomas

From Embedded Systems Learning Academy
Revision as of 22:46, 2 September 2015 by Proj user1 (talk | contribs) (PWM)

Jump to: navigation, search

Abstract

The goal of this project is try to implement the basic function of a drone, fly and controlling the direction by remote control.

Design & Implementation

PWM

The four motor on the done is controlled by the PWM (Pulse Width Modulation). The SJSUone board already gave us a good library to use, so we just have to set the frequency and pin at the beginning of the code.


The example of using the library.

#include "lpc_pwm.hpp"
#include "uart0.hpp"
#include "stdio.h"

#define PWM_FREQ 500

void motor_test()
{

    PWM servo1(PWM::pwm1, PWM_FREQ);
    PWM servo2(PWM::pwm2, PWM_FREQ);
    PWM servo3(PWM::pwm3, PWM_FREQ);
    PWM servo4(PWM::pwm4, PWM_FREQ);


    Uart0& terminal = Uart0::getInstance();
    terminal.init(38400);

    float p;
    int num;

    while(1){

        printf("NO=");
        scanf("%d",&num);
        printf("pwm percent=");
        scanf("%f", &p);
			
        switch(num){

            case 1:
                servo1.set(p);
                break;

            case 2:
                servo2.set(p);
                break;

            case 3:
                servo3.set(p);
                break;

            case 4:
                servo4.set(p);
                break;

            default:
                return;
        }
    }
}


Here is the diagram shows the output of the PWM. PWM_FREQ variable from the previous sample code can control the period and p variable can control the duration.

Thomas quad PWM diagram..PNG


For a drone, a shorter period can have better performance, but the range of the controlling will be smaller if we decrease the period. For example, my system is using 500 MHz PWM and my control rage will be about 53% to 100% because the motor will start to rotate at 53%. However, if we change the frequency, the motor will start to rotate at 90%, so the the control rage will be 90% to 100%. Therefore, we have to find the balance of the performance and the control rage.

For controlling the motor, we have to control them in a certain period very precisely, so delay function can not match our need. Fortunately, RTOS have the timer interrupt API that we can use. Here is part of my code.

void motor_control(void *p)
{

        TickType_t xLastWakeTime;
        int period_ms=2;//500 MHz
        const TickType_t xFrequency = MS_TO_TICK_RATE(period_ms);
        PWM servo1(PWM::pwm1, 500);
	PWM servo2(PWM::pwm2, 500);
	PWM servo3(PWM::pwm3, 500);
	PWM servo4(PWM::pwm4, 500);

        xLastWakeTime = xTaskGetTickCount();


      while(1)
	{
              

                //PID control code is in here

		vTaskDelayUntil( &xLastWakeTime, xFrequency ); //every 2ms the system will execute the code below


                #ifdef Y_AXIS_ACTIVE
		servo1.set(pServ1);  //pServ1 will be the % of output for the next period
		servo3.set(pServ3);
                #endif


                #ifdef X_AXIS_ACTIVE
		servo2.set(pServ2);
		servo4.set(pServ4);
                #endif
        }



}

The picture of the SJdrone Thomas quad SJdrone.jpg