From c0b5d40653c2c72130dd41e187068efaae1857fe Mon Sep 17 00:00:00 2001 From: KenwoodFox Date: Tue, 29 Jun 2021 22:46:25 -0400 Subject: [PATCH 1/7] Cleanup slave software --- .../arduino/crawler_slave/crawler_slave.ino | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/crawler_software/arduino/crawler_slave/crawler_slave.ino b/crawler_software/arduino/crawler_slave/crawler_slave.ino index b38d788..2388bec 100644 --- a/crawler_software/arduino/crawler_slave/crawler_slave.ino +++ b/crawler_software/arduino/crawler_slave/crawler_slave.ino @@ -7,6 +7,7 @@ http://www.arduino.cc/en/Tutorial/Sweep */ +#include #include Servo myservo; // create servo object to control a servo @@ -17,17 +18,20 @@ int pos = 0; // variable to store the servo position void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object - for (pos = 70; pos <= 130; pos += 1) { // goes from 0 degrees to 180 degrees - // in steps of 1 degree - myservo.write(pos); // tell servo to go to position in variable 'pos' - delay(15); // waits 15ms for the servo to reach the position - } - for (pos = 130; pos >= 70; pos -= 1) { // goes from 180 degrees to 0 degrees - myservo.write(pos); // tell servo to go to position in variable 'pos' - delay(15); // waits 15ms for the servo to reach the position - } + Wire.begin(0x8); + + // Call receiveEvent when data received + Wire.onReceive(receiveEvent); + } void loop() { - delay(1000); + delay(10); +} + +void receiveEvent(int howMany) { + while (Wire.available()) { // loop through all but the last + int pos = Wire.read(); // receive byte as a int + myservo.write(pos); + } } From 67daffd667dfb297d79434096296add1eb3e23d7 Mon Sep 17 00:00:00 2001 From: KenwoodFox Date: Tue, 29 Jun 2021 22:57:51 -0400 Subject: [PATCH 2/7] Cleanup slave software again. --- .../arduino/crawler_slave/crawler_slave.ino | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/crawler_software/arduino/crawler_slave/crawler_slave.ino b/crawler_software/arduino/crawler_slave/crawler_slave.ino index 2388bec..447eef9 100644 --- a/crawler_software/arduino/crawler_slave/crawler_slave.ino +++ b/crawler_software/arduino/crawler_slave/crawler_slave.ino @@ -1,23 +1,20 @@ -/* Sweep - by BARRAGAN - This example code is in the public domain. - - modified 8 Nov 2013 - by Scott Fitzgerald - http://www.arduino.cc/en/Tutorial/Sweep -*/ +/* Crawler Slave + * + * This code runs on the crawler i2c network + * and provides a cleaner, less CPU intensive control over PWM devices. + */ #include #include -Servo myservo; // create servo object to control a servo -// twelve servo objects can be created on most boards - -int pos = 0; // variable to store the servo position +// This servo is used to wipe and clean the camera lens +Servo windowWiperServo; void setup() { - myservo.attach(9); // attaches the servo on pin 9 to the servo object + // Attach the wiper servo to pin 9 + windowWiperServo.attach(9); + // This is the address the pi will speak to us at Wire.begin(0x8); // Call receiveEvent when data received @@ -25,13 +22,15 @@ void setup() { } +// Just loop to keep the running code alive, and wait for events to happen. void loop() { - delay(10); + delay(50); } +// This method runs when we receive a message void receiveEvent(int howMany) { while (Wire.available()) { // loop through all but the last int pos = Wire.read(); // receive byte as a int - myservo.write(pos); + windowWiperServo.write(pos); } } From aa575f67a33fb15cf5db6a2a4db73aba72352da8 Mon Sep 17 00:00:00 2001 From: KenwoodFox Date: Sat, 3 Jul 2021 22:54:50 -0400 Subject: [PATCH 3/7] Create/modify read string code --- .../arduino/crawler_slave/crawler_slave.ino | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/crawler_software/arduino/crawler_slave/crawler_slave.ino b/crawler_software/arduino/crawler_slave/crawler_slave.ino index 447eef9..201cd47 100644 --- a/crawler_software/arduino/crawler_slave/crawler_slave.ino +++ b/crawler_software/arduino/crawler_slave/crawler_slave.ino @@ -10,16 +10,23 @@ // This servo is used to wipe and clean the camera lens Servo windowWiperServo; +// This is a variable capable of storing up to 4 items +char instruction[4] = ""; + void setup() { // Attach the wiper servo to pin 9 windowWiperServo.attach(9); + // This is only useful for debugging + Serial.begin(9600); + // This is the address the pi will speak to us at Wire.begin(0x8); // Call receiveEvent when data received Wire.onReceive(receiveEvent); - + + Serial.print("Finished Setup."); } // Just loop to keep the running code alive, and wait for events to happen. @@ -28,9 +35,11 @@ void loop() { } // This method runs when we receive a message -void receiveEvent(int howMany) { - while (Wire.available()) { // loop through all but the last - int pos = Wire.read(); // receive byte as a int - windowWiperServo.write(pos); +void receiveEvent(int n) { + for (int i = 0; i < n; i++) //n is equal to the number of data bytes received + { + instruction[i] = Wire.read(); } + + Serial.print(instruction); } From 83a33cc20225116343286ea05b7d126f8f37cf16 Mon Sep 17 00:00:00 2001 From: KenwoodFox Date: Sat, 3 Jul 2021 23:57:17 -0400 Subject: [PATCH 4/7] Finalize better reading of full string over i2c and splitting --- .../arduino/crawler_slave/crawler_slave.ino | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/crawler_software/arduino/crawler_slave/crawler_slave.ino b/crawler_software/arduino/crawler_slave/crawler_slave.ino index 201cd47..dcfca5c 100644 --- a/crawler_software/arduino/crawler_slave/crawler_slave.ino +++ b/crawler_software/arduino/crawler_slave/crawler_slave.ino @@ -11,7 +11,11 @@ Servo windowWiperServo; // This is a variable capable of storing up to 4 items -char instruction[4] = ""; +char instruction[32] = ""; + +// Values for the id, and raw value of an instruction +char * id; +char * raw_value; void setup() { // Attach the wiper servo to pin 9 @@ -21,12 +25,12 @@ void setup() { Serial.begin(9600); // This is the address the pi will speak to us at - Wire.begin(0x8); + Wire.begin(0x4); // Call receiveEvent when data received Wire.onReceive(receiveEvent); - Serial.print("Finished Setup."); + Serial.println("Finished Setup."); } // Just loop to keep the running code alive, and wait for events to happen. @@ -36,10 +40,27 @@ void loop() { // This method runs when we receive a message void receiveEvent(int n) { - for (int i = 0; i < n; i++) //n is equal to the number of data bytes received - { + + for (int i = 0; i < n; i++) { instruction[i] = Wire.read(); + instruction[i + 1] = '\0'; //add null after ea. char + } + + //RPi first byte is cmd byte so shift everything to the left 1 pos so temp contains our string + for (int i = 0; i < n; ++i) + instruction[i] = instruction[i + 1]; + + id = strtok (instruction, "-"); + raw_value = strtok (NULL, "-"); + + int value = atoi(raw_value); + + Serial.println(id); + Serial.println(value); + + // Switch statements dont work on char * + if (strcmp(id,"WIPE") == 0) // if id, and WIPE cancel out + { + windowWiperServo.write(value); } - - Serial.print(instruction); } From 227815ac32c87f17ea211849267c22dce13d7666 Mon Sep 17 00:00:00 2001 From: KenwoodFox Date: Sun, 4 Jul 2021 00:07:36 -0400 Subject: [PATCH 5/7] Everything moves a lot smoother without serial: todo jump speed? --- crawler_software/arduino/crawler_slave/crawler_slave.ino | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crawler_software/arduino/crawler_slave/crawler_slave.ino b/crawler_software/arduino/crawler_slave/crawler_slave.ino index dcfca5c..820dccb 100644 --- a/crawler_software/arduino/crawler_slave/crawler_slave.ino +++ b/crawler_software/arduino/crawler_slave/crawler_slave.ino @@ -22,7 +22,7 @@ void setup() { windowWiperServo.attach(9); // This is only useful for debugging - Serial.begin(9600); + //Serial.begin(9600); // This is the address the pi will speak to us at Wire.begin(0x4); @@ -30,7 +30,7 @@ void setup() { // Call receiveEvent when data received Wire.onReceive(receiveEvent); - Serial.println("Finished Setup."); + //Serial.println("Finished Setup."); } // Just loop to keep the running code alive, and wait for events to happen. @@ -55,8 +55,8 @@ void receiveEvent(int n) { int value = atoi(raw_value); - Serial.println(id); - Serial.println(value); + //Serial.println(id); + //Serial.println(value); // Switch statements dont work on char * if (strcmp(id,"WIPE") == 0) // if id, and WIPE cancel out From 941b5ac4e8a0d6ece93675f13099229a48a56121 Mon Sep 17 00:00:00 2001 From: KenwoodFox Date: Sun, 4 Jul 2021 12:16:41 -0400 Subject: [PATCH 6/7] Create crawler service file. --- crawler_software/raspberry_pi/crawler.service | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 crawler_software/raspberry_pi/crawler.service diff --git a/crawler_software/raspberry_pi/crawler.service b/crawler_software/raspberry_pi/crawler.service new file mode 100644 index 0000000..57363ad --- /dev/null +++ b/crawler_software/raspberry_pi/crawler.service @@ -0,0 +1,12 @@ +[Unit] +Description=Crawler service overseer, manages running main crawler software +After=multi-user.target +Conflicts=getty@tty1.service + +[Service] +Type=simple +ExecStart=/usr/bin/python /srv/crawler/crawler.py +StandardInput=tty-force + +[Install] +WantedBy=multi-user.target From 2ee6bbef907d97d87e9236b09e2e34810a95942b Mon Sep 17 00:00:00 2001 From: KenwoodFox Date: Sun, 4 Jul 2021 12:18:43 -0400 Subject: [PATCH 7/7] Modify crawler service file. --- crawler_software/raspberry_pi/crawler.service | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crawler_software/raspberry_pi/crawler.service b/crawler_software/raspberry_pi/crawler.service index 57363ad..e3fcca5 100644 --- a/crawler_software/raspberry_pi/crawler.service +++ b/crawler_software/raspberry_pi/crawler.service @@ -1,12 +1,11 @@ [Unit] Description=Crawler service overseer, manages running main crawler software After=multi-user.target -Conflicts=getty@tty1.service [Service] Type=simple +Restart=always ExecStart=/usr/bin/python /srv/crawler/crawler.py -StandardInput=tty-force [Install] WantedBy=multi-user.target