RC Robot Car - RC C...
 
Notifications
Clear all

RC Robot Car - RC Controls and Arduino

24 Posts
9 Users
3 Likes
1,855 Views
Ron
 Ron
(@zander)
Illustrious Member
Joined: 3 years ago
Posts: 4805
 

@darksplat Sorry never heard of iBus. Most boards have some PWM many have lots. The UNO has 6 PWM channels, pins 3, 5, 6, 9, 10, 11. They are used with the analogWrite() function. PWM is denoted by the ~ symbol in the pic attached

Screenshot 2022 10 28 at 08.38.07

Β 

Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting


   
ReplyQuote
(@darksplat)
Active Member
Joined: 7 months ago
Posts: 5
 

@zander if that is the case then what is the code for the PWM version in the video? Can @dronebot-workshop post a PWM version? I only need 3 PWM signals asΒ that is all that is left on the board from other functions.

@zander iBus/sBus is a protocol that the RC Brands use in there more expresnsive units that puts all of their signals on one wire as per this page

Most actually use PPM these days as it is easy and more acceptable.

  • PWM (universal)
  • PPM or CPPM (universal)
  • SBUS (Futaba, Frsky)
  • IBUS (Flysky)
  • XBUS (JR)
  • MSP (Multiwii)
  • CRSF (TBS Crossfire)
  • SPEKTRUM1024 (Spektrum DSM2)
  • SPEKTRUM2048 (Spektrum DSMX)
  • FPort (Frsky)
  • SPI_RX (universal)

Β 


   
ReplyQuote
Ron
 Ron
(@zander)
Illustrious Member
Joined: 3 years ago
Posts: 4805
 

@darksplat Sorry, I don't really understand what it is you want. I looked at the DBWS blog you provided the link to and it has PWM covered. Perhaps if you could post a sketch you want to convert to PWM and detail how many PWM channels you need and what each should do and what controls each one then maybe someone can help but just hoping Bill (DBWS) has enough spare time to cater to your needs is probably a bridge too far.

Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting


   
ReplyQuote
(@darksplat)
Active Member
Joined: 7 months ago
Posts: 5
 

@zander It doesn't have PWM covered in the sense that you can control the robot with PWM, he does have a sketch to test PWM coming from your transceiver and that works fine, but doesn't have a sketch that includes controlling your robot with PWM, he only uses iBus/sBus. Does that make more sense?

As for the code, the original code here of the project even though it has in the instructions to connect the ENA and ENB it doesn't use them it is just flat out one way or the other.

So what the bare min for our PWM requirement would be 3 Channels; Throttle, Rudder and CH5(Switch) from the transceiver we mention before. But would be happier with a more traditional layout of forward, back, left and right with CH5 acting as a mode switch.

Like I said before we can get the PWM test sketch to work and see the values coming in to the serial monitor but there is no sketch utilising PWM as the final control product only iBus/sBus.

We did get it working via bluetooth but that is not what the task has asked for it wants it done via PWM

so our bluetooth code looks like this

#include <Servo.h>
int motorLeftB = 8;   // define pin8 as left back connect with IN1
int motorLeftA = 4;   // define pin4 as left forward connect with IN2
int motorRightB = 7;  // define pin7 as right back connect with IN3
int motorRightA = 2;  // define pin2 as right back connect with IN4
int UEchoPin = A0;    // define ultrasonic receive pin (Echo)
int UTriggerPin = A1; // define ultrasonic send pin(Trig)
int Fspeedd = 0;      // forward distance
int Rspeedd = 0;      // right distance
int Lspeedd = 0;      // left distance
int directionn = 0;   // intial direction
Servo myservo;        // new myservo
int delay_time = 250; // set stable time
int Fgo = 8;          // forward
int Rgo = 6;          // turn right
int Lgo = 4;          // turn left
int Bgo = 2;          // back
char bt = 0;          // BT


bool setstate = false;
void obstacleMode();
void Bluetooth();

