LeafBot electronics

website version1.0

On the site of http://www.dutchrobotgames.nl/, see picture below, I found information (sorry it is in Dutch) how to rework the windshield wiper motors (I will now use the abbreviation WSWM). On an WSWM there are several connections for the low and high speed and interval position. The high speed and interval isn't needed on the LeafBot. So these have to be removed.



To have a look how much current the motors would take the motors where attached to an external power supply. But first check on with voltage the WSWM run. 12V!



The external power supply.



In the website of http://www.dutchrobotgames.nl/ it is discribed that the interval system sould be removed. The WSWM had to be taken apart. First the removal of the wire harness.



Test the motors if they will spin at 12Volts. They do.



The pink part is the part that is for the interval position of the WSWM. This part had to be removed.



Next is open de motor it self to understand which wire is the GND and which is for the high and low speed.



The 2 blue wires are responsable for the speed. The black one is the GND. Now its time to find with of the 2 blue wires is the highspeed. the small metal wire springs pressing the 3 magnetes to the diassembled rotor. For the assembly the springs had to be dis-assembled to reduce the springload. Otherwise the rotor could not be assembled, because it was not possible to push all 3 magnetes in there sliders. The measurement of turning the motors backwards shows that the current is higher. It can caused by wear in the motors because they have turned in the cars always in the same direction and not in two directions. But they also could have been that the bearings are optimised to this specific load profile.



The middel black wire is the GND. The other 2 blue wires had to be indentified which is the high speed. The high speed should be removed so the leafbot moves slow over the lawn. Otherwise the brushes should also spin faster to pickup all the leafs. And driving faster and spinning the brushes faster will consume more energie.



Identifiing the high and low speed. Note the differents in current in both 2 pictures below.



This is the low speed. The other blue wire should be removed.



The other motor is connected to the power supply. Note the differents in current between this motor and the other one. Both motors are from an old car with could have done different mileage and therefor different wear in them. My gues is it could be that the Leafbot will not be driving in a straight line because both motors will not spin with the same RPM.



Luis advised me to use motor drivers from Pololu. But I also asked some Saxion students what sort driver they used for there own project. They advised me to use the Pololu Dual VNH5019 Motor Driver Shield. It seemed to me a good solution to go for this dual driver, which can operate both wheels. I will use a separate driver for the brushes that I made in the weeky assignment of Week13. This dual driver has an 5V output to power the arduino uno PCB. But I needed, according to the support website of Pololu, to solder an jumper on the Pololu driver to connect Vout of the driver to Vin of the Arduino.





This Pololu Dual driver is capable to handle high current and suitable to be used on 12V. See the specs in the picture below. As what measured before, the WSWM using without load around 1,5A. The driver can handle 12A continuously and 24A as a peak output. I ordered the drivers at www.Robotshop.com. After a few days waiting they arrived on time.



Because the NRF24L01 2.4GHz Radio/Wireless Transceiver uses the standard SPI bus on the Arduino Uno, pin 11 + 12 + 13, I had to remap the wiring of the motor controller. The motor driver uses the same pins as the SPI bus. According to the support website of the motor driver, see picture below, certain conections had to be cut.



In the 3 pictures below I cut the connections with an knife.







Next step is to connect the Pololu driver to the Arduino Uno. The connected wires in the picture below are remapped connections. This is according to the information in the support page.



First step to connect the Adruino + driver to the electric motors of the LeafBot. The position of the Arduino and Driver are placed on the side of the LeafBot to prevent that the drivers would getting to hot when placed bottom side of the LeafBot. It was not clear in this stage of the development, how how hot the drivers will be during the driving of the LeafBot. Also placing the electronics at the side of the robot gives it an good access.



Connecting wires to the electric motor wires.



Next step is connection the GND (black wire) and 12V VCC (red wire) to the Pololu driver. The driver has an blue connection block where these wires can be connected with an screw joint. The NRF24L01 2.4GHz Radio/Wireless Transceiver is connected also to the pins of the driver. The GND and 12V VCC wires are connected to an 12V Lead acid battery I got from my college Patrick. He had some left which I could use.



To test the remote control, first step was to connect the second NRF24L01 2.4GHz Radio/Wireless Transceiver (middle below) to the second Arduino uno. Then the push button to turn on the brushes (left on the picture) and the joystick (right below on the picture)



