Servo Shield and RC Transmitter via Arduino Uno

For other supported Arduino products from Adafruit: Shields, accessories, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
headbyte
 
Posts: 12
Joined: Wed Oct 20, 2021 12:22 pm

Servo Shield and RC Transmitter via Arduino Uno

Post by headbyte »

Hello,
I am new to programming. I have built a little tank using an Arduino Uno, Motor Shield (v2.3) and a Flysky i6x RC Transmitter. The tracks work fine but I also have a little "gun" turret on top controlled by 2 servos (1 for sideways and the other for up and down). They were very jittery and intermittent and after exhaustive investigation, I discovered there was a "Servo Timer 2" library that might fix this. I wasn't able to get that to work and then I read online that it was better to have them controlled by a separate servo shield. I have bought, prepared and addressed the servo shield so it stacks on top of my motor shield. I can't seem to find any example code to control the shield from my RC transmitter. Also "Servo Party" isn't working either. I have made sure the header assemblies are all correct and not shorting out. I really only need to control 2 Servo's and wondered if there was any code available for input from my Flysky i6X transmitter. Thank you

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo Shield and RC Transmitter via Arduino Uno

Post by adafruit_support_bill »

I have bought, prepared and addressed the servo shield so it stacks on top of my motor shield. I can't seem to find any example code to control the shield from my RC transmitter.
We don't have any example code for your transmitter. But if you have used it to control your motors, it should not be difficult to adapt to controlling servos.
Also "Servo Party" isn't working either.
Please post some photos showing all of your soldering and connections to the shield.

User avatar
headbyte
 
Posts: 12
Joined: Wed Oct 20, 2021 12:22 pm

Re: Servo Shield and RC Transmitter via Arduino Uno

Post by headbyte »

Thank you for your reply. I have successfully gotten "Servo Party" to work. Now I just need to figure out how to "apply" code to get the transmitter to work. I truly don't know where to start. I (kind of) understood how to write the code for the old Servo library but I'm lost with this new one. Maybe I will need to hire someone to help me. It's a bummer because I think I have like 6 lines of code left to do and my tank will be complete.

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo Shield and RC Transmitter via Arduino Uno

Post by adafruit_support_bill »

For best accuracy, you need to calibrate your servo to determine the min and max pulse lengths. https://learn.adafruit.com/adafruit-16- ... 1825149-10
The values in the example code are a good start:

Code: Select all

#define SERVOMIN  150 // This is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX  600 // This is the 'maximum' pulse length count (out of 4096)
Then you can use the Map function to map angular positions to pulse length: https://learn.adafruit.com/adafruit-16- ... 1825151-12

Then pass the calculated pulse length to the setPWM function:

Code: Select all

pwm.setPWM(servonum, 0, pulselen);

User avatar
headbyte
 
Posts: 12
Joined: Wed Oct 20, 2021 12:22 pm

Re: Servo Shield and RC Transmitter via Arduino Uno

Post by headbyte »

Thank you so much for this info. I just wasn't sure about how to substitute the code I was using. It's been an "amalgam" because I started w/ Motor Shield v1 then v2 and now I have the Servo Shield stacked on the v2. Everything else seems to be working fine.

User avatar
headbyte
 
Posts: 12
Joined: Wed Oct 20, 2021 12:22 pm

Re: Servo Shield and RC Transmitter via Arduino Uno

Post by headbyte »

How do I assign a pin to be read by the Servo Shield and not the Motor Shield?

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo Shield and RC Transmitter via Arduino Uno

Post by adafruit_support_bill »

No need to assign pins. Both shields use the i2c bus. And as shipped they have different default addresses (0x60 vs 0x40) so there is no need to reconfigure any addressing.

User avatar
headbyte
 
Posts: 12
Joined: Wed Oct 20, 2021 12:22 pm

Re: Servo Shield and RC Transmitter via Arduino Uno

Post by headbyte »

I mean my input pin. I used to have to assign an INT then attach to Pin 9 (or Pin 10) then assign "get servo". Is all that still the same?

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo Shield and RC Transmitter via Arduino Uno

Post by adafruit_support_bill »

There are no input pins on either shield. All communication is via the i2c bus. If you want to use other GPIO pins for other purposes, they are passed through directly via the headers.

User avatar
headbyte
 
Posts: 12
Joined: Wed Oct 20, 2021 12:22 pm

Re: Servo Shield and RC Transmitter via Arduino Uno

Post by headbyte »

Right, I understand that. And communication IS going to the Arduino Uno (through the headers). I was just curious how it's assigned to the Servo Shield instead of the Motor Shield. My motors and my switch for my little light cannon (on top of servos) works fine. But how do I tell the sketch to use say pins A2 & A3 to control servos
0 & 1? That's the part I'm not grocking. I appreciate your replies because I just can't seem to find this info anywhere.

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: Servo Shield and RC Transmitter via Arduino Uno

Post by adafruit_support_bill »

I was just curious how it's assigned to the Servo Shield instead of the Motor Shield.
i2c is a bus and each device on the bus has a unique address. i2c commands addressed to the motor shield re delivered to the motor shield. i2c commands addressed to the servo shield are delivered to the servo shield.
But how do I tell the sketch to use say pins A2 & A3 to control servos
0 & 1?
You don't. There is zero correlation between servo channels on the shield and pins on your Arduino. The PCA9685 is an entirely self-contained PWM generator chip with it's own output pins. You specify which PCA9685 servo channel as the first parameter in your call to setPWM(). https://learn.adafruit.com/adafruit-16- ... f-335628-3

