Jelajahi Sumber

testing on wheel axis input sensor and stepper on PCF8574T with laser 0x20 and 0x24 i2c

a.binder 5 tahun lalu
induk
melakukan
dd2837262e

+ 88 - 0
SE/stuff/P5_Automation_can-dev-res-working-1930/___SUBMODULES___/PCF8574_test/PCF8574_test.ino

@@ -0,0 +1,88 @@
+
+//
+//    FILE: PCF8574_test.ino
+//  AUTHOR: Rob Tillaart
+//    DATE: 7-febr-2013
+//
+// PUPROSE: test PCF8574 library
+//
+
+//#include "PCF8574.h"
+#include "Wire.h"
+//PCF8574 PCF_01(0x38);
+#include "struct.h"
+
+const int PCF_ADDR = 0x20 ;
+const int PCF_LASER_ADDR = 0x24 ;
+const byte PCF_LASER_ADDR_white_led_pin   = B10000000 ;
+const byte PCF_LASER_ADDR_white_laser_pin = B00000001 ;
+
+
+#include "ctrl.h"
+
+void setup()
+{
+  
+     Wire.begin();
+    Serial.begin(115200);
+     while (!Serial); // Waiting for Serial Monitor
+    Serial.println("\nstep test");
+    Serial.println(step_currX_A); // 1000000
+      
+    #include "scan.h"
+ 
+      Wire.beginTransmission(PCF_LASER_ADDR);
+      Wire.write(0x00);
+      Wire.endTransmission();
+  step_seq_init(step_seqX_A, step_seqY_A);
+
+  
+}
+/*
+ *byte x = 0b10000000;  // the 0b prefix indicates a binary constant
+  Serial.println(x, BIN); // 10000000
+  bitWrite(x, 0, 1);  // write 1 to the least significant bit of x
+  Serial.println(x, BIN); // 1000000
+ */
+
+void loop()
+{
+   // step_byte_T step_byte_A ; 
+    
+   
+    delay(500);
+      Wire.beginTransmission(PCF_LASER_ADDR);
+      Wire.write(PCF_LASER_ADDR_white_led_pin);
+      Wire.endTransmission();
+    delay(500);
+ 
+
+    step_ctrl(  step_seqX_A, step_seqY_A,  step_currX_A, step_currY_A, 500, 0 ) ;
+   delay(500);
+  Wire.beginTransmission(PCF_LASER_ADDR);
+      Wire.write(0x00);
+      Wire.endTransmission();
+    step_ctrl(  step_seqX_A, step_seqY_A,  step_currX_A, step_currY_A, 0, 250) ;
+
+    step_ctrl(  step_seqX_A, step_seqY_A,  step_currX_A, step_currY_A, -250, 250) ;
+
+
+    step_ctrl(  step_seqX_A, step_seqY_A,  step_currX_A, step_currY_A, -125, -125) ;
+
+    step_ctrl(  step_seqX_A, step_seqY_A,  step_currX_A, step_currY_A, -100, -125) ;
+
+    step_ctrl(  step_seqX_A, step_seqY_A,  step_currX_A, step_currY_A, -25, -125) ;
+
+
+      Wire.beginTransmission(PCF_LASER_ADDR);
+      Wire.write(PCF_LASER_ADDR_white_laser_pin);
+      Wire.endTransmission();
+
+
+      
+
+   delay(500);
+
+   //PCF_01.write(0xFF, LOW);
+   delay(2000);
+}

+ 68 - 0
SE/stuff/P5_Automation_can-dev-res-working-1930/___SUBMODULES___/PCF8574_test/ctrl.h