void setup()
{
  Serial.begin(9600);
  pinMode(motorLeftB, OUTPUT);
  pinMode(motorLeftA, OUTPUT);
  pinMode(motorRightB, OUTPUT);
  pinMode(motorRightA, OUTPUT);
  pinMode(UEchoPin, INPUT);
  pinMode(UTriggerPin, OUTPUT);
  myservo.attach(11); // define the servo pin(PWM)
}
void advance(int f) // forward
{
  digitalWrite(motorRightB, HIGH);
  digitalWrite(motorRightA, LOW);
  digitalWrite(motorLeftB, HIGH);
  digitalWrite(motorLeftA, LOW);
  delay(f * 15);
}
void turnR(int r) //turn right
{
  digitalWrite(motorRightB, LOW);
  digitalWrite(motorRightA, LOW);
  digitalWrite(motorLeftB, HIGH);
  digitalWrite(motorLeftA, LOW);
  delay(r * 50);
}
void turnL(int l) //turn left
{
  digitalWrite(motorRightB, LOW);
  digitalWrite(motorRightA, LOW);
  digitalWrite(motorLeftB, LOW);
  digitalWrite(motorLeftA, HIGH);
  delay(l * 50);
}
void stopp(int x) //stop
{
  digitalWrite(motorRightB, LOW);
  digitalWrite(motorRightA, LOW);
  digitalWrite(motorLeftB, LOW);
  digitalWrite(motorLeftA, LOW);
  delay(x * 100);
}
void back(int b) //back
{
  digitalWrite(motorRightB, LOW);
  digitalWrite(motorRightA, HIGH);
  digitalWrite(motorLeftB, LOW);
  digitalWrite(motorLeftA, HIGH);
  delay(b * 300);
}
void detection() //test the distance of different direction
{
  int delay_time = 250; //
  delay(200);
  ask_pin_F(); // read forward distance
  if (Fspeedd < 10) // if distance less then 10
  {
    stopp(1);
    back(2);
  }
  if (Fspeedd < 25) // if distance less then 10
  {
    stopp(1);
    ask_pin_L();
    delay(delay_time);
    ask_pin_R();
    delay(delay_time);
    if (Lspeedd > Rspeedd) //if left distance more than right distance
    {
      directionn = Rgo;
    }
    if (Lspeedd <= Rspeedd) //if left distance not more than right
      //distance
    {
      directionn = Lgo;
    }
    //if left if (Lspeedd < 10 && Rspeedd < 10) distance and right
    //distance both less than 10
    {
      directionn = Bgo;
    }
  }
  else
  {
    directionn = Fgo; // forward go
  }
}
void ask_pin_F() // test forward distance
{
  myservo.write(90);
  digitalWrite(UTriggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(UTriggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(UTriggerPin, LOW);
  float Fdistance = pulseIn(UEchoPin, HIGH);
  Fdistance = Fdistance / 5.8 / 10;
  Serial.print("F distance:");
  Serial.println(Fdistance);
  Fspeedd = Fdistance;
}
void ask_pin_L() // test left distance
{
  myservo.write(150);
  delay(delay_time);
  digitalWrite(UTriggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(UTriggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(UTriggerPin, LOW);
  float Ldistance = pulseIn(UEchoPin, HIGH);
  Ldistance = Ldistance / 5.8 / 10;
  Serial.print("L distance:");
  Serial.println(Ldistance);
  Lspeedd = Ldistance;
}
void ask_pin_R() // test right distance
{
  myservo.write(20);
  delay(delay_time);
  digitalWrite(UTriggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(UTriggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(UTriggerPin, LOW);
  float Rdistance = pulseIn(UEchoPin, HIGH);
  Rdistance = Rdistance / 5.8 / 10;
  Serial.print("R distance:");
  Serial.println(Rdistance);
  Rspeedd = Rdistance;
}
void loop()
{
switch(setstate){
case 1:
Bluetooth();
Serial.print("in bluetooth mode");
Serial.println();
break;
case 0:
obstacleMode();
Serial.println();
Serial.print("in Obstacle mode");
Serial.println();
break;
}
{
if (Serial.available() > 0){bt = Serial.read();
      //  digitalWrite(led, 1);
      if (bt == 'C')
{
  setstate = ! setstate;
}  
Serial.print(setstate);
Serial.println();
Serial.print(bt);
Serial.println();

}}}
    
void obstacleMode()
{
  myservo.write(90);
  detection();
  if (directionn == 2)
  {
    back(3);
    turnL(2);
    Serial.print("Movement: Reverse ");
  }
  if (directionn == 6)
  {
    back(1);
    turnR(6);
    Serial.print("Movement: Right ");
  }
  if (directionn == 4)
  {
    back(1);
    turnL(6);
    Serial.print("Movement: Left ");
  }
  if (directionn == 8)
  {
    advance(1);
    Serial.print("Movement: Advance ");
    Serial.print(" ");
  }
}




void Bluetooth(){
/*if (Serial.available() > 0)
    {
      bt = Serial.read();
      //  digitalWrite(led, 1);
*/
      if (bt == 'F')       //move forwards
      {
        digitalWrite(motorRightA, LOW);
        digitalWrite(motorRightB, HIGH);
        digitalWrite(motorLeftA,  LOW);
        digitalWrite(motorLeftB, HIGH);
      }
      else if (bt == 'B')       //move backwards
      {
        digitalWrite(motorRightA, HIGH);
        digitalWrite(motorRightB, LOW);
        digitalWrite(motorLeftA, HIGH);
        digitalWrite(motorLeftB, LOW);
      }
      else if (bt == 'X')     //stop!!
      {
        digitalWrite(motorRightA, LOW);
        digitalWrite(motorRightB, LOW);
        digitalWrite(motorLeftA, LOW);
        digitalWrite(motorLeftB, LOW);
      }
      else if (bt == 'R')    //right
      {
        digitalWrite(motorRightA, HIGH);
        digitalWrite(motorRightB, LOW);
        digitalWrite(motorLeftA, LOW);
        digitalWrite(motorLeftB, LOW);
      }
      else if (bt == 'L')     //left
      {
        digitalWrite(motorRightA, LOW);
        digitalWrite(motorRightB, LOW);
        digitalWrite(motorLeftA, HIGH);
        digitalWrite(motorLeftB, LOW);
      }  
  
}

  
    
 
     /*_________________________________________________________________________________________________*/
    
    
    /*  else if (bt == 'C')    //Select need to implement this
      {
        digitalWrite(motorRightA, HIGH);
        digitalWrite(motorRightB, LOW);
        digitalWrite(motorLeftA, LOW);
        digitalWrite(motorLeftB, HIGH);
      }
      else if (bt == 's')    //Start
      {
        digitalWrite(motorRightA, LOW);
        digitalWrite(motorRightB, HIGH);
        digitalWrite(motorLeftA, HIGH);
        digitalWrite(motorLeftB, LOW);
        */
      
  

Β 

I hope I don't sound hash just trying to convoy what we are after a very frustrating couple of weekends πŸ™

Cheers

Β 

Β 

Β 

Β 


   
ReplyQuote
Ron
 Ron
(@zander)
Illustrious Member
Joined: 3 years ago
Posts: 4805
 

@darksplat What does the following mean?

We did get it working via bluetooth but that is not what the task has asked for it wants it done via PWM

so our bluetooth code looks like this

I hope I don't sound hash just trying to convoy what we are after a very

It sounds like this is team. Is this for school or something?

Β 

Β 

Β 

Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting


   
ReplyQuote
(@darksplat)
Active Member
Joined: 7 months ago
Posts: 5
 

@zander my son and IΒ 


   
ReplyQuote
Ron
 Ron
(@zander)
Illustrious Member
Joined: 3 years ago
Posts: 4805
 

@darksplat Cool. Your answer earlier confused me though, I gave you the answer and you acted like I didn't tell you. Now that I see the sketch, what I would do is simply replace the digitalWrite with analogWrite. Here is the arduino documentation

https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/

Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting


   
ReplyQuote
Ron
 Ron
(@zander)
Illustrious Member
Joined: 3 years ago
Posts: 4805
 

@darksplat You may find the following article (video as well) instructive.

https://dronebotworkshop.com/servo-motors-with-arduino/

Arduino says and I agree, in general, the const keyword is preferred for defining constants and should be used instead of #define
"Never wrestle with a pig....the pig loves it and you end up covered in mud..." anon
My experience hours are >75,000 and I stopped counting in 2004.
Major Languages - 360 Macro Assembler, Intel Assembler, PLI/1, Pascal, C plus numerous job control and scripting


   
ReplyQuote
Will
 Will
(@will)
Famed Member
Joined: 2 years ago
Posts: 2326
 
Posted by: @darksplat

@zander It doesn't have PWM covered in the sense that you can control the robot with PWM, he does have a sketch to test PWM coming from your transceiver and that works fine, but doesn't have a sketch that includes controlling your robot with PWM, he only uses iBus/sBus. Does that make more sense?

Since you say that you're able to see the PWM signal sent from your transmitter on your Arduino and since you say you have a working sketch that uses Bluetooth, i don't really understand your problem.

It seems that all you'd need to do is read in the three remaining channels from your transmitter using PWM channel interpretation as described in Bill's sketch (as you have successfully read it already) and use those three channels' signals to invoke the associated code in your sketch.

From the code you posted, you're not using PWM to the LN298 anyway so your existing code for the different directives for Bluetooth should be completely compatible with the signals decoded from the PWM channel data. You'll just have to interpret the signals to extract left/right/forward/back equivalents from the raw values sent.

Experience is what you get when you don't get what you want.


   
Ron reacted
ReplyQuote
Page 2 / 2