Alright, so I'm close to being able to drive my RC car around. I'm able to do a little controlling at my desk. (I have to fix some stuff with the way my xbox controller's interface mapping.)
I'm gonna walk through some of the stuff I did to do this since my last post.
I first hooked up my motors to my power banks to see if I could get them to drive. Apologies for the vertical video, dunno what I was thinking....
So after that I had to get everything all wired up to the Arduino. I think the shield's perf board section ended up being really helpful, and I had to do a lot of jank wiring to get everything set up properly. It's an absolute rat's nest, but it works.
I also cut apart a USB A to A cable to connect to my old USB powerbanks. Unfortunately, I didn't realize the current draw for the motors was more than what the USB powerbank could handle, and they shut off because of presumably some safety feature for like a short or something. I was however able to use 2 in parallel, and it seems to work fine.
So my final set up is pictured below.
With the hardware in place, I needed to get my code in place.
Being as lazy as I am, I looked at a motor driver library I had used back in grad school and used it for a test program I wrote:
This program basically just cycled through some different values being sent to the motors via PWM.
I was able to see that varying the turning motor speed didn't really yield any intermediate turning angles. It's still a very ternary steering system, either straight right or left. I also realized that for many of the values being sent through, there's a large "dead band" where the motors don't even move. That aside I knew then, I could hook this up to ROS!
Writing very little code, I whipped this up:
Basically it can use the twist message that I tested in the turtle sim post I made a while back and directly use it on the Arduino. One of the main problems is that I can at the same time have max steering and max velocity. This means I can't turn currently, and I'll need to fix that. I think likely the next update I make will feature an updated controller mapping so I can actually drive this car around. You can see the current state of things here:
I'm gonna walk through some of the stuff I did to do this since my last post.
I first hooked up my motors to my power banks to see if I could get them to drive. Apologies for the vertical video, dunno what I was thinking....
So after that I had to get everything all wired up to the Arduino. I think the shield's perf board section ended up being really helpful, and I had to do a lot of jank wiring to get everything set up properly. It's an absolute rat's nest, but it works.
I also cut apart a USB A to A cable to connect to my old USB powerbanks. Unfortunately, I didn't realize the current draw for the motors was more than what the USB powerbank could handle, and they shut off because of presumably some safety feature for like a short or something. I was however able to use 2 in parallel, and it seems to work fine.
So my final set up is pictured below.
With the hardware in place, I needed to get my code in place.
Being as lazy as I am, I looked at a motor driver library I had used back in grad school and used it for a test program I wrote:
/* | |
Motor test program excersizes 3 motors on the motor driver | |
*/ | |
#include "motor_controller.h" | |
#define M1_IN1 2 | |
#define M1_IN2 4 | |
#define M1_ENA 3 | |
#define M2_IN1 5 | |
#define M2_IN2 7 | |
#define M2_ENA 6 | |
#define MAX_VEL 255 | |
#define VEL_STEP 5 | |
#define NUM_STEP (255/5) | |
MotorController motorController1(M1_IN1, M1_IN2, M1_ENA); | |
MotorController motorController2(M2_IN1, M2_IN2, M2_ENA); | |
// the setup routine runs once when you press reset: | |
void setup() { | |
} | |
// the loop routine runs over and over again forever: | |
void loop() { | |
static int velocity = -MAX_VEL; | |
static int dir = 1; | |
if (velocity > 255) { | |
dir = -1; | |
} | |
if (velocity < -255) | |
{ | |
dir = 1; | |
} | |
motorController1.turnMotor(velocity); | |
motorController2.turnMotor(velocity); | |
velocity += 5*dir; | |
delay(200); | |
} |
This program basically just cycled through some different values being sent to the motors via PWM.
I was able to see that varying the turning motor speed didn't really yield any intermediate turning angles. It's still a very ternary steering system, either straight right or left. I also realized that for many of the values being sent through, there's a large "dead band" where the motors don't even move. That aside I knew then, I could hook this up to ROS!
Writing very little code, I whipped this up:
/* | |
* rosserial Subscriber Example | |
* Blinks an LED on callback | |
*/ | |
#include <ros.h> | |
#include <geometry_msgs/Twist.h> | |
#include "motor_controller.h" | |
#define M1_IN1 2 | |
#define M1_IN2 4 | |
#define M1_ENA 3 | |
#define M2_IN1 5 | |
#define M2_IN2 7 | |
#define M2_ENA 6 | |
MotorController motorController1(M1_IN1, M1_IN2, M1_ENA); | |
MotorController motorController2(M2_IN1, M2_IN2, M2_ENA); | |
ros::NodeHandle nh; | |
void messageCb( const geometry_msgs::Twist& twist_msg){ | |
long x_lin_vel = map(twist_msg.linear.x*100, -50, 50, -255, 255); | |
long z_ang_vel = map(twist_msg.angular.z*100, -50, 50, -255, 255); | |
motorController1.turnMotor(x_lin_vel); | |
motorController2.turnMotor(z_ang_vel); | |
} | |
ros::Subscriber<geometry_msgs::Twist> sub("cmd_vel", &messageCb ); | |
void setup() | |
{ | |
nh.initNode(); | |
nh.subscribe(sub); | |
} | |
void loop() | |
{ | |
nh.spinOnce(); | |
delay(1); | |
} |
Basically it can use the twist message that I tested in the turtle sim post I made a while back and directly use it on the Arduino. One of the main problems is that I can at the same time have max steering and max velocity. This means I can't turn currently, and I'll need to fix that. I think likely the next update I make will feature an updated controller mapping so I can actually drive this car around. You can see the current state of things here:
PS
I've also worked a bit more of my cover EP stuff, and I've gotten distracted and want to cover more songs... It's so hard sometimes because there's so many songs out there that I feel like I'd like to cover. Anyway, I want to hopefully get to recording before September.
Comments
Post a Comment