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

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

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

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

001///////////////////////
002//Moteino FF Sender  //
003///////////////////////
004 
005 
006 
007#include <SPI.h>
008#include <RFM69.h>
009#include <avr/sleep.h>
010#include <OneButton.h>
011 
012 
013// You will need to initialize the radio by telling it what ID it has and what network it's on
014// The NodeID takes values from 1-127, 0 is reserved for sending broadcast messages (send to all nodes)
015// The Network ID takes values from 0-255
016// 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}
017#define NODEID        15  //network ID used for this unit
018#define NETWORKID    99  //the network ID we are on
019#define GATEWAYID     16  //the node ID we're sending to
020//#define ACK_TIME     50  // # of ms to wait for an ack
021 
022 
023 
024//int interPacketDelay = 1000; //wait this many ms between sending packets
025char input = 0;
026int data = 0;
027 
028// Need an instance of the Radio Module
029RFM69 radio;
030byte sendSize=0;
031char payload[] = "1234567890:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ";
032bool requestACK=false;
033 
034// Setup OneButton
035OneButton playButton (A1, true);
036OneButton outButton (A2, true);
037OneButton inButton (A3, true);
038OneButton realTimebutton(A4, true);
039 
040//LEDs
041#define playLED 5          //PLay LED
042#define outLED 6           //Out LED
043#define inLED 7            //In LED
044#define realTimeLED 8      //Real Time LED
045 
046//Values for focus points
047volatile int inPoint = 0;
048volatile int outPoint = 3000;
049 
050//Blink without delay
051boolean ledState = LOW;
052long previousMillis = 0;
053int ledInterval = 75;
054 
055//Defining booleans
056boolean rClickedOnce = false;
057boolean rLongPress = false;
058boolean pClickedOnce = false;
059boolean highEndMark = false;
060boolean lowEndMark = false;
061 
062//Value to recieve from 2nd Xbee: when "play" or "rewind" has finished"
063int sum;
064int a;
065 
066//Modes
067int mode;
068#define Stop 1
069#define LENSCALIB 2
070 
071int encoder0PinA = 4;
072int encoder0PinB = 3;
073int encoderValue = 0;
074int encoder0PinALast = LOW;
075int n = LOW;
076 
077void setup() {
078  pinMode (encoder0PinA,INPUT);
079  pinMode (encoder0PinB,INPUT);
080 
081  pinMode (realTimeLED, OUTPUT);
082  pinMode (playLED, OUTPUT);
083  pinMode (inLED, OUTPUT);
084  pinMode (outLED, OUTPUT);
085 
086  radio.initialize(NODEID, RF69_433MHZ, NETWORKID);
087 
088  radio.sleep(); //sleep right away to save power
089 
090  //Attach Click to Buttons
091  realTimebutton.attachClick(Click);
092  playButton.attachClick(ClickPlay);
093  inButton.attachClick(ClickIn);
094  outButton.attachClick(ClickOut);
095 
096  //Attach Press to Real Time for calibrating lens and motor
097  realTimebutton.attachPress(rPress);
098 
099  //Start Up Light
100  for (int x=0;x<3;x++)
101  {
102    for (int l=5;l<9;l++)
103    {
104      digitalWrite(l,HIGH);
105      delay (100);
106      digitalWrite(l,LOW);
107      delay (100);
108    }
109  }
110}
111 
112void loop() {
113 
114  n = digitalRead(encoder0PinA);
115  if ((encoder0PinALast == LOW) && (n == HIGH)) {
116    if (digitalRead(encoder0PinB) == LOW) {
117      encoderValue--;
118      Transmit(1);
119    }
120    else {
121      encoderValue++;
122      Transmit(2);
123    }
124  }
125  encoder0PinALast = n;
126 
127  // keep watching the push buttons:
128  realTimebutton.tick();
129  playButton.tick();
130  inButton.tick();
131  outButton.tick();
132 
133  // filter signal from stepper motor 
134  //arduino when(play/rewind/In/Out) is done.
135  if (radio.receiveDone())
136  {
137     
138    {
139      sum = 0;
140      for (byte i = 0; i < radio.DATALEN; i++)
141      {
142        a = (radio.DATA[i]);
143      }
144      sum+=a;
145      dataSort ();
146    }
147  }
148 
149  switch (mode)
150  {
151 
152  case LENSCALIB:
153    {
154      blinkFunction (realTimeLED);
155 
156      if (lowEndMark == true && highEndMark == true ||rClickedOnce == true || pClickedOnce == true)
157      {
158        rLongPress = false;
159        digitalWrite (realTimeLED, LOW);
160        if (rClickedOnce == true)
161          digitalWrite(realTimeLED, HIGH);
162        if (pClickedOnce == true)
163          digitalWrite (playLED, HIGH);
164        mode = Stop;
165        break;
166      }
167    }
168  }
169}
170 
171//4 Buttons Click Functions
172 
173//Real Time Switch
174void Click() {
175  digitalWrite(playLED, LOW);
176  rLongPress = false;
177  if (rClickedOnce == false )
178  {
179    rClickedOnce = true;
180    digitalWrite(realTimeLED, HIGH);
181    Transmit(3);
182  }
183  else
184  {
185    digitalWrite(realTimeLED, LOW);
186    rClickedOnce = false;
187  }
188}
189// Press Function - calibrating lens and stepper
190 
191void rPress() {
192  if (rLongPress == false) {
193    rLongPress = true;
194    rClickedOnce = false;
195    highEndMark = false;
196    lowEndMark = false;
197    Transmit(4);
198    mode = LENSCALIB;
199  }
200  else
201  {
202    rLongPress = false;
203    digitalWrite(realTimeLED, LOW);
204    mode = Stop;
205  }
206}
207 
208//Play Switch
209void ClickPlay () {
210  //terminate realtime in case it wasn't stopped
211  if (pClickedOnce == false)
212  {
213    pClickedOnce = true;
214    rClickedOnce = false
215    digitalWrite(realTimeLED, LOW);
216    digitalWrite(playLED, HIGH);
217     
218    radio.send(GATEWAYID, payload, sendSize+(5));
219    radio.sleep();
220  }
221  else
222  {
223    pClickedOnce = false;
224    digitalWrite(playLED, LOW);
225  }
226}
227 
228//In Switch
229void ClickIn () {
230  // Saving In
231  if (rClickedOnce == true)                      
232  {
233    inPoint = encoderValue;
234    Transmit(6);
235  }
236  else if (rLongPress == true)
237  {
238    lowEndMark = true;
239    Transmit(8);
240  }
241}
242 
243//Out Switch
244void ClickOut() {
245  //Saving Out
246  if (rClickedOnce == true)
247  {
248    Transmit(7);
249    outPoint = encoderValue;
250  }
251  else if (rLongPress == true)
252  {
253    highEndMark = true;
254    Transmit(9);
255  }
256}
257int blinkFunction (int y)   // Blink without delay
258{
259  unsigned long currentMillis = millis ();
260  if (currentMillis - previousMillis>ledInterval)
261  {
262    previousMillis = currentMillis;
263    if (ledState == LOW)
264      ledState = HIGH;
265    else
266      ledState = LOW;
267    digitalWrite (y, ledState);
268  }
269}
270 
271int blinkMark (int y)
272{
273  boolean b = HIGH;
274  for (int i=0;i<6;i++)
275  {
276    digitalWrite(y, b);
277    delay(75);
278    b=!b;
279  }
280}
281 
282int Transmit (int data)
283{
284  //1.Send encoder value to move stepper
285  if (rClickedOnce == true||rLongPress == true)
286  {
287     
288    radio.send(GATEWAYID, payload, sendSize+(data));
289    radio.sleep();
290  }
291 
292  else if (pClickedOnce == false)
293  {
294    //2. Send speed value to move stepper
295     
296    radio.send(GATEWAYID, payload, sendSize+(data+10));
297    radio.sleep();
298  }
299}
300 
301void dataSort ()
302{
303  if (sum == 49)
304    blinkMark(inLED);
305 
306  else if (sum == 50)
307    blinkMark (outLED);
308 
309  else if (sum == 51)
310  {
311    pClickedOnce = false;
312    rClickedOnce = false;
313    digitalWrite (playLED, LOW);
314  }
315}

 

