Ok, so how would you change this? is there a library that you know off hand? I tried using the multi serial library witch looks exactly like this;
void setup() { // initialize both serial ports: Serial.begin(9600); Serial1.begin(9600); } void loop() { // read from port 1, send to port 0: if (Serial1.available()) { int inByte = Serial1.read(); Serial.write(inByte); } // read from port 0, send to port 1: if (Serial.available()) { int inByte = Serial.read(); Serial1.write(inByte); } }
So I'm kinda confused because this example seams to be sending something from one serial port to another serial port although its not really sending anything. So this example is pretty confusing
Also, in C, a single '=' is used to assign a value. When performing a logical test you should use '=='.
I tried changing that part and got error msg ISO C++ forbids comparison between pointer and integer [-fpermissive}
I forgot to tell you that you'll need to change your string to a byte value (since you're only reading one byte from Serial2.
Anything seems possible when you don't know what you're talking about.
So I'm kinda confused because this example seams to be sending something from one serial port to another serial port although its not really sending anything. So this example is pretty confusing
You're right, it IS confusing because it's pretty pointless 🙂
Google "Serial.readBytes()" for a better example of getting a string. Remember that you'll need to provide a buffer for the result and determine a maximum size for the buffer (which will be just large enough for your maximum message length).
I would also suggest using shorter messages, like "ML" for move left, etc.
Anything seems possible when you don't know what you're talking about.
AccelStepper uses a different strategy to move. You set a target position for the stepper and then call the run() method each time through the loop. You can also tell it to move directly to the target (thereby blocking all other activity until it reaches that target).
Thats awesome @will but I dont think I should need all the Extras. Take in the fact of how it should be working with the object tracking
In the object tracking I want to use Open cv to build borders on the screen and from there when the object being tracked enters these borders it needs to tell the Arduino simple commands (ie. if object enters right border = turn right) It shouldn't have to calculate angles to find the target position of the steppers. When the object being tracked is inside of these 4 borders (center of the screen) it shouldn't move, then just write a function that should tell it something like
def stepper1 move //this is shoulder left and right
Stepper1.step(1);
if stepcount > 512 //90 deg to the right
try with dc motors // for the wheels to turn
turn right
Although I am struggling to right these functions, It seams so simple yet I just dont quite get it. Maybe you are right and I need all the extras, but I fail to understand why.
I forgot to tell you that you'll need to change your string to a byte value (since you're only reading one byte from Serial2.
Going off what you are saying, can I give that byte any value like 1 or 2 or 5 or does it need to be only 0 or 1 being bianary???
Google "Serial.readBytes()" for a better example of getting a string. Remember that you'll need to provide a buffer for the result and determine a maximum size for the buffer (which will be just large enough for your maximum message length).
Googling................ how to set buffer size 🙂
AccelStepper uses a different strategy to move. You set a target position for the stepper and then call the run() method each time through the loop. You can also tell it to move directly to the target (thereby blocking all other activity until it reaches that target).
Thats awesome @will but I dont think I should need all the Extras. Take in the fact of how it should be working with the object tracking
In the object tracking I want to use Open cv to build borders on the screen and from there when the object being tracked enters these borders it needs to tell the Arduino simple commands (ie. if object enters right border = turn right) It shouldn't have to calculate angles to find the target position of the steppers. When the object being tracked is inside of these 4 borders (center of the screen) it shouldn't move, then just write a function that should tell it something like
def stepper1 move //this is shoulder left and right
Stepper1.step(1);
if stepcount > 512 //90 deg to the right
try with dc motors // for the wheels to turn
turn right
Although I am struggling to right these functions, It seams so simple yet I just dont quite get it. Maybe you are right and I need all the extras, but I fail to understand why.
But you need to KEEP turning right (in your example) until you "escape" the border, so you'll need to calculate a safe turn among or keep calling Stepper1.step() until you're in the safe zone.
Anything seems possible when you don't know what you're talking about.
Google "Serial.readBytes()" for a better example of getting a string. Remember that you'll need to provide a buffer for the result and determine a maximum size for the buffer (which will be just large enough for your maximum message length).
Googling................ how to set buffer size 🙂
Part 1 - decide how long the messages can be (say it's 32 bytes).
#define BUFFERLENGTH 33
char cmdBuffer[BUFFERLENGTH];
.
.
.
int i=0;
while Serial1.available() {
cmdBuffer[i++] = Serial1.read(); Read next byte
}
cmdBuffer[i] = 0; End C string
Anything seems possible when you don't know what you're talking about.
I dont see a problem with keep calling the same functon over and over again, I might tweek it to turn a set amount of steps, I can also call
stepper1.moveDegreesCCW (45);
with this library
if that makes life easier
OK, if you feel it makes your life easier.
I just assumed that you might wish to group steppers for upper arm with lower arm (and maybe even wrist) together for a smooth movement to a new position.
Anything seems possible when you don't know what you're talking about.
Part 1 - decide how long the messages can be (say it's 32 bytes).
#define BUFFERLENGTH 33
char cmdBuffer[BUFFERLENGTH];
.
.
.
int i=0;
while Serial1.available() {
cmdBuffer[i++] = Serial1.read(); Read next byte
}
cmdBuffer[i] = 0; End C string
so if Im picking this up right I want my code to look something like:
#include
#include
const int stepsPerRevolution = 2048; // change this to fit the number of steps per revolution
// for your motor
Stepper myStepper1(stepsPerRevolution, 28, 30, 29, 31);
Stepper myStepper2(stepsPerRevolution, 32, 34, 33, 35);
Stepper myStepper3(stepsPerRevolution, 36, 38, 37, 39);
Stepper myStepper4(stepsPerRevolution, 40, 42, 41, 43);
Stepper myStepper5(stepsPerRevolution, 44, 46, 45, 47);
Stepper myStepper6(stepsPerRevolution, 48, 50, 49, 51);
const unsigned int IN1 = 23;
const unsigned int IN2 = 22;
const unsigned int EN_A = 5;
const unsigned int IN3 = 25;
const unsigned int IN4 = 24;
const unsigned int EN_B = 6;
#define BUFFERLENGTH 33
void setup() {
myStepper1.setSpeed(4);
myStepper2.setSpeed(4);
myStepper3.setSpeed(4);
myStepper4.setSpeed(4);
myStepper5.setSpeed(4);
myStepper6.setSpeed(4);
L298N motor1(EN_A, IN1, IN2);
L298N motor2(EN_B, IN3, IN4);
// initialize the serial port:
Serial.begin(115200);
Serial.println("Initializing Contact...");
Serial2.begin(9600);
while (!Serial2)
{
//do nothing
}
}
void loop() {
int i=0;
while Serial2.available() {
cmdBuffer[i++] = Serial2.read(); Read next byte
int msg = Serial2.read();
L298N motor1(EN_A, IN1, IN2);
L298N motor2(EN_B, IN3, IN4);
if (msg == "stop move");
motor1.stop();
motor2.stop();
cmdBuffer[i] = 0; End C string
Or am I still lost?
It's better, but I think you should move getting the command to a function to keep it out of the main loop.
So make a function like ...
bool getESPCommand()
{
if (!Serial2.available())
return false;
//
int i=0;
while (Serial2.available()) {
cmdBuffer[i++] = Serial2.read(); // Read next byte
}
cmdBuffer[i] = 0; // Terminate C string
//
return true;
}
And in your loop do
loop()
{
... anything that isn't related to ESP commands ...
//
if (getESPCommand()) {
... handle command ...
}
... anything else that ESP commands may require ...
}
Anything seems possible when you don't know what you're talking about.
I just assumed that you might wish to group steppers for upper arm with lower arm (and maybe even wrist) together for a smooth movement to a new position.
I do understand what you are saying, I think anyway.
Im really not trying to be rude in any way, I really do appreciate all the help, Its just in my inexperience I get frustrated sometimes because I don't understand a lot of things, or why I need or want certain things.
In the long run I would like to build a neural network and tell it this function does this, that function does that, this is where you can get all the information from all your sensors. Now try not to bump into things. and see if a neural network can figure out how to move around an environment and manipulate objects on its own.
people are already doing it, I just want to know if I can figure it out. I have a long ways to go apparently.
So I just want basic and simple controls right now and give it a bunch of "if" statements that "if" you reach this point, "try" this.
I understand and I don't think you're being rude. However, I do think you need to consider a few things about the overall function of the robot at this early stage.
For example, who decides what to do next, who's the brain and who's the eyes, ears, hands and legs ? Does the ESP interrogate the state of the sensors from the MEGA, decide what to do and then send the commands back to have the MEGA perform the actions ? Does the MEGA ask the ESP which direction(s) are available and decide how to plan a route to the desired target ?
Or does the (as yet not available) Raspberry PI (or equivalent) decide what the task is and then uses the ESP and MEGA's sensors to determine the most efficient direction to move and tells the MEGA to move that way, how far and how and when to start moving the arm to position the hand ?
If you have an idea of that overall structure, you should find it easier to distribute the work amongst the available micro controllers.
Anything seems possible when you don't know what you're talking about.