diff options
Diffstat (limited to 'debug/motordriver/nomultiplexer/nomultiplexer.ino')
| -rw-r--r-- | debug/motordriver/nomultiplexer/nomultiplexer.ino | 294 |
1 files changed, 179 insertions, 115 deletions
diff --git a/debug/motordriver/nomultiplexer/nomultiplexer.ino b/debug/motordriver/nomultiplexer/nomultiplexer.ino index d2800a8..4ca1ca8 100644 --- a/debug/motordriver/nomultiplexer/nomultiplexer.ino +++ b/debug/motordriver/nomultiplexer/nomultiplexer.ino @@ -2,25 +2,29 @@ //https://www.pololu.com/product/2997 //https://www.pololu.com/product/4861 // -// motor one -int m1penA = 1; +// motor one/A front +int m1enA = 1; int m1enB = 2; int m1pwm1 = 3; int m1pwm2 = 4; int m1encA = 13; int m1encB = 14; -// motor two +// motor two/B left int m2enA = 5; int m2enB = 6; int m2pwm1 = 7; int m2pwm2 = 8; -// motor three +int m2encA = 15; +int m2encB = 16; +// motor three/C right int m3enA = 9; int m3enB = 10; int m3pwm1 = 11; int m3pwm2 = 12; +int m3encA = 17; +int m3encB = 18; -int sp = 160; +double sp = 1.000; //speed in 0-1 double dirA = 0.00; double dirB = 0.00; @@ -29,6 +33,23 @@ double spdA = 0.00; double spdB = 0.00; double spdC = 0.00; double dirrad = 0.00; +double pi = 3.1415926535897; + +double Arpm = 0.00; +volatile long Aencount = 0; +unsigned long ALtime = 0; +unsigned long ACtime = 0; +unsigned long APtime = 0; +double Brpm = 0.00; +volatile long Bencount = 0; +unsigned long BLtime = 0; +unsigned long BCtime = 0; +unsigned long BPtime = 0; +double Crpm = 0.00; +volatile long Cencount = 0; +unsigned long CLtime = 0; +unsigned long CCtime = 0; +unsigned long CPtime = 0; void setup() { @@ -47,146 +68,189 @@ void setup() pinMode(m3pwm1, OUTPUT); pinMode(m3pwm2, OUTPUT); + //set encoder pins to input pinMode(m1encA, INPUT); pinMode(m1encB, INPUT); + pinMode(m2encA, INPUT); + pinMode(m2encB, INPUT); + pinMode(m3encA, INPUT); + pinMode(m3encB, INPUT); + attachInterrupt(digitalPinToInterrupt(m1encA), AencoderISR, CHANGE); + attachInterrupt(digitalPinToInterrupt(m1encB), AencoderISR, CHANGE); + attachInterrupt(digitalPinToInterrupt(m2encA), BencoderISR, CHANGE); + attachInterrupt(digitalPinToInterrupt(m2encB), BencoderISR, CHANGE); + attachInterrupt(digitalPinToInterrupt(m3encA), CencoderISR, CHANGE); + attachInterrupt(digitalPinToInterrupt(m3encB), CencoderISR, CHANGE); +} + +void AencoderISR() { + Aencount++; +} +void BencoderISR() { + Bencount++; +} +void CencoderISR() { + Cencount++; +} +void Areadenc() { + ACtime = micros(); + APtime = ACtime - ALtime; + ALtime = ACtime; + Arpm = (Aencount / 211.2) * (60000000.0 / APtime); + Serial.print("Motor A RPM: "); + Serial.print(Arpm); + Serial.print(" Sample period:"); + Serial.print(APtime); + Serial.print("ms, encoder count:"); + Serial.println(Aencount); + Aencount = 0; +} +void Breadenc() { + BCtime = micros(); + BPtime = BCtime - BLtime; + BLtime = BCtime; + Brpm = (Bencount / 211.2) * (60000000.0 / BPtime); + Serial.print("Motor B RPM: "); + Serial.print(Brpm); + Serial.print(" Sample period: "); + Serial.print(BPtime); + Serial.print(" ms, encoder count: "); + Serial.println(Bencount); + Bencount = 0; +} +void Creadenc() { + CCtime = micros(); + CPtime = CCtime - CLtime; + CLtime = CCtime; + Crpm = (Cencount / 211.2) * (60000000.0 / CPtime); + Serial.print("Motor C RPM: "); + Serial.print(Crpm); + Serial.print(" Sample period: "); + Serial.print(CPtime); + Serial.print(" ms, encoder count: "); + Serial.println(Cencount); + Cencount = 0; } -void Amove(int sped){ +void Amove(double sped) { + spdA = abs(sped)*255; digitalWrite(m1enA, HIGH); - digitalWrite(m1enA, LOW); - sp = abs(sped); + digitalWrite(m1enB, LOW); if (sped < 0) { - Serial.print("back"); - analogWrite(m1pwm1, sp); - digitalWrite(m1pwm2, LOW); + Serial.print("Motor A back "); + analogWrite(m1pwm1, spdA); + analogWrite(m1pwm2, 0); } else { - Serial.print("forward"); - digitalWrite(m1pwm1, LOW); - analogWrite(m1pwm2, sp); + Serial.print("Motor A forward "); + analogWrite(m1pwm1, 0); + analogWrite(m1pwm2, spdA); } - Serial.println(sp); + Serial.println(spdA); } -void Bmove(int sped) { - Serial.print("moving B "); +void Bmove(double sped) { + spdB = abs(sped)*255; + digitalWrite(m2enA, HIGH); + digitalWrite(m2enB, LOW); if (sped < 0) { - digitalWrite(in3, LOW); - digitalWrite(in4, HIGH); - Serial.print("back"); + Serial.print("Motor B back "); + analogWrite(m2pwm1, spdB); + analogWrite(m2pwm2, 0); } else { - digitalWrite(in3, HIGH); - digitalWrite(in4, LOW); - Serial.print("forward"); + Serial.print("Motor B forward "); + analogWrite(m2pwm1, 0); + analogWrite(m2pwm2, spdB); } - Serial.print("with speed: "); - int sp = abs(sped); - analogWrite(enB, sp); - Serial.println(sp); + Serial.println(spdB); } -void Cmove(int sped) { - Serial.print("moving C "); +void Cmove(double sped) { + spdC = abs(sped) * 255; + digitalWrite(m3enA, HIGH); + digitalWrite(m3enB, LOW); if (sped < 0) { - digitalWrite(in5, LOW); - digitalWrite(in6, HIGH); - Serial.print("back"); + Serial.print("Motor C back "); + analogWrite(m3pwm1, spdC); + analogWrite(m3pwm2, 0); } else { - digitalWrite(in5, HIGH); - digitalWrite(in6, LOW); - Serial.print("forward"); + Serial.print("Motor C forward "); + analogWrite(m3pwm1, 0); + analogWrite(m3pwm2, spdC); } - Serial.print("with speed: "); - int sp = abs(sped); - analogWrite(enC, sp); - Serial.println(sp); -} - -void turnoff(){ - Serial.println("stop"); - analogWrite(enA, 0); - analogWrite(enB, 0); - analogWrite(enC, 0); - digitalWrite(in1, LOW); - digitalWrite(in2, LOW); - digitalWrite(in3, LOW); - digitalWrite(in4, LOW); - digitalWrite(in5, LOW); - digitalWrite(in6, LOW); -} -void forward(int speed){ - Serial.println("forward"); - analogWrite(enA, speed); - analogWrite(enB, speed); - analogWrite(enC, speed); - digitalWrite(in1, LOW); - digitalWrite(in2, LOW); - digitalWrite(in3, HIGH); - digitalWrite(in4, LOW); - digitalWrite(in5, HIGH); - digitalWrite(in6, LOW); -} -void turn(int speed){ - Serial.println("turn"); - analogWrite(enA, speed); - analogWrite(enB, speed); - analogWrite(enC, speed); - digitalWrite(in1, HIGH); - digitalWrite(in2, LOW); - digitalWrite(in3, HIGH); - digitalWrite(in4, LOW); - digitalWrite(in5, HIGH); - digitalWrite(in6, LOW); + Serial.println(spdC); } void moveohmi(int speed, int direction){ - turnoff(); - double dirrad = (direction*71) / 4068; - dirA = 1.047198-dirrad; - dirB = 3.141593-dirrad; - dirC = 5.235988-dirrad; - spdA = speed*sin(dirA); - spdB = speed*sin(dirB); - spdC = speed*sin(dirC); - Amove(spdA); - Bmove(spdB); - Cmove(spdC); -} -void beddermoveohmi(int speed, int direction){ - turnoff(); + Amove(0); + Bmove(0); + Cmove(0); - double dirrad = (direction*71) / 4068; + double dirrad = (direction*71.0000) / 4068.0000; Serial.println(dirrad); //vector ontb in x en y - double x = sin(dirrad)*speed; - double y = cos(dirrad)*speed; + double y = sin(dirrad); + double x = cos(dirrad); Serial.println((String)"x:"+x+" y:"+y); - spdA = -0.5*x - sqrt(3)/2*y; - spdB = x; - spdC = -0.5*x + sqrt(3)/2*y; - Amove(spdA); - Bmove(spdB); - Cmove(spdC); - - Serial.println(spdA); - Serial.println(spdB); - Serial.println(spdC); + double spdeA = y*speed; + double spdeB = (-0.500*x - sqrt(3.000)/2.000*y)*speed; + double spdeC = (-0.500*x + sqrt(3.000)/2.000*y)*speed; + Serial.println(spdeA); + Amove(spdeA); + Bmove(spdeB); + Cmove(spdeC); Serial.println(); Serial.println(); } -void loop() -{ +void rotate(int rodeg, int speed){ +//use compas heading +} +void demo(){ + + Amove(sp); + Bmove(sp); + Cmove(sp); + Serial.println("seperate A B C rotation"); delay(1000); - turnoff(); - beddermoveohmi(255, 0); + + Amove(0); + Bmove(0); + Cmove(0); + Serial.println("seperate A B C stop"); delay(1000); - turnoff(); - Amove(255); + + Amove(-sp); + Bmove(-sp); + Cmove(-sp); + Serial.println("seperate A B C -rotation"); delay(1000); - turnoff(); - Bmove(255); + + moveohmi(0, 0); + Serial.println("ohmi stop"); delay(1000); - turnoff(); - Cmove(255); + + rotate(90, sp); + Serial.println("rotation function 90deg"); + rotate(90, sp); + Serial.println("rotation function -90deg"); + delay(1000); + + moveohmi(sp, 35); + Serial.println("ohmi move 35deg"); delay(1000); - turnoff(); - turn(254); + + moveohmi(sp, 215); + Serial.println("ohmi move 215deg"); delay(1000); + + for(int i = 0; i<360; i++){ + moveohmi(sp, i); + Serial.println(i); + delay(50); + } + Serial.println("ohmi move circle"); + delay(1000); + +} +void loop(){ + demo(); + delay(100); + } |
