add a TCP connection

This commit is contained in:
ackimixs
2024-04-01 20:30:07 +02:00
parent a09d92ead4
commit b8c2097ae7
9 changed files with 264 additions and 67 deletions

127
.gitignore vendored Normal file
View File

@@ -0,0 +1,127 @@
### C++ template
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
### CMake template
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
### CLion template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf
# AWS User-specific
.idea/**/aws.xml
# Generated files
.idea/**/contentModel.xml
# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml
# Gradle
.idea/**/gradle.xml
.idea/**/libraries
# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr
# CMake
cmake-build-*/
build/
# Mongo Explorer plugin
.idea/**/mongoSettings.xml
# File-based project format
*.iws
# IntelliJ
out/
# mpeltonen/sbt-idea plugin
.idea_modules/
# JIRA plugin
atlassian-ide-plugin.xml
# Cursive Clojure plugin
.idea/replstate.xml
# SonarLint plugin
.idea/sonarlint/
# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
# Editor-based Rest Client
.idea/httpRequests
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

14
CMakeLists.txt Normal file
View File

@@ -0,0 +1,14 @@
cmake_minimum_required(VERSION 3.25)
project(servo_motor)
set(CMAKE_CXX_STANDARD 17)
find_package(PkgConfig REQUIRED)
pkg_check_modules(TCPSocket REQUIRED TCPSocket)
add_executable(servo_motor servo_motor.cpp
MyTCPClient.cpp
MyTCPClient.h
main.cpp)
target_link_libraries(servo_motor TCPSocket)

62
MyTCPClient.cpp Normal file
View File

@@ -0,0 +1,62 @@
#include "MyTCPClient.h"
MyTCPClient::MyTCPClient(const char *serverIP, int port) : TCPClient(serverIP, port) {
this->handle = -1;
}
int MyTCPClient::init() {
if (gpioInitialise() < 0)
{
fprintf(stderr, "Impossible d'initialiser pigpio\n");
return 1;
}
this->handle = i2cOpen(1, PCA9685_ADDR, 0);
if (this->handle < 0)
{
fprintf(stderr, "Impossible d'ouvrir la connexion I2C\n");
gpioTerminate();
return 1;
}
initPCA9685(this->handle);
return 0;
}
void MyTCPClient::handleMessage(const std::string &message) {
std::vector<std::string> token = TCPSocket::split(message, ";");
if (token.size() != 4) {
std::cerr << "Message invalide: " << message << std::endl;
return;
}
if (token[1] == "servo_pot" || token[1] == "all") {
if (token[2] == "ping") {
this->sendMessage("pong");
}
else if (token[2] == "ouvrir pince") {
int pince = std::stoi(token[3]);
ouvrir_pince(handle, pince);
}
else if (token[2] == "fermer pince") {
int pince = std::stoi(token[3]);
fermer_pince(handle, pince);
}
else if (token[2] == "baisser bras") {
baisser_bras(handle);
}
else if (token[2] == "lever bras") {
lever_bras(handle);
}
}
}
MyTCPClient::~MyTCPClient() {
i2cClose(this->handle);
// Terminaison de pigpio
gpioTerminate();
}

20
MyTCPClient.h Normal file
View File

@@ -0,0 +1,20 @@
#pragma once
#include "TCPSocket/TCPClient.hpp"
#include "TCPSocket/TCPUtils.hpp"
#include "servo_motor.h"
class MyTCPClient : public TCPClient {
public:
explicit MyTCPClient(const char* serverIP = "127.0.0.1", int port = 8080);
~MyTCPClient() override;
int init();
void handleMessage(const std::string &message) override;
private:
int handle;
};

36
main.cpp Normal file
View File

@@ -0,0 +1,36 @@
#include "MyTCPClient.h"
#include "servo_motor.h"
int main(int argc, char* argv[]) {
int port = 8080;
if (argc > 1) {
port = std::stoi(argv[1]);
}
MyTCPClient client("127.0.0.1", port);
int err = client.init();
if (err != 0) {
return err;
}
client.start();
while (true) {
std::string message;
std::cout << "Enter message ('quit' to exit): ";
std::getline(std::cin, message);
if (message == "quit") {
client.stop();
break;
}
client.sendMessage(message.c_str());
}
client.stop();
return 0;
}

View File

@@ -1,20 +1,4 @@
#include <stdio.h>
#include <pigpio.h>
#include <time.h>
//Define registers address
#define PCA9685_ADDR 0x40
#define MODE1_REG 0x00
#define PRE_SCALE 0xFE
#define LED0_ON_L 0x06
#define LED0_ON_H 0x07
#define LED0_OFF_L 0x08
#define LED0_OFF_H 0x09
//Define duty cycle (in a range of 12 bits = 4096) for PWM (min 2%, max 12%)
#define SERVO_MIN 82 // 0.02*4096
#define SERVO_MAX 492 // 0.12*4096
#include "servo_motor.h"
// Fonction pour initialiser le PCA9685
void initPCA9685(int handle)
@@ -115,54 +99,7 @@ void ouvrir_pince(int handle, int pince){
break;
case 2:
angle = 125;
break;
break;
}
setServoPosition(handle, pince, angle);
}
int main()
{
int handle;
if (gpioInitialise() < 0)
{
fprintf(stderr, "Impossible d'initialiser pigpio\n");
return 1;
}
handle = i2cOpen(1, PCA9685_ADDR, 0);
if (handle < 0)
{
fprintf(stderr, "Impossible d'ouvrir la connexion I2C\n");
gpioTerminate();
return 1;
}
initPCA9685(handle);
lever_bras(handle);
gpioSleep(PI_TIME_RELATIVE, 1, 0);
ouvrir_pince(handle, 0);
gpioSleep(PI_TIME_RELATIVE, 1, 0); // Pause de 2 secondes
ouvrir_pince(handle, 1);
gpioSleep(PI_TIME_RELATIVE, 1, 0); // Pause de 2 secondes
ouvrir_pince(handle, 2);
gpioSleep(PI_TIME_RELATIVE, 1, 0); // Pause de 2 secondes
fermer_pince(handle, 0);
gpioSleep(PI_TIME_RELATIVE, 1, 0); // Pause de 2 secondes
fermer_pince(handle, 1);
gpioSleep(PI_TIME_RELATIVE, 1, 0); // Pause de 2 secondes
fermer_pince(handle, 2);
gpioSleep(PI_TIME_RELATIVE, 1, 0);
baisser_bras(handle);
gpioSleep(PI_TIME_RELATIVE, 1, 0); // Pause de 2 secondes
i2cClose(handle);
// Terminaison de pigpio
gpioTerminate();
return 0;
}

View File

@@ -1,8 +1,9 @@
#ifndef SERVO_MOTOR
#define SERVO_MOTOR
#include <stdio.h>
#include <iostream>
#include <pigpio.h>
#include <time.h>
#include <ctime>
//Define registers address
#define PCA9685_ADDR 0x40

Binary file not shown.

Binary file not shown.