2016年3月29日 星期二

RX1002 + 遙控器 + 馬達....



新增

pin  2, 3, 8, 9 去驅動馬達 .....


接收器接腳如下

左手   上下 : 油門  (第一通道)     pin 7

            左右 :  YAW (第四通道)    pin 4

 右手   上下 : PITCH (第三通道)    pin 5

             左右    ROLL (第二通道)   pin 6

code 如下:




#include <Servo.h>
#include <PinChangeInt.h>



#define THR_PIN  7
#define ROLL_PIN  6
#define PITCH_PIN  5
#define YAW_PIN  4  // PITH
#define SERIAL_BAND 9600

#define  FL_PIN  2
#define  FR_PIN  3
#define  BL_PIN  8
#define  BR_PIN  9

#define  thr_min  1100
#define  thr_max  1900

#define SERVO_NUM 4

unsigned int thr_start_time=0, thr_time=0;
unsigned int yaw_start_time=0, yaw_time=0;
unsigned int roll_start_time=0, roll_time=0;
unsigned int pitch_start_time=0, pitch_time=0;
unsigned int thr_val;

Servo MOTOR[SERVO_NUM];

void setup()
{

   Serial.begin(SERIAL_BAND);
  // while (!Serial) {}
 
    Serial.println("Init done");
   pinMode(THR_PIN, INPUT);
   pinMode(YAW_PIN, INPUT);
   pinMode(PITCH_PIN, INPUT);
   pinMode(ROLL_PIN, INPUT);
   //digitalWrite(UPPER_LIMIT_PIN, HIGH);
   PCintPort::attachInterrupt(THR_PIN, THR_Function, CHANGE );
   PCintPort::attachInterrupt(YAW_PIN, YAW_Function, CHANGE );
   PCintPort::attachInterrupt(PITCH_PIN, PITCH_Function, CHANGE );
   PCintPort::attachInterrupt(ROLL_PIN, ROLL_Function, CHANGE );
 
  MOTOR[0].attach(FL_PIN);
  MOTOR[1].attach(FR_PIN);
  MOTOR[2].attach(BL_PIN);
  MOTOR[3].attach(BR_PIN);

   for (int i=0;i<SERVO_NUM;i++)
      MOTOR[i].writeMicroseconds(thr_min);
 
}


void loop()
{
   while(Serial.available() > 0)
  {  
    char c = Serial.read();  // get it
     Serial.println(thr_time);
     //Serial.println(yaw_time);
     //Serial.println(pitch_time);
     //Serial.println(roll_time);
     Serial.println(thr_val);
  }
  /*
  thr_val =   (( long)(thr_time - thr_min)) * 100 / (thr_max - thr_min) + 137;

  //  1100/2040 *255 ~ 1900/2040*255  ->  137 ~ 237

  if(thr_val >=255)
      thr_val = 255;
  else if(thr_val <=0)
      thr_val = 0;


  analogWrite(FL_PIN,thr_val);
  analogWrite(FR_PIN,thr_val);
  analogWrite(BL_PIN,thr_val);
  analogWrite(BR_PIN,thr_val);
  */

   for (int i=0;i<SERVO_NUM;i++)
MOTOR[i].writeMicroseconds(thr_time);

}

void  THR_Function()
{
    if(digitalRead(THR_PIN)==HIGH) //  rising
       thr_start_time = micros();
    else
          thr_time  = micros() - thr_start_time;
}

void  YAW_Function()
{
    if(digitalRead(YAW_PIN)==HIGH) //  rising
       yaw_start_time = micros();
    else
          yaw_time  = micros() - yaw_start_time;
}

void  PITCH_Function()
{
    if(digitalRead(PITCH_PIN)==HIGH) //  rising
       pitch_start_time = micros();
    else
          pitch_time  = micros() - pitch_start_time;
}

void  ROLL_Function()
{
    if(digitalRead(ROLL_PIN)==HIGH) //  rising
       roll_start_time = micros();
    else
          roll_time  = micros() - roll_start_time;
}

沒有留言:

張貼留言