So I got my parts in.
Went for the VNH2SP30 with a arduino mega.
Now trying to figure how to code it properly.
I want to control it by a joystick. I have one from robotdyn, the joystick arduino shield.
Having some issues with trying to have a satisfying turning function. Any tips on how to approach this issue? I have 2left and 2 right motors. Both on a separate bridge on the driver.
Here’s what I coded so far:
#include <nRF24L01.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
#define CE_PIN 47
#define CSN_PIN 48
int myArray[9]; //0 = x_pos & 1 = y_pos
int x_pos = myArray[0];
int y_pos = myArray[1];
int up_button = myArray [2];
int down_button = myArray [3];
int left_button = myArray[4];
int right_button = myArray[5];
int start_button = myArray[6];
int select_button = myArray[7];
int analog_button = myArray[8];
// Motor A
int enA = 5;
int in1 = 4;
int in2 = 9;
// Motor B
int enB = 6;
int in3 = 7;
int in4 = 8;
// Joystick Input
int joyVert = A0; // Vertical
int joyHorz = A1; // Horizontal
// Motor Speed Values - Start at zero
int MotorSpeed1R = 0;
int MotorSpeed2L = 0;
// Joystick Values - Start at 512 (middle position)
int joyposVert = 512;
int joyposHorz = 512;
RF24 radio(CE_PIN,CSN_PIN);
const uint64_t pipe = 0xE8E8F0F0E1LL;
void setup()
{
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
Serial.begin (9600);
// Set all the motor control pins to outputs
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
// // Start with motors disabled and direction forward
//
// // Motor A
//
// digitalWrite(enA, LOW);
// digitalWrite(in1, HIGH);
// digitalWrite(in2, LOW);
//
// // Motor B
//
// digitalWrite(enB, LOW);
// digitalWrite(in3, LOW);
// digitalWrite(in4, HIGH);
//
}
void loop(){
if (radio.available()){
radio.read(myArray,sizeof(myArray) );
//debug//
Serial.print("LR : ");Serial.print(myArray [0]); Serial.print(":");
Serial.print("FR : ");Serial.print(myArray [1]);Serial.print(":");
//Serial.print("Up : ");Serial.print(myArray [2]);Serial.print(":");
//Serial.print("Down : ");Serial.print(myArray [3]);Serial.print(":");
//Serial.print("Left : ");Serial.print(myArray [4]);Serial.print(":");
//Serial.print("Right : ");Serial.print(myArray [5]);Serial.print(":");
//Serial.print("Start : ");Serial.print(myArray [6]);Serial.print(":");
//Serial.print("Select : ");Serial.print(myArray [7]);Serial.print(":");
//Serial.print("Stickbutton : ");Serial.println(myArray [8]);
Serial.print(MotorSpeed1R);Serial.print(":");
Serial.println(MotorSpeed2L);
}
// Read the Joystick X and Y positions
joyposVert = myArray[1];
joyposHorz = myArray[0];
// Determine if this is a forward or backward motion
// Do this by reading the Verticle Value
// Apply results to MotorSpeed and to Direction
if (joyposVert > 546)
{
// This is Backward
// Set Motor A backward
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
// Set Motor B backward
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
//Determine Motor Speeds
// As we are going backwards we need to reverse readings
MotorSpeed1R = map(joyposVert, 526, 1023, 0, 90);
MotorSpeed2L = map(joyposVert, 526, 1023, 0, 90);
}
else if (joyposVert < 506)
{
// This is Forward
// Set Motor A forward
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
//Determine Motor Speeds
MotorSpeed1R = map(joyposVert, 506, 0, 0, 90);
MotorSpeed2L = map(joyposVert, 506, 0, 0, 90);
}
else
{
// This is Stopped
MotorSpeed1R = 0;
MotorSpeed2L = 0;
//digitalWrite(in1, LOW);
//digitalWrite(in2, LOW);
//digitalWrite(in3, LOW);
//digitalWrite(in4, LOW);
}
// Now do the steering
// The Horizontal position will "weigh" the motor speed
// Values for each motor
if (joyposHorz > 504)
{
// Move Left 1023<------|504|----|494|----|484|------>0
int xMapped = map(joyposHorz, 504,1023,0,50);
MotorSpeed1R = MotorSpeed1R - xMapped;
MotorSpeed2L = MotorSpeed2L + xMapped;
// // Don't exceed range of 0-50 for motor speeds
if (MotorSpeed1R < 0) {
MotorSpeed1R = 0;
}
if (MotorSpeed2L > 50) {
MotorSpeed2L = 50;
}
}
if (joyposHorz < 484)
{
// Move Right 1023<------|504|----|494|----|484|------>0
int xMapped = map(joyposHorz, 484,0,0,50);
MotorSpeed1R = MotorSpeed1R + xMapped;
MotorSpeed2L = MotorSpeed2L - xMapped;
// // Don't exceed range of 0-50 for motor speeds
if (MotorSpeed1R > 50) {
MotorSpeed1R = 50;
}
if (MotorSpeed2L < 0) {
MotorSpeed2L = 0;
}
}
// Adjust to prevent "buzzing" at very low speed
if (MotorSpeed1R < 8){
MotorSpeed1R = 0;
}
if (MotorSpeed2L < 8){
MotorSpeed2L = 0;
}
// Set the motor speeds
analogWrite(enA, MotorSpeed1R);
analogWrite(enB, MotorSpeed2L);
}
Problem with this setup is there’s no brake function, and the code seems to remember which direction was last used so sometimes when I steer it moves to front while steering, but sometimes it also just reverses the wheels and start to drive back while steering. Think it’s because of the CW/CCW function isn’t triggered while steering…
How can I solve this issue?
Or is it better to make more possible directions. As in. Front-left/front-right/left/right/back-left/back-right?