001////////////////////////
002//Moteino FF Receiver///
003////////////////////////
004 
005#include <SPI.h>
006#include <RFM69.h>
007#include <AccelStepper.h>
008#include <avr/sleep.h>
009 
010#define FREQUENCY   RF69_433MHZ
011#define IS_RFM69HW  
012 
013//encoder/motor/driver setup
014int easyDriverMicroSteps = 1;
015int rotaryEncoderSteps = 100;
016int motorStepsPerRev = 200;
017int MinPulseWidth = 50; //too low and the motor will stall, too high and it will slow it down
018 
019int easyDriverStepPin = 15;
020int easyDriverDirPin = 16;
021int enablePin = 17;
022 
023volatile long encoderValue = 0;
024long lastencoderValue = 0;
025 
026//Values for focus points
027int inPoint;
028int outPoint;
029 
030//value for variable speed
031int speedValue = 1000;
032 
033//Values for Lens Calibration
034int lowEndMark = -50000;
035int highEndMark = 50000;
036 
037boolean lowEndSwitch = false;
038boolean highEndSwitch = false;
039 
040 
041//ON LED
042#define onLed 12
043 
044int mode;
045#define Rewind 1
046#define Play 2
047// standBy mode is meant to enable changes in speed
048#define standBy 3
049#define realTime 4
050#define Stop 5
051#define LENSCALIB 6
052 
053AccelStepper stepper(1, easyDriverStepPin, easyDriverDirPin);
054 
055//Sleep Function - to disable ED when not active
056long previousMillis = 0;
057int sleepTimer = 5000;
058 
059// You will need to initialize the radio by telling it what ID it has and what network it's on
060// The NodeID takes values from 1-127, 0 is reserved for sending broadcast messages (send to all nodes)
061// The Network ID takes values from 0-255
062// 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}
063#define NODEID        16  //network ID used for this unit
064#define NETWORKID     99  //the network ID we are on
065#define GATEWAYID     15  //the node ID we're sending to
066#define SERIAL_BAUD 115200
067 
068//encryption is OPTIONAL
069//to enable encryption you will need to:
070// - provide a 16-byte encryption KEY (same on all nodes that talk encrypted)
071// - to call .Encrypt(KEY) to start encrypting
072// - to stop encrypting call .Encrypt(NULL)
073uint8_t KEY[] = "ABCDABCDABCDABCD";
074 
075//int for incoming radio data
076int a;
077int sum;
078 
079//variables for outgoing radio data
080char input = 0;
081int data = 0;
082 
083// Need an instance of the Radio Module
084RFM69 radio;
085byte sendSize=0;
086char payload[] = "1234567890:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ";
087bool requestACK=false;
088 
089 
090void setup()
091{
092  radio.initialize(FREQUENCY, NODEID, NETWORKID);
093        //comment this out to disable encryption
094 
095  stepper.setMinPulseWidth(MinPulseWidth);
096  stepper.setMaxSpeed(speedValue);             //variable to later determine speed play/rewind
097  stepper.setAcceleration(100000);
098  stepper.setSpeed(50000);
099 
100  pinMode(enablePin, OUTPUT);
101}
102 
103void loop()
104{
105  stepper.run();
106 
107  if (radio.receiveDone())
108  {   
109    lastencoderValue = encoderValue;
110    digitalWrite (enablePin, LOW);
111    previousMillis = millis();
112 
113    sum = 0;
114    for (byte i = 0; i < radio.DATALEN; i++)
115    {
116      a = (radio.DATA[i]);
117    }
118    sum+=a;
119    dataSort ();
120    stepperMove();
121 
122  }
123  else
124  {
125    //Stepper sleep after 5sec of no data
126    unsigned long currentMillis = millis ();
127    if (currentMillis - previousMillis>sleepTimer)
128      digitalWrite (enablePin, HIGH);
129  }
130 
131  switch (mode)
132  {
133 
134  case standBy:
135    break;
136 
137  case Stop:
138    break;
139 
140  case realTime:
141 
142    dataSort();
143    stepperMove();
144 
145    break;
146 
147  case Rewind:
148    //take care of variable speed
149    stepper.setMaxSpeed(speedValue);
150    stepper.moveTo(inPoint);
151    stepper.run();
152    digitalWrite (enablePin, LOW);
153    if(stepper.currentPosition()==inPoint)
154    {
155      Transmit (3);
156      mode=standBy;
157    }
158 
159    break;
160 
161  case LENSCALIB:
162    highEndMark = 50000;
163    lowEndMark = -50000;
164    lowEndSwitch = false;
165    highEndSwitch = false;
166    mode = Stop;
167    break;
168 
169  case Play:
170    //take care of variable speed
171    stepper.setMaxSpeed(speedValue);
172    stepper.moveTo(outPoint);
173    stepper.run();
174    digitalWrite (enablePin, LOW);
175    if(stepper.currentPosition()==outPoint)
176    {
177      Transmit (3);
178      mode=standBy;
179    }
180    break;
181  }
182}
183 
184void dataSort()
185{
186 
187  if (sum == 54)
188  {
189    inPoint = stepper.currentPosition();
190    Transmit (1);
191  }
192  else if (sum == 55)
193  {
194    outPoint = stepper.currentPosition();
195    Transmit (2);
196  }
197  else if (sum == 49)
198  {
199    encoderValue++;
200  }
201  else if (sum == 50)
202  {
203    encoderValue--;
204  }
205  else if (sum == 56)
206  {
207    lowEndMark = stepper.currentPosition();
208    lowEndSwitch = true;
209    Transmit (1);
210  }
211  else if (sum == 57)
212  {
213    highEndMark = stepper.currentPosition();
214    highEndSwitch = true;
215    Transmit (2);
216  }
217  else if (sum == 52)
218  {
219    mode = LENSCALIB;
220  }
221 
222  else if (sum == 51)
223  {
224    mode = Stop;
225  }
226  else if (sum == 53)
227  {
228    //function to make Play or Rewind
229    defineDirection();
230  }
231  else if (sum == 58)
232  {
233    speedValue -=30;
234    if (speedValue <100)
235      speedValue = 100;
236  }
237  else if (sum = 59)
238  {
239    speedValue+=30;
240    if (speedValue>5500)
241      speedValue = 6000;
242  }
243}
244 
245void defineDirection()
246{
247  if (stepper.currentPosition()!=inPoint)
248  {
249    mode = Rewind;
250  }
251  else if (stepper.currentPosition() == inPoint)
252  {
253    mode = Play;
254  }
255}
256 
257void stepperMove ()
258{
259  int stepsPerRotaryStep = (motorStepsPerRev * easyDriverMicroSteps) / rotaryEncoderSteps;
260  int motorMove = (encoderValue * stepsPerRotaryStep);
261  if (mode==standBy)return;
262  //Lens Calibration
263  if (motorMove >lowEndMark)
264    motorMove = lowEndMark;
265  if (motorMove<highEndMark)
266    motorMove = highEndMark;
267 
268  stepper.run();
269  stepper.setMaxSpeed(1000);
270  stepper.moveTo(motorMove);
271}
272 
273int Transmit (int data)
274{
275   
276  radio.send(GATEWAYID, payload, sendSize+(data));
277  radio.sleep();
278}

 

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

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

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

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

m_t01@mail.ru