Because of a shortage of 5V pins on the Arduino, and this is also the case with the fabkit that I will use later to replace this Arduino Uno PCB, the VCC wire of the Joystick and push Button, see on the middel of this picture below in the background, are connected to each other.



A detailed view of the NRF24L01 2.4GHz Radio/Wireless Transceiver



To operate both wheels an differential driver must be programmed. See wikipedia. The first step is was to let the robot drive only forward and backwards with the joystick. Because only 1 joystick is used, both the x-axis (the direction) and Y-axis (throttle) of the joystick had to be mixed together. These values where then used in the Arduino Sketch which contained the code for the differential control of both motors. But first only one resistor is programmed.

In the library for the Pololu Dual VNH5019 Motor Driver Shield, see picture below, Set speed and direction for motor 1. Speed should be between -400 and 400. 400 corresponds to motor current flowing from M1A to M1B. -400 corresponds to motor current flowing from M1B to M1A. 0 corresponds to full coast.



One of the two motors have to turn anti clockwise, because it is placed otherway around. The second driver must then get a different value then the first motor driver. The joystick is analog and sould be connected to the analog pin of the Arduino Uno. The values from the joystick ranges from 255 to 767 (the differents between these values is 512). The code to the driver will be =map(value from joystick,255,767,-400,400). First the values of the joystick transfered to the motordriver value. The value of the second driver, with has to turn anti clockwise, is set in the negative value status by multiplying the =map( ) value into a negative value. The second motor driver values -400,400 will then be changed in to 400,-400.

An new arduino sketch was programmed. This picture below showing the code of the loop.



First test with one motor controlled by the joystick

First test with 2 motors running and one resistor operating in the joystick. The joystick is connected by wire to the LeafBot

It worked. Time to implement also the Y-axis of the joystick. See picture below.



First test with 2 motors running and both resistors operated in the joystick. The joystick is connected by wire to the LeafBot

Time to testride the robot for the first time, with wire connected joystick. Yessss it drives and it drives very nicely. The robot is very good to control and the the speed of the Leafbot is slow, but exactly what I want to have. So first test is very succesfull.

Next step is to implement the NRF24L01 2.4GHz Radio/Wireless Transceiver and make the remote control ready to be tested. First an second Arduino uno is used to build up the remote control. It was planned during the test to power it from the Labtop.



In this picture the connections of the NRF24L01 2.4GHz Radio/Wireless Transceiver to the Arduino Uno is displayed. See also picture below this picture how to connect the NRF24L01 2.4GHz Radio/Wireless Transceiver.



Blue hatched the connections of the NRF24L01 2.4GHz Radio/Wireless Transceiver to the Arduino pins



In this picture below an oveview of the the potensio meter (top), NRF24L01 2.4GHz Radio/Wireless Transceiver (middle) and joystick (below) connected to the Arduino Uno.



First step in building up the remote control. First with the use of the Arduino uno, second step is to replace the Arduino Uno with the fabkit.



To use the Arduino Uno in the new bigger sized remote control housing, is not possible, because the pins have an vertical connection and the fabkit an horizontal pin connection. This makes the remote control smaller in dimensions.



The joystick has two analog outputs. The X-axis and Y-axis. These outputs have been connected to the Analog pins of the Arduino. See the blue highlighted connections.

Now is was time to surge for an Arduino Sketch to connect the two NRF24L01 2.4GHz Radio/Wireless Transceiver to each other, because the transmitting was not succesfull. The reason was the wrong adres to transmit to. From the website an Arduino nRF24L01 TX – Transmitter Sketch and nRF24L01 RX – Receiver Sketch is found and downloaded. From these sketches some important input was copied into the main sketch. With these extra information, how to make the connection between the two NRF24L01 2.4GHz Radio/Wireless Transceivers, the connection was established.

Below the transmitter code and from it the transmitting adres with was needed.



Below the receiver code and from it the transmitting adres with was copied.



To transmit the data from the joystick, and also the push buttons and potensio meter, an Sketch is used from JSON. This is also copied in the Arduino Library.

This sketch is used to transmit the data.



This code below is used to transceive the data.



Below the final transmitting sketch. It referes to several libraries. The libraries are copied in the Arduino Library folder

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

#include <ArduinoJson.h>

#define JOYSTICK_PIN_X A4
#define JOYSTICK_PIN_Y A5
#define BRUSH_POTENTIOMETER A3
#define BUTTON_1 2
#define BUTTON_2 4

