Беспроводное управление фокусом кинообъектива! Проблема.

Shahf2d
Offline
Зарегистрирован: 20.02.2015

Привет всем! Собрал готовый проект по управлению фокусировкой объектива!
В проекте используется moteino r4 с транссиверами rfm69hw на частоте 433mhz 

Как я полагаю проблема заключается в том что не устанавливается связь между радиомодулями!
Т.к проверка с тестовым кодом показывает что радиомодули рабочие и связь есть! 
Так же проверка с тестовым кодом показывает что moteino оптический энкодер шд и драйвер рабочие.
Остается разобраться в коде. Буду очень признателен за помощь!  С предложениями и условиями shahf2d@mail.ru
Прилагаю код проекта!
 

///////////////////////
//Moteino FF Sender  //
///////////////////////



#include <SPI.h>
#include <RFM69.h>
#include <avr/sleep.h>
#include <OneButton.h>


// You will need to initialize the radio by telling it what ID it has and what network it's on
// The NodeID takes values from 1-127, 0 is reserved for sending broadcast messages (send to all nodes)
// The Network ID takes values from 0-255
// By default the SPI-SS line used is D10 on Atmega328. You can change it by calling .SetCS(pin) where pin can be {8,9,10}
#define NODEID        15  //network ID used for this unit
#define NETWORKID    99  //the network ID we are on
#define GATEWAYID     16  //the node ID we're sending to
//#define ACK_TIME     50  // # of ms to wait for an ack



//int interPacketDelay = 1000; //wait this many ms between sending packets
char input = 0;
int data = 0;

// Need an instance of the Radio Module
RFM69 radio;
byte sendSize=0;
char payload[] = "1234567890:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ";
bool requestACK=false;

// Setup OneButton 
OneButton playButton (A1, true);
OneButton outButton (A2, true);
OneButton inButton (A3, true);
OneButton realTimebutton(A4, true);

//LEDs
#define playLED 5          //PLay LED
#define outLED 6           //Out LED
#define inLED 7            //In LED
#define realTimeLED 8      //Real Time LED

//Values for focus points
volatile int inPoint = 0;
volatile int outPoint = 3000;

//Blink without delay 
boolean ledState = LOW;
long previousMillis = 0;
int ledInterval = 75;

//Defining booleans
boolean rClickedOnce = false;
boolean rLongPress = false;
boolean pClickedOnce = false;
boolean highEndMark = false;
boolean lowEndMark = false;

//Value to recieve from 2nd Xbee: when "play" or "rewind" has finished"
int sum;
int a;

//Modes
int mode;
#define Stop 1
#define LENSCALIB 2

int encoder0PinA = 4;
int encoder0PinB = 3;
int encoderValue = 0;
int encoder0PinALast = LOW;
int n = LOW;

void setup() { 
  pinMode (encoder0PinA,INPUT);
  pinMode (encoder0PinB,INPUT);

  pinMode (realTimeLED, OUTPUT);
  pinMode (playLED, OUTPUT);
  pinMode (inLED, OUTPUT);
  pinMode (outLED, OUTPUT);

  radio.initialize(NODEID, RF69_433MHZ, NETWORKID);

  radio.sleep(); //sleep right away to save power

  //Attach Click to Buttons
  realTimebutton.attachClick(Click);
  playButton.attachClick(ClickPlay);
  inButton.attachClick(ClickIn);
  outButton.attachClick(ClickOut);

  //Attach Press to Real Time for calibrating lens and motor
  realTimebutton.attachPress(rPress);

  //Start Up Light
  for (int x=0;x<3;x++)
  {
    for (int l=5;l<9;l++)
    {
      digitalWrite(l,HIGH);
      delay (100);
      digitalWrite(l,LOW); 
      delay (100);
    }
  } 
} 

void loop() {

  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      encoderValue--;
      Transmit(1);
    } 
    else {
      encoderValue++;
      Transmit(2);
    }
  } 
  encoder0PinALast = n;

  // keep watching the push buttons:
  realTimebutton.tick();
  playButton.tick();
  inButton.tick();
  outButton.tick(); 

  // filter signal from stepper motor  
  //arduino when(play/rewind/In/Out) is done.
  if (radio.receiveDone())
  {
    
    {
      sum = 0;
      for (byte i = 0; i < radio.DATALEN; i++)
      {
        a = (radio.DATA[i]); 
      }
      sum+=a;
      dataSort ();
    } 
  }

  switch (mode)
  {

  case LENSCALIB:
    {
      blinkFunction (realTimeLED);

      if (lowEndMark == true && highEndMark == true ||rClickedOnce == true || pClickedOnce == true)
      {
        rLongPress = false;
        digitalWrite (realTimeLED, LOW);
        if (rClickedOnce == true)
          digitalWrite(realTimeLED, HIGH);
        if (pClickedOnce == true)
          digitalWrite (playLED, HIGH);
        mode = Stop;
        break;
      }
    }
  }
}

