Notifications
Clear all

Adding a servo to ROV

30 Posts
3 Users
1 Likes
5,536 Views
(@chickenhawk)
Member
Joined: 4 years ago
Posts: 44
Topic starter  

Can some have a look at my wireless ROV sketch and see where I went wrong or what I missed. The movement of the ROV works great forward/backward/left/right ( joystick controlled). but when I tried to add a servo controlled by a potentiometer that's where I have run into problems, the transmitter when hooked up to the Serial monitor shows the joystick and the potentiometer are transmitting as the should. But nothing on the receiver side. Eventually I want to add 5 more servos and 2 more joysticks.

#include <SPI.h>
#include "RF24.h"

RF24 myRadio (10, 53);
byte addresses[][6] = {"0", "servo1" };

struct package
{
int X=1; // Forward & back tank
int Y=1; // Right & left tank
int servo1=1; // Camera
};

typedef struct package Package;
Package data;

void setup()
{
Serial.begin(115200);
delay(100);
myRadio.begin();
myRadio.setChannel(115);
myRadio.setPALevel(RF24_PA_MIN);
myRadio.setDataRate( RF24_250KBPS ) ;
myRadio.openWritingPipe( addresses[0]); // Joystick for tank
myRadio.openWritingPipe( addresses[1]); // Servo for camera

delay(100);
}

void loop()
{

myRadio.write(&data, sizeof(data));

Serial.print("X:");
Serial.print(data.X);
Serial.print(" Y");
Serial.println(data.Y);
Serial.println(data.servo1);
Serial.print(data.servo1);

data.X = analogRead(A0);
data.Y = analogRead(A1);
data.servo1=analogRead(A4);
delay(100);
}

 

#include <SPI.h>
#include "RF24.h"
#include <Servo.h>

RF24 myRadio (10, 53);
struct package
{
int X=512; // Motor control forward & back
int Y=512; // Motro control left & right
int servo1=1;

};

byte addresses[][6] = {"0","servo1"};
Servo myservo;

int OUT1 = 5;
int OUT2 = 6;
int OUT3 = 9;
int OUT4 = 11;

typedef struct package Package;
Package data;
int val;

void setup()
{
Serial.begin(115200);
delay(1000);

myRadio.begin();
myRadio.setChannel(115);
myRadio.setPALevel(RF24_PA_MAX);
myRadio.setDataRate( RF24_250KBPS ) ;
myRadio.openReadingPipe(1, addresses[0]); // tank forward and back and left and right
myRadio.openReadingPipe(1,addresses[1]); // camera servo

myRadio.startListening();
myservo.attach(3);

pinMode(OUT1, OUTPUT);
pinMode(OUT2, OUTPUT);
pinMode(OUT3, OUTPUT);
pinMode(OUT4, OUTPUT);
analogWrite(OUT1, 0);
analogWrite(OUT2, 0);
analogWrite(OUT3, 0);
analogWrite(OUT4, 0);
}

void loop()
{
if ( myRadio.available())
{
while (myRadio.available())
{
myRadio.read( &data, sizeof(data) );
}
val = map(data.servo1, 0, 1023, 1, 180);
myservo.write(val);
delay(15);

Serial.print("X:");
Serial.print(data.X);
Serial.print(" Y");
Serial.println(data.Y);
int X = data.X;
int Y = data.Y;
int foward = map(X,524,1024,0,255);
int backward = map(X,500,0,0,255);
int right = map(Y,524,1024,0,255);
int left = map(Y,500,0,0,255);
if(X > 524 && Y < 524 && Y > 500){
analogWrite(OUT3, foward);
analogWrite(OUT4, 0);
analogWrite(OUT1, foward);
analogWrite(OUT2, 0);
}else if(X < 500 && Y < 524 && Y > 500){
analogWrite(OUT4, backward);
analogWrite(OUT3, 0);
analogWrite(OUT2, backward);
analogWrite(OUT1, 0);
}else if(X < 524 && X > 500 && Y < 524 && Y > 500){
analogWrite(OUT4, 0);
analogWrite(OUT3, 0);
analogWrite(OUT2, 0);
analogWrite(OUT1, 0);
}else if(X < 524 && X > 500 && Y > 524){
analogWrite(OUT4, 0);
analogWrite(OUT3, left);
analogWrite(OUT2, left);
analogWrite(OUT1, 0);
}else if(X < 524 && X > 500 && Y < 500){
analogWrite(OUT4, right);
analogWrite(OUT3, 0);
analogWrite(OUT2, 0);
analogWrite(OUT1, right);
}else if(X > 524 && Y > 524){
analogWrite(OUT3, foward);
analogWrite(OUT4, 0);
analogWrite(OUT1, foward-right);
analogWrite(OUT2, 0);
}else if(X > 524 && Y < 500){
analogWrite(OUT3, foward-left);
analogWrite(OUT4, 0);
analogWrite(OUT1, foward);
analogWrite(OUT2, 0);
}else if(X < 500 && Y > 524){
analogWrite(OUT4, backward);
analogWrite(OUT3, 0);
analogWrite(OUT2, backward-right);
analogWrite(OUT1, 0);
}else if(X < 500 && Y < 500){
analogWrite(OUT4, backward-left);
analogWrite(OUT3, 0);
analogWrite(OUT2, backward);
analogWrite(OUT1, 0);
}

}

}


   
Quote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 

