Robotics‎ > ‎

Robo-kart


To control the Robo-kart, you can use almost the same code used to remote control a NanoMouse/LogoMouse via Bluetooth. Start with the code you developed for your mouse, and make the following modifications (create a new folder in Bitbucket for this project):

To control DC motors using the BTS7960B motor drivers, you'll need to create a new tab. Let's name is BTS7960B.h. Once you've done that, add the following code to your newly created file:
#include <Arduino.h> template <int LPWM, int RPWM> class BTS7960B { public: BTS7960B() { pinMode(LPWM,OUTPUT); pinMode(RPWM,OUTPUT); } void drive(int velocity) { if (velocity >= 0) { analogWrite(LPWM,abs(velocity)); digitalWrite(RPWM,0); } else { digitalWrite(LPWM,0); analogWrite(RPWM,abs(velocity)); } } };
The template line (3rd line) is used to relay which pins will be used to control the motors. DC motors can be controlled in many ways, but typically two pins are used to send a signal to a motor driver. The motor driver then powers the motors accordingly. By setting one GPIO pin either high or low we can control the direction the motor will turn. By varying how often the other pin is high using the analogWrite command, we can control how fast the motor turns.

Next, you'll have to modify the code within your main file. Start by swapping the line that includes the servo library with the BTS7960B library you just created.
#include <Servo.h> #include "BTS7960B.h"

Then, change how the motor objects are created:

Servo leftMotor; BTS7960<10,9> leftMotor;
Servo rightMotor; BTS7960<5,6> rightMotor;

Since your new library defines which pins are used when you create the motor objects, you can now delete the lines of code that attached your servos to pins:
  leftMotor.attach(6);
  rightMotor.attach(5);

The manner in which we send commands to the motors is different too. Instead of using the writeMicroseconds function to send values above and below 1500 (the stop signal for servo motors), we can now use the drive function in the library we created and send values between 0 and 255 to go forwards and 0 to -255 to go backwards.
    leftServo.writeMicroseconds(1500-msg[0]*5); leftMotor.drive(msg[0] * 2.55);
    rightServo.writeMicroseconds(1500+msg[1]*5); rightMotor.drive(msg[0] * 2.55);

That will get the Robo-kart moving, but we will have to make another change. Test the modifications you've made and see how they work with the Robo-kart's wheels propped off the ground.


Comments