diff --git a/Mapping/Arduino/Motorz.h b/Mapping/Arduino/Motorz.h new file mode 100644 index 0000000..085c7de --- /dev/null +++ b/Mapping/Arduino/Motorz.h @@ -0,0 +1,91 @@ +#ifndef _MOTORZ_H // not #ifnotdef +#define _MOTORZ_H + +class Motorz +{ + private: + int motor_speed=0; + int enable,in1,in2,dir; + public: + + Motorz(int Enb,int ina,int inb){ + enable=Enb; + in1=ina; + in2=inb; + pinMode(enable,OUTPUT); + pinMode(in1,OUTPUT); + pinMode(in2,OUTPUT); + } + Motorz(int Enb,int ina,int inb,int d){ + enable=Enb; + in1=ina; + in2=inb; + dir=d; + pinMode(enable,OUTPUT); + pinMode(in1,OUTPUT); + pinMode(in2,OUTPUT); + Direction(); + } + void SetSpeed(int Mspeed){ + motor_speed=Mspeed; + } + int GetSpeed(){ + return motor_speed; + } + void SetEnable(int pin){ + enable=pin; + } + int GetEnable(){ + return enable; + } + void SetInput1(int pin){ + in1=pin; + } + int GetInput1(){ + return in1; + } + void SetInput2(int pin){ + in2=pin; + } + int GetInput2(){ + return in2; + } + void SetDirection(int a){ + dir=a; + } + int GetDirection(){ + return dir; + } + void ChangeDir(){ + if(dir==0){ + dir=1; + } + else{ + dir=0; + } + } + void Start(){ + this->Direction(); + analogWrite(enable,motor_speed); + } + void Stop(){ + this->Halt(); + } + private: + void Direction(){ + if(dir==0){ + digitalWrite(in1,HIGH); + digitalWrite(in2,LOW); + } + else{ + digitalWrite(in1,LOW); + digitalWrite(in2,HIGH); + } + } + void Halt(){ + digitalWrite(in1,LOW); + digitalWrite(in2,LOW); + } + +}; +#endif _MOTOR_H diff --git a/Mapping/Arduino/motor_arduino_garbage b/Mapping/Arduino/motor_arduino_garbage new file mode 100644 index 0000000..24a5d08 --- /dev/null +++ b/Mapping/Arduino/motor_arduino_garbage @@ -0,0 +1,218 @@ + +//very crude libraries with objects for motor and tandrive system (2 motors no steering) +//tandrive is an object that you attach the motor objects you make bellow made some functions +//in hope it will be more easier to read etc. + +#include "Motorz.h"; +#include "tankdrive.h"; +#include +#include + +#define SONAR_PIN 10 +#define SERVO_PIN 9 +#define MAX_DISTANCE 200 //max sonar dinstance according to the type of sensor i use Hc-SR04 + +NewPing sonar(SONAR_PIN,SONAR_PIN, MAX_DISTANCE); +Servo myservo; + + + +int GetDistances(); + +int i; +int enr=5; // enable pin for motor r (must be pwm) +int in1=4; // pin for one of the two connections of the mottor +int in2=3;// other connection (these two are digital pins) +// depends on how you end up wiring and placing the motors it will spin a certain way +// and another way when you reverse the voltage , direct holds (0,1) depending on how you +// wired the motor to go "forwards" what ever that means for your project +int directr=0; +int enl=6; +int in3=7; +int in4=8; +int directl=0; + +char buffer1[(sizeof(char)+sizeof(unsigned long))*sizeof(char)+1]; + + char a; + unsigned long runforsmth; +Motorz motorR(enr,in1,in2,directr); +Motorz motorL(enl,in3,in4,directl); + +TankDrive mytank(&motorR,directr,&motorL,directl); + +void setup(){ + Serial.begin(9600); + motorR.SetSpeed(80); //im using diffrent motors (trying to simulate wheels so i figure things out ) + motorL.SetSpeed(80); // so that explains the diffrent speed in each motor + pinMode(LED_BUILTIN, OUTPUT); +} + + +void loop(){ + + digitalWrite(LED_BUILTIN,LOW); + + + + + + /* + * + * + * + * if(a=='1'){ + digitalWrite(LED_BUILTIN, HIGH); + // turn the LED on (HIGH is the voltage level) + } + else if(a=='0') + digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW + + * + * + * + * + * + * + * + * + * + mytank.TurnRight(); + delay(10000); + mytank.TurnLeft(); + delay(10000); + mytank.MoveForward(); + delay(10000); + + + + +motorR.Start(); +Serial.println(motorR.GetDirection()); +delay(1000); +motorR.ChangeDir(); +Serial.print("Changing Dir: "); +Serial.println(motorR.GetDirection()); + +Serial.print("F:"); +Serial.print(mytank.getFDirR()); +Serial.print(" B:"); +Serial.print(mytank.getBDirR()); +Serial.print(" "); +Serial.print("F:"); +Serial.print(mytank.getFDirL()); +Serial.print(" B:"); +Serial.println(mytank.getBDirL()); +mytank.MoveForward(); +delay(5000); +mytank.MoveBack(); +delay(5000); + + * delay(1000); +mytank.TurnLeft(); +delay(5000); +mytank.TurnRight(); +delay(5000); +mytank.MoveForward(); +delay(5000); + + +Serial.println("Getting distances..."); +i=GetDistances(); +Serial.println("Done"); + if(i==-1) + { + Serial.println("Turning Left..."); + run_for_millis(&mytank,'L',200); + } + else if(i==0) + { + Serial.println("Going Forward..."); + run_for_millis(&mytank,'M',200); + } + else{ + Serial.println("Turning Right..."); + run_for_millis(&mytank,'R',200); + } */ +} +void serialEvent() { + while (Serial.available()) { + Serial.readBytes(buffer1,sizeof(buffer1)); + sscanf( buffer1, "%c%lu",&a, &runforsmth); + run_for_millis(&mytank,a,runforsmth); + /*if(a=='L'){ + digitalWrite(LED_BUILTIN, HIGH); + delay(runforsmth); + }*/ + } +} +int GetDistances(){ + int pos; + int maxdist[2]; + int temp; + myservo.attach(SERVO_PIN); + maxdist[0]=-1; + + for (pos = 20; pos <= 160; pos += 1) { // goes from 0 degrees to 180 degrees + // in steps of 1 degree + myservo.write(pos); + temp=sonar.ping_cm(); + Serial.print("Distance:"); + Serial.print(temp); + Serial.print(" Angle:"); + Serial.println(pos); + + if(temp>maxdist[0]){ + maxdist[0]=temp; + maxdist[1]=pos; + } + delay(55); + } + for (pos = 160; pos >=20; pos -= 1) { // goes from 0 degrees to 180 degrees + myservo.write(pos); + delay(5); + } + myservo.detach(); + + if(maxdist[1]<70){ + return -1; + } + + else if(maxdist[1]>110){ + return 1; + } + + else return 0; + +} +void run_for_millis(TankDrive *tank,char ch,unsigned long timetorun){ + + unsigned long count; + switch(ch){ + case 'L': + count=millis(); + do{ + tank->TurnLeft();} + while(millis()-count<=timetorun); + tank->Stop(); + break; + + case 'F': + count=millis(); + do{ + tank->MoveForward();} + while(millis()-count<=timetorun); + tank->Stop(); + break; + case 'R': + count=millis(); + do{ + tank->TurnRight();} + while(millis()-count<=timetorun); + tank->Stop(); + break; + } + + + +} diff --git a/Mapping/Arduino/motortest_first.ino b/Mapping/Arduino/motortest_first.ino new file mode 100644 index 0000000..075cb02 --- /dev/null +++ b/Mapping/Arduino/motortest_first.ino @@ -0,0 +1,136 @@ + +//very crude libraries with objects for motor and tandrive system (2 motors no steering) +//tandrive is an object that you attach the motor objects you make bellow made some functions +//in hope it will be more easier to read etc. + +#include "Motorz.h"; +#include "tankdrive.h"; +#include +#include + +#define SONAR_PIN 10 +#define SERVO_PIN 9 +#define MAX_DISTANCE 200 //max sonar dinstance according to the type of sensor i use Hc-SR04 + +NewPing sonar(SONAR_PIN,SONAR_PIN, MAX_DISTANCE); +Servo myservo; + + + +int GetDistances(); + +int i; +int enr=5; // enable pin for motor r (must be pwm) +int in1=4; // pin for one of the two connections of the mottor +int in2=3;// other connection (these two are digital pins) +// depends on how you end up wiring and placing the motors it will spin a certain way +// and another way when you reverse the voltage , direct holds (0,1) depending on how you +// wired the motor to go "forwards" what ever that means for your project +int directr=0; +int enl=6; +int in3=7; +int in4=8; +int directl=0; + +char buffer1[(sizeof(char)+sizeof(unsigned long))*sizeof(char)+1]; + + char a; + unsigned long runforsmth; +Motorz motorR(enr,in1,in2,directr); +Motorz motorL(enl,in3,in4,directl); + +TankDrive mytank(&motorR,directr,&motorL,directl); + +void setup(){ + Serial.begin(9600); + motorR.SetSpeed(80); //im using diffrent motors (trying to simulate wheels so i figure things out ) + motorL.SetSpeed(80); // so that explains the diffrent speed in each motor + pinMode(LED_BUILTIN, OUTPUT); +} + + +void loop(){ + + digitalWrite(LED_BUILTIN,LOW); +} +void serialEvent() { + while (Serial.available()) { + Serial.readBytes(buffer1,sizeof(buffer1)); + sscanf( buffer1, "%c%lu",&a, &runforsmth); + run_for_millis(&mytank,a,runforsmth); + /*if(a=='L'){ + digitalWrite(LED_BUILTIN, HIGH); + delay(runforsmth); + }*/ + } +} +int GetDistances(){ + int pos; + int maxdist[2]; + int temp; + myservo.attach(SERVO_PIN); + maxdist[0]=-1; + + for (pos = 20; pos <= 160; pos += 1) { // goes from 0 degrees to 180 degrees + // in steps of 1 degree + myservo.write(pos); + temp=sonar.ping_cm(); + Serial.print("Distance:"); + Serial.print(temp); + Serial.print(" Angle:"); + Serial.println(pos); + + if(temp>maxdist[0]){ + maxdist[0]=temp; + maxdist[1]=pos; + } + delay(55); + } + for (pos = 160; pos >=20; pos -= 1) { // goes from 0 degrees to 180 degrees + myservo.write(pos); + delay(5); + } + myservo.detach(); + + if(maxdist[1]<70){ + return -1; + } + + else if(maxdist[1]>110){ + return 1; + } + + else return 0; + +} +void run_for_millis(TankDrive *tank,char ch,unsigned long timetorun){ + + unsigned long count; + switch(ch){ + case 'L': + count=millis(); + do{ + tank->TurnLeft();} + while(millis()-count<=timetorun); + tank->Stop(); + break; + + case 'F': + count=millis(); + do{ + tank->MoveForward();} + while(millis()-count<=timetorun); + tank->Stop(); + break; + case 'R': + count=millis(); + do{ + tank->TurnRight();} + while(millis()-count<=timetorun); + tank->Stop(); + break; + } + + + +} diff --git a/Mapping/Arduino/tankdrive.h b/Mapping/Arduino/tankdrive.h new file mode 100644 index 0000000..45485b3 --- /dev/null +++ b/Mapping/Arduino/tankdrive.h @@ -0,0 +1,66 @@ +#ifndef _TANKDRIVE_H // not #ifnotdef +#define _TANKDRIVE_H + +#include "Motorz.h" + +class TankDrive +{ + Motorz *motorL; + Motorz *motorR; + int forwardR,forwardL,backR,backL; + public: + TankDrive(Motorz *MR,int fr,Motorz *ML,int fl){ + motorL=ML; + motorR=MR; + forwardR=fr; + forwardL=fl; + if(fr==1)backR=0; + else backR=1; + if(fl==1)backL=0; + else backL=1; + } + void MoveForward(){ + motorR->SetDirection(forwardR); + motorL->SetDirection(forwardL); + motorR->Start(); + motorL->Start(); + } + void MoveBack(){ + motorR->SetDirection(backR); + motorL->SetDirection(backL); + motorR->Start(); + motorL->Start(); + } + void Stop(){ + + motorR->Stop(); + motorL->Stop(); + } + void TurnRight(){ + motorR->SetDirection(backR); + motorL->SetDirection(forwardL); + motorR->Start(); + motorL->Start(); + } + void TurnLeft(){ + motorR->SetDirection(forwardR); + motorL->SetDirection(backL); + motorL->Start(); + motorR->Start(); + } + int getFDirR(){ + return forwardR; + } + int getFDirL(){ + return forwardL; + } + int getBDirR(){ + return backR; + } + int getBDirL(){ + return backL; + } +}; + + +#endif _TANKDRIVE_H