//4 Buttons Click Functions

//Real Time Switch
void Click() {
  digitalWrite(playLED, LOW);
  rLongPress = false;
  if (rClickedOnce == false )
  {
    rClickedOnce = true;
    digitalWrite(realTimeLED, HIGH); 
    Transmit(3);
  }
  else
  {
    digitalWrite(realTimeLED, LOW);
    rClickedOnce = false;
  }
}
// Press Function - calibrating lens and stepper

void rPress() {
  if (rLongPress == false) {
    rLongPress = true;
    rClickedOnce = false;
    highEndMark = false;
    lowEndMark = false;
    Transmit(4);
    mode = LENSCALIB;
  }
  else
  {
    rLongPress = false;
    digitalWrite(realTimeLED, LOW);
    mode = Stop;
  }
}

//Play Switch
void ClickPlay () {
  //terminate realtime in case it wasn't stopped
  if (pClickedOnce == false)
  {
    pClickedOnce = true;
    rClickedOnce = false;  
    digitalWrite(realTimeLED, LOW);
    digitalWrite(playLED, HIGH);
    
    radio.send(GATEWAYID, payload, sendSize+(5));
    radio.sleep();
  }
  else
  {
    pClickedOnce = false;
    digitalWrite(playLED, LOW); 
  }
}

//In Switch
void ClickIn () {
  // Saving In
  if (rClickedOnce == true)                       
  {
    inPoint = encoderValue;
    Transmit(6);
  }
  else if (rLongPress == true)
  {
    lowEndMark = true; 
    Transmit(8);
  }
}

//Out Switch
void ClickOut() {
  //Saving Out
  if (rClickedOnce == true) 
  {
    Transmit(7);
    outPoint = encoderValue;
  }
  else if (rLongPress == true)
  {
    highEndMark = true;
    Transmit(9);
  }
}
int blinkFunction (int y)   // Blink without delay
{
  unsigned long currentMillis = millis ();
  if (currentMillis - previousMillis>ledInterval)
  {
    previousMillis = currentMillis;
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
    digitalWrite (y, ledState);
  }
}

int blinkMark (int y)
{
  boolean b = HIGH;
  for (int i=0;i<6;i++)
  {
    digitalWrite(y, b);
    delay(75);
    b=!b;
  }
}

int Transmit (int data)
{ 
  //1.Send encoder value to move stepper 
  if (rClickedOnce == true||rLongPress == true)
  {
    
    radio.send(GATEWAYID, payload, sendSize+(data));
    radio.sleep();
  }

  else if (pClickedOnce == false)
  {
    //2. Send speed value to move stepper
    
    radio.send(GATEWAYID, payload, sendSize+(data+10));
    radio.sleep();
  }
}

void dataSort ()
{
  if (sum == 49)
    blinkMark(inLED);

  else if (sum == 50)
    blinkMark (outLED);

  else if (sum == 51)
  {
    pClickedOnce = false;
    rClickedOnce = false;
    digitalWrite (playLED, LOW);
  }
}

 

////////////////////////
//Moteino FF Receiver///
////////////////////////

#include <SPI.h>
#include <RFM69.h>
#include <AccelStepper.h>
#include <avr/sleep.h>

#define FREQUENCY   RF69_433MHZ
#define IS_RFM69HW   

//encoder/motor/driver setup
int easyDriverMicroSteps = 1; 
int rotaryEncoderSteps = 100; 
int motorStepsPerRev = 200; 
int MinPulseWidth = 50; //too low and the motor will stall, too high and it will slow it down

int easyDriverStepPin = 15;
int easyDriverDirPin = 16;
int enablePin = 17;

volatile long encoderValue = 0;
long lastencoderValue = 0;

//Values for focus points
int inPoint;
int outPoint;

//value for variable speed
int speedValue = 1000;

//Values for Lens Calibration
int lowEndMark = -50000;
int highEndMark = 50000;

boolean lowEndSwitch = false;
boolean highEndSwitch = false;


//ON LED
#define onLed 12

int mode;
#define Rewind 1
#define Play 2
// standBy mode is meant to enable changes in speed
#define standBy 3
#define realTime 4
#define Stop 5
#define LENSCALIB 6

AccelStepper stepper(1, easyDriverStepPin, easyDriverDirPin);

//Sleep Function - to disable ED when not active
long previousMillis = 0;
int sleepTimer = 5000;

// You will need to initialize the radio by telling it what ID it has and what network it's on
// The NodeID takes values from 1-127, 0 is reserved for sending broadcast messages (send to all nodes)
// The Network ID takes values from 0-255
// By default the SPI-SS line used is D10 on Atmega328. You can change it by calling .SetCS(pin) where pin can be {8,9,10}
#define NODEID        16  //network ID used for this unit
#define NETWORKID     99  //the network ID we are on
#define GATEWAYID     15  //the node ID we're sending to
#define SERIAL_BAUD 115200