User avatar
headbyte
 
Posts: 12
Joined: Wed Oct 20, 2021 12:22 pm

Re: Servo Shield and RC Transmitter via Arduino Uno

Post by headbyte »

I have addressed the Servo Shield as 0x41 on the i2C bus (via soldering) and Servo Party works. So you're saying that in my sketch, the commands for the servos don't go "through" the Arduino (or Motor Shield)? They are completely sovereign to the Servo Shield — and thus do not interfere with those commands, which is what I'm hoping for because Servo commands were totally gumming up the works for the motors (tracks) and my "Light Cannon".

PWM Test showed that commands were making it from transmitter to receiver good. But once I used my final sketch, as long as Servos and Channels from from FS-iA6B were disconnected (and not part of sketch), everything worked fine. The minute I added Servos (this was before I bought Servo Shield), every time I would "touch" Channels 3 or 4 (Left Joystick on transmitter), everything went off the rails. I'm sorry if I seem a little "dense" about this but as I said, I'm very new at this. My coding chops are REALLY limited ... but I'm trying. Thanks again.

User avatar
headbyte
 
Posts: 12
Joined: Wed Oct 20, 2021 12:22 pm

Re: Servo Shield and RC Transmitter via Arduino Uno

Post by headbyte »

So how do I get this code

Code: Select all


#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
#include <Servo.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();

Adafruit_DCMotor *lmotor = AFMS.getMotor(2);
Adafruit_DCMotor *rmotor = AFMS.getMotor(4);
Servo servo1;
Servo servo2;

int ch1, ch2, ch3, ch4, ch5, ltrack, rtrack;
const int in1 = 5, in2 = 6, in3 = A2, in4 = A3, in5 = 3;


void setup() {
  Serial.begin(9600);
  AFMS.begin(1000);
  pinMode(in1, INPUT);
  pinMode(in2, INPUT);
  pinMode(in3, INPUT);
  pinMode(in4, INPUT);
  pinMode(in5, INPUT);
  pinMode(2, OUTPUT);
  
  lmotor->setSpeed(255);
  lmotor->run(RELEASE);
  rmotor->setSpeed(255);
  rmotor->run(RELEASE);
  servo1.attach(9);
  servo2.attach(10);  
}

void loop() {
  ch1 = pulseIn(in1, HIGH, 35000);
  ch2 = pulseIn(in2, HIGH, 35000);
  ch3 = pulseIn(in3, HIGH, 50000);
  ch4 = pulseIn(in4, HIGH, 50000);
  
  Serial.print("1: ");
  Serial.print(ch1);
  Serial.print(", 2: ");
  Serial.print(ch2);
  Serial.print(", 3: ");
  Serial.print(ch3);
  Serial.print(", 4: ");
  Serial.print(ch4);


  if (ch1 != 0 || ch2 != 0 || ch3 != 0 || ch4 != 0) {
    ch1 = constrain(ch1, 970, 1990);
    ch2 = constrain(ch2, 970, 1990);
    ch3 = constrain(ch3, 970, 1990);
    ch4 = constrain(ch4, 970, 1990);
    ch1 = map(ch1, 970, 1990, -100, 100);
    ch2 = map(ch2, 970, 1990, -100, 100);
    ch3 = map(ch3, 970, 1990, -100, 100);
    ch4 = map(ch4, 970, 1990, -100, 100);

    servo1.write(map(ch4, -100, 100, 0, 180));
    servo2.write(map(ch3, -100, 100, 0, 180));


    ltrack = ch2 - ch1;
    rtrack = ch2 + ch1;
    ltrack = constrain(ltrack, -100, 100);
    rtrack = constrain(rtrack, -100, 100);
    if (ltrack > 5) {
      lmotor->run(FORWARD);
      lmotor->setSpeed(map(ltrack, 6, 100, 0, 255));
    } else if (ltrack < -5) {
      lmotor->run(BACKWARD);
      lmotor->setSpeed(map(ltrack, -6, -100, 0, 255));
    } else {
      lmotor->run(RELEASE);
    }
    if (rtrack > 5) {
      rmotor->run(FORWARD);
      rmotor->setSpeed(map(rtrack, 6, 100, 0, 255));
    } else if (rtrack < -5) {
      rmotor->run(BACKWARD);
      rmotor->setSpeed(map(rtrack, -6, -100, 0, 255));
    } else {
      rmotor->run(RELEASE);
    }
  } else {
    lmotor->run(RELEASE);
    rmotor->run(RELEASE);
  }


  ch5 = pulseIn(3, HIGH);
  if (ch5 > 1700) {
    digitalWrite(2, HIGH);
  }
  else {
    digitalWrite(2, LOW);
  }
  Serial.print(" SwitchA ");
  Serial.println(ch5);
  delay(10);

}
To work with the Servo Shield?

User avatar
headbyte
 
Posts: 12
Joined: Wed Oct 20, 2021 12:22 pm

Re: Servo Shield and RC Transmitter via Arduino Uno

Post by headbyte »

I didn't want to do this until it was done so as to not spoil the surprise with a "Half baked cake" but here's "Frankie my Tanky" LOL
Last edited by headbyte on Thu Oct 21, 2021 10:22 pm, edited 1 time in total.

User avatar
headbyte
 
Posts: 12
Joined: Wed Oct 20, 2021 12:22 pm

Re: Servo Shield and RC Transmitter via Arduino Uno

Post by headbyte »

This should've been first

Locked
Please be positive and constructive with your questions and comments.

Return to “Other Arduino products from Adafruit”