@@ -0,0 +1,68 @@
+
+
+void step_ctrl_send(step_seq_T* step_seqX_A, step_seq_T* step_seqY_A, step_curr_T &step_currX_A,  step_curr_T &step_currY_A, int stepsX, int stepsY ) {
+
+    if(stepsX > 0) {
+           step_currX_A ++; 
+    } else if(stepsX < 0) {
+           step_currX_A --; 
+    } else {
+        //   step_currX_A --;
+    }
+
+    if(stepsY > 0) {
+           step_currY_A ++; 
+    } else if(stepsY < 0) {
+           step_currY_A --; 
+    } else {
+        //   step_currY_A --;
+    }
+
+    
+    if(step_currX_A >= 9) step_currX_A = 1;
+    if(step_currX_A <= 0) step_currX_A = 8;
+
+    if(step_currY_A >= 9) step_currY_A = 1;
+    if(step_currY_A <= 0) step_currY_A = 8;
+   
+    
+    Serial.println( );
+    Serial.print(" SEQ ");     Serial.print(stepsX); Serial.print("]["); Serial.println(step_currX_A);
+    //Serial.print(String(step_seq_A[step_curr_A].step_byte_A , BIN));
+   delay(10);
+     Wire.beginTransmission(PCF_ADDR);
+      Wire.write(step_seqX_A[step_currX_A].step_byte_A | step_seqY_A[step_currY_A].step_byte_A );
+      Wire.endTransmission();
+   // return step_seq_A[step_curr_A].step_byte_A ;
+}
+
+void step_ctrl(step_seq_T* step_seqX_A, step_seq_T* step_seqY_A, step_curr_T &step_currX_A, step_curr_T &step_currY_A, int stepsX, int stepsY) {
+  int ratio ;
+  int mod_ratio ;
+  for(int i = 1 ; i<=abs(stepsX) or i<= abs(stepsY); i++) {
+
+         if(abs(stepsX) == abs(stepsY) or stepsX == 0 or stepsY == 0) {
+            step_ctrl_send(step_seqX_A, step_seqY_A, step_currX_A, step_currY_A,stepsX, stepsY );
+         } else if(abs(stepsX) > abs(stepsY)) {
+            ratio = abs(stepsX) /  abs(stepsY) ;
+            mod_ratio = i %  ratio ;
+                if(mod_ratio < 1) {
+                  step_ctrl_send(step_seqX_A, step_seqY_A, step_currX_A, step_currY_A,stepsX, stepsY );
+                   Serial.print(" [Y+] ");
+                } else {
+                  step_ctrl_send(step_seqX_A, step_seqY_A, step_currX_A, step_currY_A,stepsX, 0 );
+                  Serial.print(" [Y-] ");
+                }
+         }  else if(abs(stepsX) < abs(stepsY)) {
+            ratio = abs(stepsY) /  abs(stepsX) ;
+            mod_ratio = i %  ratio ;
+                if(mod_ratio < 1) {
+                  step_ctrl_send(step_seqX_A, step_seqY_A, step_currX_A, step_currY_A,stepsX, stepsY );
+                    Serial.print(" [X+] ");
+                } else {
+                  step_ctrl_send(step_seqX_A, step_seqY_A, step_currX_A, step_currY_A,0, stepsY );
+                  Serial.print(" [X-] %[");  Serial.print(mod_ratio);  Serial.print("] r[");     Serial.print(ratio); 
+                }
+         }
+  }
+}

+ 41 - 0
SE/stuff/P5_Automation_can-dev-res-working-1930/___SUBMODULES___/PCF8574_test/scan.h

@@ -0,0 +1,41 @@
+
+{
+  byte error, address; //variable for error and I2C address
+  int nDevices;
+
+  Serial.println("Scanning.1..");
+
+  nDevices = 0;
+  for (address = 1; address < 127; address++ )
+  {
+    // The i2c_scanner uses the return value of
+    // the Write.endTransmisstion to see if
+    // a device did acknowledge to the address.
+    Wire.beginTransmission(address);
+    error = Wire.endTransmission();
+  //Serial.print(address);
+
+    if (error == 0)
+    {
+      Serial.print("I2C device found at address 0x");
+      if (address < 16)
+        Serial.print("0");
+      Serial.print(address, HEX);
+      Serial.println("  !");
+      nDevices++;
+    }
+    else if (error == 4)
+    {
+      Serial.print("Unknown error at address 0x");
+      if (address < 16)
+        Serial.print("0");
+      Serial.println(address, HEX);
+    }
+  }
+  if (nDevices == 0)
+    Serial.println("No I2C devices found\n");
+  else
+    Serial.println("done\n");
+
+ // delay(5000); // wait 5 seconds for the next I2C scan
+}

+ 39 - 0
SE/stuff/P5_Automation_can-dev-res-working-1930/___SUBMODULES___/PCF8574_test/struct.h

@@ -0,0 +1,39 @@
+
+typedef   byte step_byte_T ;
+typedef   byte step_curr_T ;
+
+ struct step_seq_S {
+  step_byte_T step_byte_A ;
+};
+
+  typedef struct step_seq_S step_seq_T ;
+
+   step_curr_T step_currX_A = 1;
+   step_curr_T step_currY_A = 1;
+   step_seq_T step_seqX_A[8]  ;
+   step_seq_T step_seqY_A[8]  ;
+//  step_seq_A[0] = { 1 };
+
+  step_seq_T step_seq_init(step_seq_T* step_seqX_A, step_seq_T* step_seqY_A) {
+         step_seqX_A[1].step_byte_A = B00000001  ;
+         step_seqX_A[2].step_byte_A = B00000011 ;
+         step_seqX_A[3].step_byte_A = B00000010 ;
+         step_seqX_A[4].step_byte_A = B00000110 ;
+         step_seqX_A[5].step_byte_A = B00000100 ;
+         step_seqX_A[6].step_byte_A = B00001100 ;
+         step_seqX_A[7].step_byte_A = B00001000 ;
+         step_seqX_A[8].step_byte_A = B00001001 ;
+
+         step_seqY_A[1].step_byte_A = B00010000 ;
+         step_seqY_A[2].step_byte_A = B00110000 ;
+         step_seqY_A[3].step_byte_A = B00100000 ;
+         step_seqY_A[4].step_byte_A = B01100000 ;
+         step_seqY_A[5].step_byte_A = B01000000 ;
+         step_seqY_A[6].step_byte_A = B11000000 ;
+         step_seqY_A[7].step_byte_A = B10000000 ;
+         step_seqY_A[8].step_byte_A = B10010000 ;
+         
+  }
+
+
+  

