diff --git a/.gitignore b/.gitignore index 9a23d88..92a805a 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,7 @@ serial_read/go.mod serial_read/go.sum *.exe + +monitor/go.sum + +monitor/go.mod diff --git a/monitor/main.go b/monitor/main.go new file mode 100644 index 0000000..8565eeb --- /dev/null +++ b/monitor/main.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "foglar/monitor/serial_read" + "log" + // "foglar/monitor/parse" +) + +func main() { + // Initialize serial connection + serialHandler, err := serial_read.NewSerialHandler() + if err != nil { + log.Fatal(err) + } + + defer serialHandler.Close() + + // Read serial data + data, err := serialHandler.ReadSerial() + if err != nil { + log.Fatal(err) + } + + fmt.Println("Received data:", data) +} diff --git a/monitor/parse/parse.go b/monitor/parse/parse.go new file mode 100644 index 0000000..dbbb77a --- /dev/null +++ b/monitor/parse/parse.go @@ -0,0 +1,3 @@ +package parse + +func parse() {} diff --git a/monitor/serial_read/serial.go b/monitor/serial_read/serial.go new file mode 100644 index 0000000..2ef1035 --- /dev/null +++ b/monitor/serial_read/serial.go @@ -0,0 +1,44 @@ +package serial_read + +import ( + "github.com/tarm/serial" + "log" +) + +// TODO: +// - Validation of port and baudrate +// - And input of port and baudrate + +// SerialHandler is a struct to handle serial communication +type SerialHandler struct { + port *serial.Port +} + +// NewSerialHandler initializes a new SerialHandler +func NewSerialHandler() (*SerialHandler, error) { + // CALL port and baudrate input from user !!! + s, err := serial.OpenPort(&serial.Config{Name: "/dev/ttyACM0", Baud: 9600}) + if err != nil { + log.Fatal(err) + return nil, err + } + + return &SerialHandler{port: s}, nil +} + +// ReadSerial reads from the serial port and returns the received data as a string +func (sh *SerialHandler) ReadSerial() (string, error) { + buf := make([]byte, 128) + n, err := sh.port.Read(buf) + if err != nil { + log.Fatal(err) + return "", err + } + + return string(buf[:n]), nil +} + +// Close closes the serial port +func (sh *SerialHandler) Close() error { + return sh.port.Close() +} diff --git a/receiver_module/receiver_module.ino b/receiver_module/receiver_module.ino index 8e928ac..1e25700 100644 --- a/receiver_module/receiver_module.ino +++ b/receiver_module/receiver_module.ino @@ -15,7 +15,7 @@ void setup() { void loop() { //Read the data if available in buffer if (radio.available()) { - char text[32] = { 0 }; + char text[64] = { 0 }; radio.read(&text, sizeof(text)); Serial.println(text); } diff --git a/sender_module/sender_module.ino b/sender_module/sender_module.ino index 04ac6fc..378a1cd 100644 --- a/sender_module/sender_module.ino +++ b/sender_module/sender_module.ino @@ -50,10 +50,42 @@ void loop() { imuDataGet( &stAngles, &stGyroRawData, &stAccelRawData, &stMagnRawData); pressSensorDataGet(&s32TemperatureVal, &s32PressureVal, &s32AltitudeVal); - char result[8]; + + char temp_str[8], pressure_str[8], altitude_str[8]; + char roll_str[8], pitch_str[8], yaw_str[8]; + float temperature = (s32TemperatureVal / 100); - dtostrf(temperature, 6, 2, result); - //Send message to receiver - radio.write(&result, sizeof(result)); - //delay(1000); -} \ No newline at end of file + float pressure = (s32PressureVal / 100); + float altitude = (s32AltitudeVal / 100); + + float roll = stAngles.fRoll; + float pitch = stAngles.fPitch; + float yaw = stAngles.fYaw; + + dtostrf(temperature, 6, 2, temp_str); + dtostrf(pressure, 6, 2, pressure_str); + dtostrf(altitude, 6, 2, altitude_str); + + dtostrf(roll, 6, 2, roll_str); + dtostrf(pitch, 6, 2, pitch_str); + dtostrf(yaw, 6, 2, yaw_str); + + //Serial.println(roll, pitch, yaw); + //Serial.println(temperature, pressure, altitude); + + char msg[64]; + + String ctemp = ("$1;"+String(temp_str)+"*"); + String cpressure = ("$2;"+String(pressure_str)+"*"); + String caltitude = ("$3;"+String(altitude_str)+"*"); + String croll = ("$4;"+String(roll_str)+"*"); + String cpitch = ("$5;"+String(pitch_str)+"*"); + String cyaw = ("$6;"+String(yaw_str) + "*"); + + ctemp.toCharArray(msg, sizeof(msg)); + radio.write(&msg, sizeof(msg)); + cpressure.toCharArray(msg, sizeof(msg)); + radio.write(&msg, sizeof(msg)); + + delay(200); +} diff --git a/serial_read/main.go b/serial_read/main.go index 1502d05..487d33f 100644 --- a/serial_read/main.go +++ b/serial_read/main.go @@ -73,6 +73,7 @@ func main() { var port string var baudrate int + // parsing command line arguments if len(args) == 0 { port, baudrate = UserInput() } else {