From 8cb00734d2aaf87c2bc8b6087692d8e142e71f72 Mon Sep 17 00:00:00 2001 From: ddebernardde <diego.debernard@student.uclouvain.be> Date: Wed, 9 Nov 2022 02:17:10 +0100 Subject: [PATCH] bon avancement --- I2C.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++-- Makefile | 8 +++--- encoder.c | 7 +++-- 3 files changed, 88 insertions(+), 9 deletions(-) diff --git a/I2C.cpp b/I2C.cpp index 01a2603..b269c9b 100644 --- a/I2C.cpp +++ b/I2C.cpp @@ -8,11 +8,21 @@ using namespace std; +void takeARide(int speed); +void StopTheRide(); + const int LED = 0x08; const int ON = 0x01; const int OFF = 0x00; -int main(int argc, char const *argv[]) { +const int ADC = 0x48; + +const int PWM_PIN_RIGHT = 23; +const int M1DIR_RIGHT = 21; +const int PWM_PIN_LEFT = 26; +const int M2DIR_LEFT = 22; + +void useTheLED() { int fd = wiringPiI2CSetup(LED); @@ -22,13 +32,81 @@ int main(int argc, char const *argv[]) { printf("LIGHT ON!\n"); } - delay(5000); + for (int i = 0; i < 10; i++) { + wiringPiI2CWrite(fd, ON); + delay(770); + wiringPiI2CWrite(fd, OFF); + delay(770); + } wiringPiI2CWrite(fd, OFF); if (wiringPiI2CRead(fd) == OFF) { printf("LIGHT OFF!\n"); } +} + +void useADC() { + + int fd = wiringPiI2CSetup(ADC); + + cout << wiringPiI2CRead(fd) << endl; + + takeARide(600); + for (int i = 0; i < 10; i++) + { + cout << wiringPiI2CRead(fd) << endl; + delay(500); + } + StopTheRide(); + cout << wiringPiI2CRead(fd) << endl; + +} + +void takeARide(int speed) { + + int wheelSpeed = speed; + + if (wiringPiSetup() == -1) { + printf("WiringPiSetup failed\n"); + exit(EXIT_FAILURE); + } + pinMode(PWM_PIN_RIGHT, PWM_OUTPUT); + pinMode(PWM_PIN_LEFT, PWM_OUTPUT); + pinMode(M1DIR_RIGHT, OUTPUT); + pinMode(M2DIR_LEFT, OUTPUT); + + printf("pin ready !\n"); + + digitalWrite(M1DIR_RIGHT, 1); + digitalWrite(M2DIR_LEFT, 0); + pwmWrite(PWM_PIN_RIGHT, wheelSpeed); + pwmWrite(PWM_PIN_LEFT, wheelSpeed); + + printf("There we go !\n"); + +} + +void StopTheRide() { + + printf("And we stop\n"); + + pwmWrite(PWM_PIN_RIGHT, 0); + pwmWrite(PWM_PIN_LEFT, 0); +} + +int main(int argc, char const *argv[]) { + + string answer; + + cout << "led or adc ?" << endl; + cin >> answer ; + + if (answer == "led") useTheLED(); + else if (answer == "adc") { + useADC(); + } + return 0; } diff --git a/Makefile b/Makefile index 22a4528..3e2d8c7 100644 --- a/Makefile +++ b/Makefile @@ -6,8 +6,8 @@ switch : compile_switch motor : compile_motor sudo ./motor -led : compile_LED - sudo ./led +i2c : compile_LED + sudo ./i2c # compilations @@ -18,8 +18,8 @@ compile_motor : gcc -o motor motor.c -lwiringPi compile_LED : - g++ -o led I2C.cpp -lwiringPi + g++ -o i2c I2C.cpp -lwiringPi .PHONY : clean clean : - rm -rf switch motor led + rm -rf switch motor led i2c diff --git a/encoder.c b/encoder.c index b7977b2..8d22335 100644 --- a/encoder.c +++ b/encoder.c @@ -14,9 +14,10 @@ const int ENC_PIN_2A = 35; //from FPGA to ENCODER -> ouput data from encod const int ENC_PIN_2B = 34; //from FPGA to ENCODER -> ouput data from encoder const int SCK_PIN = 14; //from RASPI to FPGA -> clock signal const int SCK_PIN_FPGA = 13; //from FPGA to ENCODER -> clock signal -const int SS_BAR_1 = 10; //CE0 from RASPI to FPGA -> enable encoder -const int SS_BAR_2 = 11; //CE1 from RASPI to FPGA -> enable encoder - +const int SS_PIN_1 = 10; //CE0 from RASPI to FPGA -> enable encoder +const int SS_PIN_2 = 11; //CE1 from RASPI to FPGA -> enable encoder +const int CE0_PIN_FPGA = 11; +const int CE1_PIN_FPGA = 11; int main(int argc, char const *argv[]) { if (wiringPiSetup() == -1) { -- GitLab