//encryption is OPTIONAL
//to enable encryption you will need to:
// - provide a 16-byte encryption KEY (same on all nodes that talk encrypted)
// - to call .Encrypt(KEY) to start encrypting
// - to stop encrypting call .Encrypt(NULL)
uint8_t KEY[] = "ABCDABCDABCDABCD";

//int for incoming radio data
int a;
int sum;

//variables for outgoing radio data
char input = 0;
int data = 0;

// Need an instance of the Radio Module
RFM69 radio;
byte sendSize=0;
char payload[] = "1234567890:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ";
bool requestACK=false;


void setup()
{
  radio.initialize(FREQUENCY, NODEID, NETWORKID);
        //comment this out to disable encryption

  stepper.setMinPulseWidth(MinPulseWidth); 
  stepper.setMaxSpeed(speedValue);             //variable to later determine speed play/rewind
  stepper.setAcceleration(100000); 
  stepper.setSpeed(50000); 

  pinMode(enablePin, OUTPUT);
}

void loop()
{
  stepper.run();

  if (radio.receiveDone())
  {    
    lastencoderValue = encoderValue;
    digitalWrite (enablePin, LOW);
    previousMillis = millis();

    sum = 0;
    for (byte i = 0; i < radio.DATALEN; i++) 
    {
      a = (radio.DATA[i]);
    }
    sum+=a;
    dataSort ();
    stepperMove();

  }
  else
  {
    //Stepper sleep after 5sec of no data
    unsigned long currentMillis = millis ();
    if (currentMillis - previousMillis>sleepTimer)
      digitalWrite (enablePin, HIGH);
  }

  switch (mode)
  {

  case standBy:
    break;

  case Stop:
    break;

  case realTime:

    dataSort();
    stepperMove();

    break;

  case Rewind:
    //take care of variable speed
    stepper.setMaxSpeed(speedValue);
    stepper.moveTo(inPoint);
    stepper.run();
    digitalWrite (enablePin, LOW);
    if(stepper.currentPosition()==inPoint)
    {
      Transmit (3);
      mode=standBy; 
    }

    break; 

  case LENSCALIB:
    highEndMark = 50000;
    lowEndMark = -50000;
    lowEndSwitch = false;
    highEndSwitch = false;
    mode = Stop;
    break;

  case Play:
    //take care of variable speed
    stepper.setMaxSpeed(speedValue);
    stepper.moveTo(outPoint);
    stepper.run();
    digitalWrite (enablePin, LOW);
    if(stepper.currentPosition()==outPoint)
    {
      Transmit (3);
      mode=standBy;
    }
    break;
  }
}

void dataSort()
{

  if (sum == 54)
  {
    inPoint = stepper.currentPosition(); 
    Transmit (1);
  }
  else if (sum == 55)
  {
    outPoint = stepper.currentPosition();
    Transmit (2);
  }
  else if (sum == 49)
  {
    encoderValue++;
  }
  else if (sum == 50)
  {
    encoderValue--;
  }
  else if (sum == 56)
  {
    lowEndMark = stepper.currentPosition();
    lowEndSwitch = true;
    Transmit (1);
  }
  else if (sum == 57)
  {
    highEndMark = stepper.currentPosition();
    highEndSwitch = true; 
    Transmit (2);
  }
  else if (sum == 52)
  {
    mode = LENSCALIB; 
  }

  else if (sum == 51)
  {
    mode = Stop;
  }
  else if (sum == 53)
  {
    //function to make Play or Rewind
    defineDirection();
  }
  else if (sum == 58)
  {
    speedValue -=30;
    if (speedValue <100)
      speedValue = 100;
  }
  else if (sum = 59)
  {
    speedValue+=30;
    if (speedValue>5500)
      speedValue = 6000;
  }
}

void defineDirection()
{
  if (stepper.currentPosition()!=inPoint)
  { 
    mode = Rewind; 
  }
  else if (stepper.currentPosition() == inPoint)
  {
    mode = Play;
  }
}

void stepperMove ()
{
  int stepsPerRotaryStep = (motorStepsPerRev * easyDriverMicroSteps) / rotaryEncoderSteps;
  int motorMove = (encoderValue * stepsPerRotaryStep);
  if (mode==standBy)return; 
  //Lens Calibration
  if (motorMove >lowEndMark)
    motorMove = lowEndMark; 
  if (motorMove<highEndMark)
    motorMove = highEndMark;

  stepper.run();
  stepper.setMaxSpeed(1000);
  stepper.moveTo(motorMove);
}

int Transmit (int data)
{
  
  radio.send(GATEWAYID, payload, sendSize+(data));
  radio.sleep();
}

 

Shahf2d
Offline
Зарегистрирован: 20.02.2015

Выручайте! Устройство очень нужно для съемок дипломного фильма!!!

m_t
Offline
Зарегистрирован: 29.03.2012

Могу помочь. Пишите на почту 

m_t01@mail.ru