#define CE_PIN   9
#define CSN_PIN 10

const uint64_t pipe = 0xE8E8F0F0E1LL;
char sendBuffer[50]; 
int button_1, button_2, button_3 = false;
int lastStateButton_1, lastStateButton_2, lastStateButton_3 = false;

StaticJsonBuffer<200> jsonSendBuffer;
RF24 radio(CE_PIN, CSN_PIN);

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_1, INPUT_PULLUP);
  pinMode(BUTTON_2, INPUT_PULLUP);

  radio.begin();
  radio.openWritingPipe(pipe);
}

void loop() {
  //sendBuffer[0];
  //memset(sendBuffer, 0, sizeof(sendBuffer));
  
  int valX = analogRead(JOYSTICK_PIN_X);
  int valY = analogRead(JOYSTICK_PIN_Y);
  int valBrush_Motor = analogRead(BRUSH_POTENTIOMETER);

  button_1 = !digitalRead(BUTTON_1);
  button_2 = !digitalRead(BUTTON_2);
  
  JsonObject& root = jsonSendBuffer.createObject();
  JsonArray& data = root.createNestedArray("remote");
  data.add(valX);
  data.add(valY);
  data.add(valBrush_Motor);
  data.add(button_1);
  data.add(button_2);

  // {"remote":[518,514,1,1,1]}
  root.printTo(sendBuffer, sizeof(sendBuffer));

  bool ok = radio.write(&sendBuffer, sizeof(sendBuffer));
  Serial.println(sendBuffer);
  
  if(ok) {
    Serial.println("Transfer succes!");
  } else {
    Serial.println("Transfer failure!");
  }
  
  //Serial.println("");
  jsonSendBuffer = StaticJsonBuffer<200>();
  
  delay(100);
}


Below the receiving sketch

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

#include <ArduinoJson.h>
#include <DualVNH5019MotorShield.h>

// For the motor shield
#define INA1      2
#define INB1      4
#define EN1DIAG1  6
#define CS1       A0
#define INA2      3
#define INB2      5
#define EN2DIAG2  A5
#define CS2       A1

// For the brush motor
#define BRUSH_SPEED 255
#define BRUSH_MOTOR_PIN A4

// For the wireless //
#define MAX_BUFFER_SIZE 255
#define CE_PIN    8
#define CSN_PIN   7

const uint64_t pipe = 0xE8E8F0F0E1LL;
StaticJsonBuffer<200> jsonRecvBuffer;

int val_Y, val_X = 0;                     // Temporary variable to store 
int valBrush_Motor = 0;
int valBrush = 0;
int throttle, direction = 0;              // Throttle (Y axis) and direction (X axis) 
int leftMotor,leftMotorScaled = 0;        // Left Motor helper variables
int rightMotor,rightMotorScaled = 0;      // Right Motor helper variables
float leftMotorScale = 0;
float rightMotorScale = 0;
float maxMotorScale = 0;                  // Holds the mixed output scaling factor

char recvMsg[MAX_BUFFER_SIZE];
uint16_t len = 0;
uint8_t button_1 = 0;
uint8_t button_2 = 0;

// Changed the physical pins on the driver-shield, so we have to declare the pins manually:
DualVNH5019MotorShield motorDriver(INA1, INB1, EN1DIAG1, CS1, INA2, INB2, EN2DIAG2, CS2);
RF24 radio(CE_PIN, CSN_PIN);

int returnInvertedValue(int toInvert) {
  return toInvert * -1;
}

void rotateBrush(int brush_speed) {
  analogWrite(BRUSH_MOTOR_PIN, brush_speed);
}

void nRF_receive() {
  len = 0;
  if ( radio.available() ) {
    len = radio.getDynamicPayloadSize();
    radio.read(&recvMsg, len);
    delay(5);
    Serial.println(recvMsg);
    
    if(strlen(recvMsg) != 0) {
      JsonObject& root = jsonRecvBuffer.parseObject(recvMsg);
      
      /* Message received... Process here...      * For the processing of the wireless packet, we're using JSON (JavaScript Object Notation) here.      * Using the arduinoJson-library, we can easily parse the incoming message.      *       * We're using the following message structure, using JSON structure: (This is what's being transmitted)      * {"remote":[joystick_x,joystick_y,potmeter_brush,button_1,button_2]}      *        */
      
      if (!root.success()) {
        Serial.println("parseObject() failed");
      }

      // Now parse the message
      val_X = root["remote"][0];
      val_Y = root["remote"][1];
      valBrush_Motor = root["remote"][2];
      button_1 = root["remote"][3];
      button_2 = root["remote"][4];

      // TODO: Toggle

      // Clear the recieving buffer
      recvMsg[len] = 0;
      recvMsg[0] = 0;
    }
  }
  // Clear the recieving buffers
  recvMsg[len] = 0;
  recvMsg[0] = 0;
  jsonRecvBuffer = StaticJsonBuffer<200>();
  delay(20);
}  