+ 49 - 0
SE/stuff/P5_Automation_can-dev-res-working-1930/___SUBMODULES___/wheelDriveModTest/ctrl.h

@@ -0,0 +1,49 @@
+
+
+void pwm_swing(pin_T pin_A, int delay_A, bool dir) {
+
+    if(dir == true) {
+      for(int i = 0; i <= 255; i++) {
+        analogWrite(pin_A, i);
+        delay(delay_A);
+      }
+    } else {
+      for(int i = 255; i >=0 ; i--) {
+        analogWrite(pin_A, i);
+        delay(delay_A);
+      }
+    }
+}
+
+
+void pwm_swing_freq(pin_T pin_A,  int delay_A, bool dir) {
+    if(dir == true) {
+      for(int i = 0; i <= 255; i++) {
+          for(int freq = 1 ; freq < delay_A; freq++ ) {
+            digitalWrite(pin_A, LOW);
+            delay(delay_A);
+            digitalWrite(pin_A, HIGH);
+            delay(freq);
+          }
+            digitalWrite(pin_A, LOW);
+      }
+    } else {
+      for(int i = 255; i >=0 ; i--) {
+        for(int freq = 1 ; freq < delay_A; freq++ ) {
+            digitalWrite(pin_A, LOW);
+            delay(delay_A);
+            digitalWrite(pin_A, HIGH);
+            delay(freq);
+          }
+            digitalWrite(pin_A, LOW);
+      }
+    }
+}
+
+
+
+void analog_read(pin_T pin_A) {
+  int analog;
+  analog = analogRead(pin_A);
+  Serial.print(" @["); Serial.print(analog); Serial.println("] ");
+}

+ 9 - 0
SE/stuff/P5_Automation_can-dev-res-working-1930/___SUBMODULES___/wheelDriveModTest/init.h

@@ -0,0 +1,9 @@
+
+
+
+void init_ports(pin_T pin_A) {
+  pinMode(pin_A, OUTPUT);
+  digitalWrite(pin_A, HIGH);
+  delay(500);
+  digitalWrite(pin_A, LOW);
+}

+ 11 - 0
SE/stuff/P5_Automation_can-dev-res-working-1930/___SUBMODULES___/wheelDriveModTest/struct.h

@@ -0,0 +1,11 @@
+
+
+typedef int pin_T ;
+
+struct pins_S {
+  pin_T pin_A ;
+} ;
+
+typedef pins_S pins_T ;
+
+pins_T pins_A[10] = {} ;

+ 39 - 0
SE/stuff/P5_Automation_can-dev-res-working-1930/___SUBMODULES___/wheelDriveModTest/wheelDriveModTest.ino

@@ -0,0 +1,39 @@
+
+
+const int TestPinOutPwm = 9 ;
+const int TestPinOutDir = 6 ;
+const int TestPinInAnalogWheel = A0 ;
+
+#include "struct.h"
+#include "ctrl.h"
+
+#include "init.h"
+
+//TCCR1B = ( TCCR1B & B11111000 ) | B00000101;
+
+void setup() {
+  Serial.begin(9600);
+  Serial.print("Test WheelDriveMod pwm["); Serial.print(TestPinOutPwm); Serial.print("] Dir[");
+       Serial.print(TestPinOutDir); Serial.print("] DistIn[");
+        Serial.print(TestPinInAnalogWheel, HEX); Serial.println("] ");
+      analog_read(TestPinInAnalogWheel);
+
+    init_ports(TestPinOutPwm);
+    init_ports(TestPinOutDir);
+
+      analog_read(TestPinInAnalogWheel);
+    pwm_swing(TestPinOutPwm,50, true);
+    init_ports(TestPinOutPwm);
+      analog_read(TestPinInAnalogWheel);
+
+    digitalWrite(TestPinOutDir, HIGH);
+    pwm_swing(TestPinOutPwm,50, false);
+
+}
+
+void loop() {
+  // put your main code here, to run repeatedly:
+        analog_read(TestPinInAnalogWheel);
+        delay(250);
+
+}