Notifications
Clear all

Ibus with ESP32

13 Posts
2 Users
0 Likes
1,434 Views
AlanL
(@alanl)
Member
Joined: 3 years ago
Posts: 34
Topic starter  

Build an ESP32CAM Robot Car article mentions that an ESP32 could be used as it also has mutiple UARTS which the Ibus system requires.

The article uses they Arduino Mege 2560, so I used this as a basis for an ESP version;

Code sample:

 

#include<IBusBM.h>

IBusBM IBus;  // IBus object

// For a given ESP32 serial port. rxPin and txPin can be specified for the serial ports 1 and 2 of ESP32 architectures (default to RX1=9, TX1=10, RX2=16, TX2=17).
//#define RX2  16

// Read the number of a given channel and convert to the range provided.
// If the channel is off, return the default value
intreadChannel(bytechannelInput, intminLimit, intmaxLimit, intdefaultValue) {
uint16_t ch = IBus.readChannel(channelInput);
if (ch < 100) return defaultValue;
returnmap(ch, 1000, 2000, minLimit, maxLimit);
}

// Read the channel and return a boolean value
boolreadSwitch(bytechannelInput, booldefaultValue) {
int intDefaultValue = (defaultValue) ? 100 : 0;
int ch = readChannel(channelInput, 0, 100, intDefaultValue);
return (ch > 50);
}
voidsetup() {

  // Start serial monitor
Serial.begin(115200);
IBus.begin(Serial2, 1);  // iBUS object connected to serial2 RX2 pin using timer 1
}
 
 
In order for this to compile you have to use Serial2 because Serial1 is used for other purposes.
The serial2 input is passed through a Level Logic Converter as the source signal is 5v and needs to be 3.3v for the ESP. (I am using an ESP32 DevkitC v4) 
The program uploads and runs fine, but the results for all channels is a Zero (0).
I need help to get the results that my Mega produces!
 

   
Quote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6983
 

@alanl Well you got 1 of 4. s is your code is unreadable.

Are you using a sketch designed for ann arduino Mega and trying to put it on an ESP32? That might not work.

I see you were told that Serial1 AND Serial2 are not usable, you need to choose two other pins.

I grabbed your code above and put it in the IDE, did a reformat, changed some comments to be foldable, fixed a half dozen compile errors and it now compiles clean, but obviously a bunch of code is missing. Sorry, but there are no mind readers here. The usual approach is I got some code from [link to where it came from]. I did not change a thing but it will not compile. The error is [paste the error ONLY here] OR it does compile but when I do X, it should do Y, but it does Z.

Now we have something we can work on.

 

Here is your? code back. I did a copy from the IDE, clicked the <> code button above and did a paste. Check the preview to see what it will look like.

It's obvious some of the code is missing, so do a Tools/Auto Format, clean up anything that didn't format well (any multi line comments need to be formatted as shown to enable folding. Later we will look at style.

#include <IBusBM.h>

IBusBM IBus;  // IBus object

/*
  For a given ESP32 serial port. rxPin and txPin can be specified for the serial ports 1 and 2 of ESP32 architectures 
  (default to RX1=9, TX1=10, RX2=16, TX2=17).
*/

//#define RX2  16

/*
  Read the number of a given channel and convert to the range provided.
  If the channel is off, return the default value
*/

int readChannel(byte channelInput, int minLimit, int maxLimit, int defaultValue) {
  uint16_t ch = IBus.readChannel(channelInput);
  if (ch < 100) return defaultValue;
  return map(ch, 1000, 2000, minLimit, maxLimit);
}

// Read the channel and return a boolean value
bool readSwitch(byte channelInput, bool defaultValue) {
  int intDefaultValue = (defaultValue) ? 100 : 0;
  int ch = readChannel(channelInput, 0, 100, intDefaultValue);
  return (ch > 50);
}
void setup() {
  // Start serial monitor
  Serial.begin(115200);
  IBus.begin(Serial2, 1);  // iBUS object connected to serial2 RX2 pin using timer 1
}
void loop(){}

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6983
 

@alanl Check out the Test Forum and look at my posts under Post test. I did 3 or 4 tests with different IDE's, different source code tags, <> and {;}, normal copy and Copy with Markdown.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
AlanL
(@alanl)
Member
Joined: 3 years ago
Posts: 34
Topic starter  

Ron, thanks for that, I used the {;} button instead of <>. I followed Bill's video on how to upload code and I don't recall any thing about multi line comments. Serial2 is OK to use but Serial1 i not. I got this ifo from: https://github.com/G6EJD/ESP32-Using-Hardware-Serial-Ports/blob/master/ESP32_Using_Serial2.ino


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6983
 

@alanl Ok, excellent find. Did you see where it said Serial 1 (the 2nd serial as it is 0 based) might be used by SPI?

Also there is a test sketch there, run it to make sure it will work.

If it was me, I would use the 3rd port just in case.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
AlanL
(@alanl)
Member
Joined: 3 years ago
Posts: 34
Topic starter  

Ron I have tried including the Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2); in my code atit does not get results. The reciever (iA6B) requires IBus comuneication.