void setup()  { 
  Serial.begin(9600);
  Serial.println("Starting reciever...");
  motorDriver.init();
  
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();
}

void loop()  { 
  // Call the receive-function every beginning of the loop
  nRF_receive();

  // Map the value of the potentiometer for the brush motor
  valBrush = map(valBrush_Motor, 0, 1024, 0, 255);

  // Turn the brush motor on (or not...)
  if(valBrush > 10) {
    rotateBrush(valBrush);
  } else {
    rotateBrush(0);
  }

  // Check for empty values
  if((val_X != 0) || (val_Y != 0)) {

    // Map the values of the joysticks to proper values for the motor controller
    throttle = map(val_Y, 255, 767, 400, -400);
    direction = map(val_X, 255, 767, -400, 400);

    //----------------- Differential drive code -------------//
    // Since we're only using 1 joystick, we've got to mix the direction (X-axis) and the throttle (Y-axis)
  
    leftMotor = throttle + direction;
    rightMotor = throttle - direction;
  
    leftMotorScale =  leftMotor / 400.0;
    leftMotorScale = abs(leftMotorScale);
    rightMotorScale =  rightMotor / 400.0;
    rightMotorScale = abs(rightMotorScale);
  
    maxMotorScale = max(leftMotorScale,rightMotorScale);
    maxMotorScale = max(1,maxMotorScale);
  
    leftMotorScaled = constrain(leftMotor/maxMotorScale,-400,400);
    rightMotorScaled = constrain(rightMotor/maxMotorScale,-400,400);

    // Use the library of the motordriver-shield to set the speeds of the motors
    motorDriver.setM1Speed(leftMotorScaled);
    motorDriver.setM2Speed(rightMotorScaled);
    delay(50);
  }
}


I had a mistake. I had copied the libraries in an wrong folder, so the sketch could not find the libraries.



Now they are placed in the right folder.



Now the first data could be transmitted. The first 2 values are the joystick values, the other 3 the potension meter and 2 push button (last two buttons are not connected)



Small problem. I had uploaded the transmitting file into the receiving Arduino placed on the leafBot. At this moment the joystick was powered by the laptop. And not by the 9V battery.



And of course no Data is transmitted.



Next step was to change the Arduino uno in the remote control for the fabkit. All the wires where plugged on to the fabkit. The 9V battery was connected to the 9V to 3,3V regulater PCB that I made in Week15. In week 15 I already had configurated the connection of the pins to the FabKit. See the last pictures in week 15 website page. From this regulator the FabKit was supplied with 3,3V. The Arduino transmitting sketch was uploaded, with went well. Next step was to transmit data to the LeafBot. Data was transmitted to the LeafBot. This went well.





But no data was received by the LeafBot. The Arduino Uno was reconnected and the same was done again. The Data from the remote control was send and received. The remoted control was again rebuild with the fabkit, but again data was transmitted, but was not received by the leafBot. I haven't found the root course for this problem. The system isn't giving mistakes. Data is transmitted, but the LeafBot doesn't pickup the signals. It was Tuesday 14 of June. I had to present the next day on 15 June my final presentation. So I had to decide to present the LeafBot with the remote controle controlled by the Arduino Uno instead of the fabkit.



After rebuilding the remote control again with the Arduino Uno the first remote controlled testride could take place.

First testride with radio controlled joystick

Second testride with radio controlled joystick

The potensio meter on the remote control was turned on and the brushes starting to spin. Brushes making an Funny sound.

First test of the spinning brushes

Special thanks goes to my college Patrick who helped me in training me to learn Arduino programming and in electronics in general.

Schematic Pololu Dual VNH5019 Motor Driver



Arduino library for Pololu Dual VNH5019 Motor Driver



Arduino sketches of the remote control



Arduino library