Just in case you might like to edit the source code before the post's time out.

https://forum.dronebotworkshop.com/question-suggestion/sticky-post-for-editing/#post-4939

1. Select the code to be posted in the Arduino IDE.

2. Right-click on  the selected code and choose "Copy as HTML" from the pop-up menu.

3. Then in the forum editor window  choose the source code icon {:} at the top of the editor.

4. Paste the  HTML code into the pop-up window and select ok.

5. You can then select "Preview" at the bottom of the post editor to see exactly how the code will appear when posted.


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 

The project looks interesting hope you will share it when you get it working.  I see it involves a camera so I am guessing you can view its output to control it?

A radio controlled car is something I haven't done yet will have to get a RF24 module.

I see Bill has a tutorial on it.  Where did your code come from?

https://dronebotworkshop.com/nrf24l01-wireless-joystick/

 


   
ReplyQuote
(@chickenhawk)
Member
Joined: 4 years ago
Posts: 44
Topic starter  

How much time do I have? Wont be back home for a 4 hours maybe longer

Do I delete the post and repost it?

This post was modified 4 years ago by chickenhawk

   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 

I think about 1 hour.  You don't have to delete a post only edit it. I suggest you just add another post with the source code properly displayed.  Adding source code is something of a pain really.  On another forum all I had to do is precede it with [code] and end it with [/code] and could edit it years later!

 


   
ReplyQuote
(@chickenhawk)
Member
Joined: 4 years ago
Posts: 44
Topic starter  

   
ReplyQuote
(@chickenhawk)
Member
Joined: 4 years ago
Posts: 44
Topic starter  

   
ReplyQuote
(@chickenhawk)
Member
Joined: 4 years ago
Posts: 44
Topic starter  

Sorry screwed up, but at lease both sketches are there


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

@chickenhawk

Looking at this doc http://tmrh20.github.io/RF24/classRF24.html#a9edc910ccc1ffcff56814b08faca5535 , I think, reading the warning there, that you should have 5-character strings with only the 1st character being different for your pipes, like "0abcd", "1abcd"

Eric


   
ReplyQuote
(@chickenhawk)
Member
Joined: 4 years ago
Posts: 44
Topic starter  

Ok I will rename the pipes to "0pipe"  and "1pipe" and try that.


   
ReplyQuote
(@chickenhawk)
Member
Joined: 4 years ago
Posts: 44
Topic starter  

No that did not work, serial monitor on receiver still reads "x" and "y" from the joy stick but nothing from the potentiometer, and servo does not move.


   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

@chickenhawk

Re-reading your initial code above it seems you are trying to have 2 pipes going from the same source to the same destination, but you just send the full block (struct) of data in one go ?

Also reading the docs (I don't have RF24's so that's my only resource...) there https://tmrh20.github.io/RF24/classRF24.html#af2e409e62d49a23e372a70b904ae30e1 , i think you could (should?) use only use 1 pipe ?

Eric


   
ReplyQuote
(@chickenhawk)
Member
Joined: 4 years ago
Posts: 44
Topic starter  

Ok I must not be understanding the use of pipes, ( I will have to go back and reread again) . My understanding was "0pipe" was to send data from the joystick to the motorcontrols and "1pipe" was to send data from the potentiometer to the servo.?????????

This post was modified 4 years ago by chickenhawk

   
ReplyQuote
(@zeferby)
Member
Joined: 5 years ago
Posts: 355
 

I may misunderstand as well since I can't experiment ? 

Is your code coming from an example ?

Eric


   
ReplyQuote
(@chickenhawk)
Member
Joined: 4 years ago
Posts: 44
Topic starter  

Part from me and 3 other sketches, 2 of the sketches used just 1 pipe. I was trying to combine them all together into my sketch. My sketch worked, and each sketch on it's own worked, it was when I tried to put them all together that I ran in to the problems.


   
ReplyQuote
Page 1 / 2