Hello,
A quick bit of background for my project. I'm making a VR controlled driveable robot. I've got tank controls setup for my arduino, with wheels on the left and wheels on the right of my robot. So, I'm trying to map the input from two levers in VR to the two sets of wheels on my real robot.
I am trying to send data from Unity to my arduino via a Serial Connection. I have it all set up and working just fine if I was only trying to send regular old floats. However, for my application, I want to be able to control two separate values, one for the left set of wheels, and one for the right set of wheels. I don't know a great way to tell the arduino which ones are for the left wheels and which ones are for the right wheels. On top of this, I'm having a very difficult time troubleshooting because I can't use the serial monitor to print out debug statements. Any help you can offer would be great!
Unity:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO.Ports;
public class SerialSend : MonoBehaviour {
private bool sendingData = true;
static public SerialPort serial = new SerialPort("COM5", 9600);
public Transform start;
public Transform end;
private Vector3 mid;
float dist = 0;
float totalDist = 0;
public float scale(float OldMin, float OldMax, float NewMin, float NewMax, float OldValue) {
float OldRange = (OldMax - OldMin);
float NewRange = (NewMax - NewMin);
float NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin;
return (NewValue);
}
// Use this for initialization
void Awake() {
mid = (start.transform.position + end.transform.position) * 0.5f;
totalDist = Vector3.Distance(start.position, end.position);
}
void Start() {
if (serial.IsOpen == false) {
serial.Open();
}
}
// Update is called once per frame
void Update() {
dist = Vector3.Distance(mid, transform.position);
if (Vector3.Distance(start.position, transform.position) > Vector3.Distance(end.position, transform.position)) {
dist *= -1;
}
dist = scale(-1 * (totalDist / 2), totalDist / 2, -255, 255, dist);
if (serial.IsOpen == false) {
serial.Open();
}
if (sendingData == true) {
Debug.Log(this.name.ToCharArray()[0] + dist.ToString());
serial.Write( dist.ToString() + "\n");
}
}
void OnApplicationQuit() {
if (serial.IsOpen == true) {
serial.Write(0.ToString());
serial.Close();
}
Debug.Log("Application ending after " + Time.time + " seconds");
}
}
Arduino:
/* Code: BattleBot
* Version: 0.1
* By: Tyler Gragg
* Date: 2/01/2019
* For: VRBot Personal Project
*/
#include <AFMotor.h>
AF_DCMotor FL(2);
AF_DCMotor FR(1);
AF_DCMotor BL(3);
AF_DCMotor BR(4);
int motorEnabled = 0; // values for turning on system
int motor1Direction = 0; // direction for motors
int motor2Direction = 0;
int skipTime = 0; // down time for changing spin direction
int ch1; // input from reciever
int ch2; // input from reciever
int leftData = 0;
int rightData = 0;
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup() {
Serial.begin(9600);
FL.setSpeed(200);
FR.setSpeed(200);
BL.setSpeed(200);
BR.setSpeed(200);
FL.run(RELEASE);
FR.run(RELEASE);
BL.run(RELEASE);
BR.run(RELEASE);
//pinMode(13, OUTPUT);
}
// Turn right motor forword
void rightF() {
FR.run(FORWARD);
BR.run(FORWARD);
}
// Turn left motor forword
void leftF() {
FL.run(FORWARD);
BL.run(FORWARD);
}
// Turn right motor backword
void rightB() {
FR.run(BACKWARD);
BR.run(BACKWARD);
}
// Turn left motor backword
void leftB() {
FL.run(BACKWARD);
BL.run(BACKWARD);
}
void stopAll(){
FL.run(RELEASE);
FR.run(RELEASE);
BL.run(RELEASE);
BR.run(RELEASE);
}
void serialInput(){
if(Serial.available()){
//String temp = Serial.readString();
//Serial.println(temp);
recvWithEndMarker();
String receivedString(receivedChars);
if(receivedString.charAt(0) == 'L'){
leftData = receivedString.substring(1).toFloat();
}
else if(receivedString.charAt(0) == 'R'){
rightData = receivedString.substring(1).toFloat();
}
}
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void setDirection(){
if(leftData > 0){
leftF();
}
else{
leftB();
leftData *= -1;
}
if(rightData > 0){
rightF();
}
else{
rightB();
rightData *= -1;
}
}
void spinMotors(){
for (int i=0; i<255; i++) {
FR.setSpeed(i);
FL.setSpeed(i);
BR.setSpeed(i);
BL.setSpeed(i);
delay(10);
}
for (int i=255; i>0; i--) {
FR.setSpeed(i);
FL.setSpeed(i);
BR.setSpeed(i);
BL.setSpeed(i);
delay(10);
}
}
void loop() {
serialInput();
setDirection();
motorEnabled = 1;
if (motorEnabled != 1) {
stopAll();
}
else {
FR.setSpeed(rightData);
FL.setSpeed(leftData);
BR.setSpeed(rightData);
BL.setSpeed(leftData);
}
}