Go Down

Topic: RC Car controlled Wirelesly (Read 13394 times) previous topic - next topic

AllSystemGO

Sure thing. Here's my Arduino code

Code: [Select]

#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <Bridge.h>
#include <Console.h>
#include <Wire.h>
#include <YunServer.h>
#include <YunClient.h>

Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *frontWheels = AFMS.getMotor(2);
// You can also make another motor on port M2
Adafruit_DCMotor *rearWheels = AFMS.getMotor(1);

const int ledPin = 13; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into

// We open the socket 5678 to communicate.
YunServer server(5678);

void setup() {
  Serial.begin(9600);

  // Bridge startup
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  Bridge.begin();
  digitalWrite(ledPin, HIGH);

  AFMS.begin();  // create with the default frequency 1.6KHz

  // Set the speed to start, from 0 (off) to 255 (max speed)
  frontWheels->setSpeed(0);
  frontWheels->run(FORWARD);
  // turn on motor
  frontWheels->run(RELEASE);

  //OTHER MOTOR
  rearWheels->setSpeed(0);
  rearWheels->run(FORWARD);
  // turn on motor
  rearWheels->run(RELEASE);

  server.noListenOnLocalhost();
  server.begin();
}

void loop() {
  uint8_t i;
  YunClient client = server.accept();

  // There is a new client?
  if (client) {
    String command = "none";
    digitalWrite(ledPin, LOW);

    while (!command.equals("stop")) {
      if (client.available() > 0) {
        command = client.readString();
        command.trim();

        Serial.println(command);

        if ( command.equals("back") ) {
          rearWheels->run(RELEASE);
          rearWheels->setSpeed(255);
          rearWheels->run(BACKWARD);
        }
        else if (command.equals("forward") ) {
          rearWheels->run(RELEASE);
          rearWheels->setSpeed(255);
          rearWheels->run(FORWARD);
        }
        else if ( command.equals("brake") ) {
          rearWheels->run(RELEASE);
          frontWheels->run(RELEASE);
        }
        else if ( command.equals("left") ) {
          frontWheels->run(RELEASE);
          frontWheels->setSpeed(150);
          frontWheels->run(FORWARD);
        }
        else if ( command.equals("right") ) {
          frontWheels->run(RELEASE);
          frontWheels->setSpeed(150);
          frontWheels->run(BACKWARD);
        }
      }
    }
    digitalWrite(ledPin, HIGH);
    client.stop();
  }
}


And my C# code just incase

Code: [Select]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RCController
{
    public partial class Form1 : Form
    {
        byte[] msg;
        byte[] bytes = new byte[256];
        Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                msg = Encoding.UTF8.GetBytes("back");

                try
                {
                    // Blocks until send returns.
                    int i = s.Send(msg);
                }
                catch (SocketException z)
                {
                    Console.WriteLine(z.Message);
                }
            }
            else if (e.KeyCode == Keys.Up)
            {
                msg = Encoding.UTF8.GetBytes("forward");

                try
                {
                    // Blocks until send returns.
                    int i = s.Send(msg);
                }
                catch (SocketException z)
                {
                    Console.WriteLine(z.Message);
                }
            }
            else if (e.KeyCode == Keys.Left)
            {
                msg = Encoding.UTF8.GetBytes("left");

                try
                {
                    // Blocks until send returns.
                    int i = s.Send(msg);
                }
                catch (SocketException z)
                {
                    Console.WriteLine(z.Message);
                }
            }
            else if (e.KeyCode == Keys.Right)
            {
                msg = Encoding.UTF8.GetBytes("right");

                try
                {
                    // Blocks until send returns.
                    int i = s.Send(msg);
                }
                catch (SocketException z)
                {
                    Console.WriteLine(z.Message);
                }
            }
            else if (e.KeyCode == Keys.ShiftKey)
            {
                msg = Encoding.UTF8.GetBytes("brake");

                try
                {
                    // Blocks until send returns.
                    int i = s.Send(msg);
                }
                catch (SocketException z)
                {
                    Console.WriteLine(z.Message);
                }
            }
        }

        private void connexionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IPAddress[] IPs = Dns.GetHostAddresses("192.168.240.1");

            s.Connect(IPs[0], 5678);

            msg = Encoding.UTF8.GetBytes("This is a test");
            try
            {
                // Blocks until send returns.
                int i = s.Send(msg);
            }
            catch (SocketException z)
            {
                Console.WriteLine(z.Message);
            }

        }

        private void deconnexionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            msg = Encoding.UTF8.GetBytes("stop");
            try
            {
                // Blocks until send returns.
                int i = s.Send(msg);
            }
            catch (SocketException z)
            {
               Console.WriteLine(z.Message);
            }
            s.Disconnect(true);
        }
    }
}


Thank you for the help

AllSystemGO

You see anything wrong with the code Frederico?

cmaglie

I see that you put some debugging prints, may you post the output?

(both C# and Arduino)
C.

AllSystemGO

My problem isn't with the output of my command cause that works perfectly, my problem is with the 2 seconds delay between my soft and the arduino giving the command. I tried different approach in my arduino code like, changing from a string to convert to a Int and switching my if to a switch.. case and the effect is still there. I even tried changing my string to a char and the 2 seconds delay is always there.

PS: Something else that I tried is using telnet as my command giver instead of my c# program and the delay remains.

AllSystemGO

FYI : adding a delay at the end, delay(500); and even changing the Serial speed to 115200 didn't change a thing.

I'm running out of ideas, if anyone has something else to try let me know.

Thanks

federicofissore

We are deep diving into Bridge code. The bottle neck seems that readString. We'll keep you posted

federicofissore

#21
Oct 30, 2013, 10:33 am Last Edit: Oct 30, 2013, 10:41 am by Federico Fissore Reason: 1
Turned out it wasn't a Bridge issue, but rather how Stream::timedRead works
It waits 1 second after having received the last character
Modify your sketch this way
Code: [Select]

if (client) {
 client.setTimeout(5);

It should become nearly instantaneous.

Tuning timeout depends on the quality of your connection: if it's poor and the timeout is too short, you may end up with incomplete commands

AllSystemGO

I'll test this out as soon as I get home tonight. Thank you Frederico, you guys rock.

AllSystemGO

Hey Frederico,

   You were right, the bottleneck is with the readString. After putting setTimeout(5); everything works flawlessly.

I had a glitch when I tried it and I don't know if it's because of the change but the board stopped responding after the second command. All the lights on the board started to flash and even after rebooting I couldn't access it via WiFI. I had to connect it USB and push my scketch again.

It is ok now and since then I didn't get that problem again.

Thank you Frederico for your time.


cagiva

We built a similar project.  However, we installed an Android device onto a RC car to control it autonomously. Here is the preliminary result:

http://youtu.be/vkvkfcqEUkk


newbotuser

AllSystemGO , Can you please share your final code(Sketch)? I am having trouble controlling my car.

Go Up