I am gotng to try example from https://github.com/bmellink/IBusBM

 

#include <IBusBM.h>
#include <ESP32Servo.h>

IBusBM IBus;    // IBus object
Servo myservo;  // create servo object to control a servo

// Possible PWM GPIO pins on the ESP32: 0(used by on-board button),2,4,5(used by on-board LED),12-19,21-23,25-27,32-33 
#define servoPin 18

void setup() {
  IBus.begin(Serial2,1);        // iBUS object connected to serial2 RX2 pin using timer 1
  myservo.attach(servoPin);     // attaches the servo on pin 18 to the servo object (using timer 0)
}

void loop() {
  int val;
  val = IBus.readChannel(0); // get latest value for servo channel 1
  myservo.writeMicroseconds(val);   // sets the servo position 
  delay(20);
}

 

 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6983
 

@alanl I am sorry, I can't follow what you are doing.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
AlanL
(@alanl)
Member
Joined: 3 years ago
Posts: 34
Topic starter  

Ron I am trying to determine that ESP can read output from iA6B


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6983
 

@alanl Please use the REPLY button.

The answer is YES

What is an iA6B

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
AlanL
(@alanl)
Member
Joined: 3 years ago
Posts: 34
Topic starter  

@zander The iA6B is the Flysky Reciever.

see Bill's article: https://dronebotworkshop.com/radio-control-arduino-car/#Using_iBus_Output_8211_Arduino_Mega_2560


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6983
 

@alanl Ok, you seem to have all the info, it should work. One very common thing is to reverse RX and TX, it's a bit tricky so just try reversing them to start. If that yields nothing, use the 'normal serial just to prove everything is working, then move the wires and change the begin to the new pins first in one way then the reverse. IF that yields a working on default serial but neither connection works on serial2 then your esp32 is broken.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
AlanL
(@alanl)
Member
Joined: 3 years ago
Posts: 34
Topic starter  

Problem solved. I had to use the serial monitor in Arduino 1.8.19 because the output pushes up from the bottom. Serial monitor in Arduino 2.0.3 just keeps adding results and pushing down so you can't really see the results.

The susseful code is enclosed:

 

/*
 * There are three serial ports on the ESP known as U0UXD, U1UXD and U2UXD.
 * 
 * U0UXD is used to communicate with the ESP32 for programming and during reset/boot.
 * U1UXD is unused and can be used for your projects. Some boards use this port for SPI Flash access though
 * U2UXD is unused and can be used for your projects.
 * 
*/

// Include iBusBM Library
#include <IBusBM.h>
 
// Create iBus Object
IBusBM ibus;

// Read the number of a given channel and convert to the range provided.
// If the channel is off, return the default value
int readChannel(byte channelInput, int minLimit, int maxLimit, int defaultValue) {
  uint16_t ch = ibus.readChannel(channelInput);
  if (ch < 100) return defaultValue;
  return map(ch, 1000, 2000, minLimit, maxLimit);
}
 
// Read the channel and return a boolean value
bool readSwitch(byte channelInput, bool defaultValue) {
  int intDefaultValue = (defaultValue) ? 100 : 0;
  int ch = readChannel(channelInput, 0, 100, intDefaultValue);
  return (ch > 50);
}

void setup() {
  // Note the format for setting a serial port is as follows: Serial2.begin(baud-rate, protocol, RX pin, TX pin);
  Serial.begin(115200);
  Serial2.begin(11520, SERIAL_8N1, RXD2, TXD2);
    // Attach iBus object to serial port
  ibus.begin(Serial2);
}

void loop() { //Choose Serial1 or Serial2 as required
 // while (Serial2.available()) {
 //   Serial.print(char(Serial2.read()));
 //
   // Cycle through first 5 channels and determine values
  // Print values to serial monitor
  // Note IBusBM library labels channels starting with "0"
 
  for (byte i = 0; i < 5; i++) {
    int value = readChannel(i, -100, 100, 0);
    Serial.print("Ch");
    Serial.print(i + 1);
    Serial.print(": ");
    Serial.print(value);
    Serial.print(" | ");
  }
 
  // Print channel 6 (switch) boolean value
  Serial.print("Ch6: ");
  Serial.print(readSwitch(5, false));
  Serial.println();
 
  delay(10); 
}

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6983
 

@alanl This is why being a programmer is about a lot more than just coding. Learn how to use the IDE, you will see 3 icons on the Serial Monitor window bar on the right, the first is Autoscroll, the next is Toggle Timestamps, and the 3rd is clear the window. There are also clickable icons on the bottom bar, and the top bar. Experiment with all of them or at least hover your mouse over them to see what they say, most if not all are obvious.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote