Advertisement
Guest User

Untitled

a guest
Mar 4th, 2017
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.65 KB | None | 0 0
  1. // NRF24 based radio receiver for radio controlled tractors
  2. // by modelleicher
  3. // NRF24 Library: https://github.com/TMRh20/RF24
  4. // Inspiration by: "RC Tractor Guy" on youtube https://www.youtube.com/user/magicalmachines/videos
  5. // Also great thanks to Antony Cartwright for the best NRF24 tutorial on youtube (bare basics explanation) https://www.youtube.com/watch?v=Qn2YLSYz3WM
  6.  
  7. // work in progress, just a start for now.
  8. // March 2017
  9.  
  10.  
  11.  
  12. #include <SPI.h>
  13. #include "RF24.h"
  14.  
  15. #include <Servo.h>
  16.  
  17.  
  18. // right now we are only receiving 5 bytes..
  19. uint8_t dataPackage[5];
  20.  
  21. // initialize the RF24
  22. RF24 radio(9, 10);
  23.  
  24. // again, the pipe. Important, Sender and Receiver have to use the same, obviously.
  25. const uint64_t pipe = 1;
  26.  
  27. // the pins for the motor driver.
  28. // first two are used to select driving direction, third one uses PWM to control speed of the motor
  29. int pin_driveMotor_dir1 = 5;
  30. int pin_driveMotor_dir2 = 4;
  31. int pin_driveMotor_pwm = 3;
  32.  
  33.  
  34.  
  35.  
  36. // not used yet
  37. int dt = 0;
  38. int lastMillis = 0;
  39.  
  40. // initialize the steering servo
  41. Servo servo1;
  42.  
  43. void setup() {
  44.   // serial for debug
  45.   Serial.begin(9600);
  46.   // begin radio
  47.   radio.begin();
  48.   // no need to set PA level anything higher than min as long as the communication is only one way.
  49.   radio.setPALevel(RF24_PA_MIN);
  50.   // debug
  51.   Serial.println("started..");
  52.   // since we are the receiver, open a reading pipe
  53.   radio.openReadingPipe(1, pipe);
  54.   // debug
  55.   Serial.println("opened..");
  56.   // start listening (now we are a receiver and listening for packets)
  57.   radio.startListening();
  58.   //debug
  59.   Serial.println("listening..");
  60.  
  61.   // attach the steering servo at pin 7
  62.   servo1.attach(7);
  63. }
  64.  
  65. void loop() {
  66.   // not used yet
  67.   dt = millis() - lastMillis;
  68.  
  69.   // check if there are packets received. radio.availiable() will return true if there is anything in the buffer
  70.   if (radio.available())
  71.   {
  72.     //read the data and store in dataPackage array.
  73.     radio.read(dataPackage, sizeof(dataPackage));
  74.     // current array index to axis:
  75.     // 1 = X
  76.     // 2 = Y
  77.     // 3 = RX
  78.     // 4 = RY
  79.  
  80.     // control drive motor
  81.     if (dataPackage[4] > 135) // larger than 130 -> forwards
  82.     {
  83.         analogWrite(pin_driveMotor_pwm, map(dataPackage[4], 135, 255, 0, 255)); // write the PWM value
  84.         // set direction:
  85.         digitalWrite(pin_driveMotor_dir1, HIGH);
  86.         digitalWrite(pin_driveMotor_dir2, LOW);
  87.     }
  88.     else if (dataPackage[4] < 115) // smaller than 120 -> backwards
  89.     {
  90.         analogWrite(pin_driveMotor_pwm, map(dataPackage[4], 115, 0, 0, 255)); // write PWM value
  91.         // set direction:
  92.         digitalWrite(pin_driveMotor_dir1, LOW);
  93.         digitalWrite(pin_driveMotor_dir2, HIGH);      
  94.     }
  95.     else // stop driving
  96.     {
  97.         analogWrite(pin_driveMotor_pwm, 0);
  98.         digitalWrite(pin_driveMotor_dir1, LOW);
  99.         digitalWrite(pin_driveMotor_dir2, LOW);    
  100.     }
  101.  
  102.  
  103.     // steering trim on Deutz D16006 - min 50 max 120
  104.  
  105.     // map the steering value to servo min/max values
  106.     dataPackage[1] = map(dataPackage[1], 0, 255, 50, 120);
  107.  
  108.     // debug
  109.     Serial.println(dataPackage[4]);
  110.  
  111.     // set the servo value
  112.     servo1.write(dataPackage[1]);  
  113.  
  114.     // debug
  115.     //Serial.println(dataPackage[1]);
  116.     //Serial.println(dataPackage[2]);
  117.     //Serial.println(dataPackage[3]);    
  118.     //Serial.println("----------------------");    
  119.   }
  120.   else
  121.   {
  122.      // this is called each time the loop runs and there is no data in the buffer from the NRF24
  123.     //Serial.println("Not Available!");
  124.   }
  125.  
  126.   // not sure if the dalay is needet.
  127.   delay(10);
  128.   //Serial.println("Wait...");
  129.  
  130.   // not used yet
  131.   lastMillis = millis();
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement