Compare commits

...

10 Commits

Author SHA1 Message Date
Paul Dino Jones e1ddd73352 Add wheelslip and abs to test loop 2024-05-28 15:00:46 +00:00
Paul Dino Jones aac41aa882 Updated to latest simapi 2024-03-09 13:39:12 -05:00
Paul Dino Jones bf6eec8c26 Updated to latest simapi 2024-03-09 13:36:18 -05:00
Paul Dino Jones 33d0b44f98 Fix wrong data type for velocity 2024-03-05 17:23:29 -05:00
Paul Dino Jones 34e9ba4405 Fix compilation issue for wheel slip 2024-03-05 00:36:34 -05:00
Paul Dino Jones e29cc12ab4 Fix compilation issues for arduino devices 2024-03-05 00:34:29 -05:00
Paul Dino Jones eb18a05c51 Refactored serial arduino devices. Created custom structs for sending data to arduino devices. 2024-03-05 04:51:49 +00:00
Paul Dino Jones d2e0bebc4e Added preliminary support for tyre slip and abs vibrations 2024-02-29 19:19:57 +00:00
Paul Dino Jones 46adafd5f0 Fixed simapi version 2024-02-28 14:10:13 -05:00
Paul Dino Jones b51d570b73 Add makefiles for arduino and related documentation 2024-02-27 17:36:02 -05:00
19 changed files with 684 additions and 278 deletions

View File

@ -29,7 +29,7 @@ Cross Platform device manager for driving and flight simulators, for use with co
- argtable2 - argtable2
- libconfig - libconfig
- [slog](https://github.com/kala13x/slog) (static) - [slog](https://github.com/kala13x/slog) (static)
- [wine-linux-shm-adapter](https://github.com/spacefreak18/simshmbridge) - for sims that need shared memory mapping like AC. - [simshmbridge](https://github.com/spacefreak18/simshmbridge) - for sims that need shared memory mapping like AC and Project Cars related.
- [simapi](https://github.com/spacefreak18/simapi) - [simapi](https://github.com/spacefreak18/simapi)
## Building ## Building
@ -51,9 +51,36 @@ to use the pulseaudio backend use this cmake command
cmake -DUSE_PULSEAUDIO=YES .. cmake -DUSE_PULSEAUDIO=YES ..
``` ```
## Testing ## Using Arduino Devices
### Setting up Your Arduino Device Currently Monocoque supports simwind and shiftlights through the included arduino sketches which have been tested on Uno boards. The simwind controller requires a Motor shield.
There are included Makefiles for each controller. For now, the makefiles expect the device to be attached at /dev/ttyACM0. So unplug both controllers, and then plug in just the
controller you're updating to ensure the correct controller is at /dev/ttyACM0.
To compile and upload these sketches, the Makefiles use arduino-cli. Unfortunately it seems some distributions such as debian do not include this in the repositories. If this is
the case follow the install instructions here:
```
https://arduino.github.io/arduino-cli/0.35/installation/
```
You may have to download the core libraries, it will prompt you to do so if you do not have them and you go further
```
arduino-cli core install arduino:avr
```
Then for shiftlights navigate to included shiftlight directory ( be sure only the shiftlight controller is plugged into the machine and is available at /dev/ttyACM0 ) and
```
arduino-cli lib install FastLED
make
```
Then for simwind navigate to the included simwind directory ( be sure only the simwind controller is plugged into the machine and is available at /dev/ttyACM0 ) and
```
ARDUINO_LIBRARY_ENABLE_UNSAFE_INSTALL=true arduino-cli lib install --git-url https://github.com/adafruit/Adafruit_Motor_Shield_V2_Library.git
ARDUINO_LIBRARY_ENABLE_UNSAFE_INSTALL=true arduino-cli lib install --git-url https://github.com/adafruit/Adafruit_BusIO.git
make
```
## Testing
### Static Analysis ### Static Analysis
``` ```

View File

@ -0,0 +1,166 @@
# Makefile for Arduino based scketches
#
# Copyright 2020 Valerio Di Giampietro http://va.ler.io v@ler.io
# MIT License - see License.txt file
#
# This Makefile uses the arduino-cli, the Arduino command line interface
# and has been designed and tested to run on Linux, not on Windows.
# Probably it will run on a Mac, but it has not been tested.
#
# Please note that:
#
# 1. each sketch must reside in his own folder with this Makefile
#
# 2. the main make targets are:
# - all compiles and upload
# - compile compiles only
# - upload upload via serial port, compile if the binary file is
# not available
# - ota upload Over The Air, automatically find the device
# IP address using the IOT_NAME (device hostname)
# - clean clean the build directory
# - find find OTA updatable devices on the local subnet
# - requirements it the file "requirements.txt" exists it will
# install the libraries listed in this file
#
# default is "all"
#
# 3. it gets the name of the sketch using the wildcard make command;
# the name is *.ino; this means that you must have ONLY a file
# with .ino extension, otherwise this makefile will break. This
# also means that you can use this Makefile, almost unmodified,
# for any sketch as long as you keep a single .ino file in each
# folder
#
# 4. you can split your project in multiple files, if you wish,
# using a single .ino file and multiple .h files, that you can
# include in the .ino file with an '#include "myfile.h"'
# directive
#
# Optionally some environment variables can be set:
#
# FQBN Fully Qualified Board Name; if not set in the environment
# it will be assigned a value in this makefile
#
# SERIAL_DEV Serial device to upload the sketch; if not set in the
# environment it will be assigned:
# /dev/ttyUSB0 if it exists, or
# /dev/ttyACM0 if it exists, or
# unknown
#
# IOT_NAME Name of the IOT device; if not set in the environment
# it will be assigned a value in this makefile. This is
# very useful for OTA update, the device will be searched
# on the local subnet using this name
#
# OTA_PORT Port used by OTA update; if not set in the environment
# it will be assigned the default value of 8266 in this
# makefile
#
# OTA_PASS Password used for OTA update; if not set in the environment
# it will be assigned the default value of an empty string
#
# V verbose flag; can be 0 (quiet) or 1 (verbose); if not set
# in the environment it will be assigned a default value
# in this makefile
MAKE_DIR := $(PWD)
#
# ----- setup wor Wemos D1 mini -----
#FQBN ?= esp8266:esp8266:d1_mini
#IOT_NAME ?= esp8266-meteo
#OTA_PORT ?= 8266
#OTA_PASS ?=
# ----- setup for Arduino Uno
FQBN ?= arduino:avr:uno
# ----- ---------------------
V ?= 0
VFLAG =
ifeq "$(V)" "1"
VFLAG =-v
endif
ifndef SERIAL_DEV
ifneq (,$(wildcard /dev/ttyUSB0))
SERIAL_DEV = /dev/ttyUSB0
else ifneq (,$(wildcard /dev/ttyACM0))
SERIAL_DEV = /dev/ttyACM0
else
SERIAL_DEV = unknown
endif
endif
BUILD_DIR := $(subst :,.,build/$(FQBN))
SRC := $(wildcard *.ino)
HDRS := $(wildcard *.h)
BIN := $(BUILD_DIR)/$(SRC).bin
ELF := $(BUILD_DIR)/$(SRC).elf
$(info FQBN is [${FQBN}])
$(info IOT_NAME is [${IOT_NAME}])
$(info OTA_PORT is [${OTA_PORT}])
$(info OTA_PASS is [${OTA_PASS}])
$(info V is [${V}])
$(info VFLAG is [${VFLAG}])
$(info MAKE_DIR is [${MAKE_DIR}])
$(info BUILD_DIR is [${BUILD_DIR}])
$(info SRC is [${SRC}])
$(info HDRS is [${HDRS}])
$(info BIN is [${BIN}])
$(info SERIAL_DEV is [${SERIAL_DEV}])
all: $(ELF) upload
.PHONY: all
compile: $(ELF)
.PHONY: compile
$(ELF): $(SRC) $(HDRS)
arduino-cli compile -b $(FQBN) $(VFLAG)
@if which arduino-manifest.pl; \
then echo "---> Generating manifest.txt"; \
arduino-manifest.pl -b $(FQBN) $(SRC) $(HDRS) > manifest.txt; \
else echo "---> If you want to generate manifest.txt, listing used libraries and their versions,"; \
echo "---> please install arduino-manifest, see https://github.com/digiampietro/arduino-manifest"; \
fi
upload:
@if [ ! -c $(SERIAL_DEV) ] ; \
then echo "---> ERROR: Serial Device not available, please set the SERIAL_DEV environment variable" ; \
else echo "---> Uploading sketch\n"; \
arduino-cli upload -b $(FQBN) -p $(SERIAL_DEV) $(VFLAG); \
fi
ota:
@PLAT_PATH=`arduino-cli compile -b $(FQBN) --show-properties | grep '^runtime.platform.path' | awk -F= '{print $$2}'` ; \
PY_PATH=`arduino-cli compile -b $(FQBN) --show-properties | grep '^runtime.tools.python3.path' | awk -F= '{print $$2}'` ; \
IOT_IP=`avahi-browse _arduino._tcp --resolve --parsable --terminate|grep -i ';$(IOT_NAME);'|grep ';$(OTA_PORT);'| awk -F\; '{print $$8}'|head -1`; \
BINFILE=$(wildcard $(BUILD_DIR)/$(SRC)*bin); \
echo "PLAT_PATH is [$$PLAT_PATH]" ; \
echo "PY_PATH: is [$$PY_PATH]" ; \
echo "IOT_IP: is [$$IOT_IP]" ; \
echo "BINFILE: is [$$BINFILE]" ; \
if [ "$$IOT_IP" = "" ] ; \
then echo "Unable to find device IP. Check that the IOT_NAME environment variable is correctly set. Use 'make find' to search devices"; \
else echo "---> Uploading Over The Air"; \
$$PY_PATH/python3 $$PLAT_PATH/tools/espota.py -i $$IOT_IP -p $(OTA_PORT) --auth=$(OTA_PASS) -f $$BINFILE ;\
fi
clean:
@echo "---> Cleaning the build directory"
rm -rf build
find:
avahi-browse _arduino._tcp --resolve --parsable --terminate
requirements:
@if [ -e requirements.txt ]; \
then while read -r i ; do echo ; \
echo "---> Installing " '"'$$i'"' ; \
arduino-cli lib install "$$i" ; \
done < requirements.txt ; \
else echo "---> MISSING requirements.txt file"; \
fi

View File

@ -0,0 +1,27 @@
#ifndef _SHIFTLIGHTSDATA_H
#define _SHIFTLIGHTSDATA_H
#include <stdint.h>
#include <stdbool.h>
typedef struct
{
unsigned char color_1_red;
unsigned char color_1_green;
unsigned char color_1_blue;
unsigned char color_2_red;
unsigned char color_2_green;
unsigned char color_2_blue;
unsigned char color_3_red;
unsigned char color_3_green;
unsigned char color_3_blue;
unsigned char space_1;
unsigned char space_2;
unsigned char space_3;
uint32_t maxrpm;
uint32_t rpm;
}
ShiftLightsData;
#endif

View File

@ -1,7 +1,7 @@
#include <FastLED.h> #include <FastLED.h>
#include "simdata.h" #include "shiftlights.h"
#define SIMDATA_SIZE sizeof(SimData) #define SIMDATA_SIZE sizeof(ShiftLightsData)
#define LED_PIN 7 #define LED_PIN 7
#define NUM_LEDS 6 #define NUM_LEDS 6
@ -18,7 +18,7 @@
#define COLOR3C 0 #define COLOR3C 0
CRGB leds[NUM_LEDS]; CRGB leds[NUM_LEDS];
SimData sd; ShiftLightsData sd;
int maxrpm = 0; int maxrpm = 0;
int rpm = 0; int rpm = 0;
int numlights = NUM_LEDS; int numlights = NUM_LEDS;
@ -39,11 +39,8 @@ void setup()
} }
FastLED.clear(); FastLED.clear();
sd.rpms = 0; sd.rpm = 0;
sd.maxrpm = 6500; sd.maxrpm = 6500;
sd.altitude = 10;
sd.pulses = 40000;
sd.velocity = 10;
} }
void loop() void loop()
@ -55,12 +52,10 @@ void loop()
{ {
Serial.readBytes(buff, SIMDATA_SIZE); Serial.readBytes(buff, SIMDATA_SIZE);
memcpy(&sd, &buff, SIMDATA_SIZE); memcpy(&sd, &buff, SIMDATA_SIZE);
rpm = sd.rpms; rpm = sd.rpm;
maxrpm = sd.maxrpm; maxrpm = sd.maxrpm;
} }
while (l < numlights) while (l < numlights)
{ {
lights[l] = 0; lights[l] = 0;

View File

@ -0,0 +1,166 @@
# Makefile for Arduino based scketches
#
# Copyright 2020 Valerio Di Giampietro http://va.ler.io v@ler.io
# MIT License - see License.txt file
#
# This Makefile uses the arduino-cli, the Arduino command line interface
# and has been designed and tested to run on Linux, not on Windows.
# Probably it will run on a Mac, but it has not been tested.
#
# Please note that:
#
# 1. each sketch must reside in his own folder with this Makefile
#
# 2. the main make targets are:
# - all compiles and upload
# - compile compiles only
# - upload upload via serial port, compile if the binary file is
# not available
# - ota upload Over The Air, automatically find the device
# IP address using the IOT_NAME (device hostname)
# - clean clean the build directory
# - find find OTA updatable devices on the local subnet
# - requirements it the file "requirements.txt" exists it will
# install the libraries listed in this file
#
# default is "all"
#
# 3. it gets the name of the sketch using the wildcard make command;
# the name is *.ino; this means that you must have ONLY a file
# with .ino extension, otherwise this makefile will break. This
# also means that you can use this Makefile, almost unmodified,
# for any sketch as long as you keep a single .ino file in each
# folder
#
# 4. you can split your project in multiple files, if you wish,
# using a single .ino file and multiple .h files, that you can
# include in the .ino file with an '#include "myfile.h"'
# directive
#
# Optionally some environment variables can be set:
#
# FQBN Fully Qualified Board Name; if not set in the environment
# it will be assigned a value in this makefile
#
# SERIAL_DEV Serial device to upload the sketch; if not set in the
# environment it will be assigned:
# /dev/ttyUSB0 if it exists, or
# /dev/ttyACM0 if it exists, or
# unknown
#
# IOT_NAME Name of the IOT device; if not set in the environment
# it will be assigned a value in this makefile. This is
# very useful for OTA update, the device will be searched
# on the local subnet using this name
#
# OTA_PORT Port used by OTA update; if not set in the environment
# it will be assigned the default value of 8266 in this
# makefile
#
# OTA_PASS Password used for OTA update; if not set in the environment
# it will be assigned the default value of an empty string
#
# V verbose flag; can be 0 (quiet) or 1 (verbose); if not set
# in the environment it will be assigned a default value
# in this makefile
MAKE_DIR := $(PWD)
#
# ----- setup wor Wemos D1 mini -----
#FQBN ?= esp8266:esp8266:d1_mini
#IOT_NAME ?= esp8266-meteo
#OTA_PORT ?= 8266
#OTA_PASS ?=
# ----- setup for Arduino Uno
FQBN ?= arduino:avr:uno
# ----- ---------------------
V ?= 0
VFLAG =
ifeq "$(V)" "1"
VFLAG =-v
endif
ifndef SERIAL_DEV
ifneq (,$(wildcard /dev/ttyUSB0))
SERIAL_DEV = /dev/ttyUSB0
else ifneq (,$(wildcard /dev/ttyACM0))
SERIAL_DEV = /dev/ttyACM0
else
SERIAL_DEV = unknown
endif
endif
BUILD_DIR := $(subst :,.,build/$(FQBN))
SRC := $(wildcard *.ino)
HDRS := $(wildcard *.h)
BIN := $(BUILD_DIR)/$(SRC).bin
ELF := $(BUILD_DIR)/$(SRC).elf
$(info FQBN is [${FQBN}])
$(info IOT_NAME is [${IOT_NAME}])
$(info OTA_PORT is [${OTA_PORT}])
$(info OTA_PASS is [${OTA_PASS}])
$(info V is [${V}])
$(info VFLAG is [${VFLAG}])
$(info MAKE_DIR is [${MAKE_DIR}])
$(info BUILD_DIR is [${BUILD_DIR}])
$(info SRC is [${SRC}])
$(info HDRS is [${HDRS}])
$(info BIN is [${BIN}])
$(info SERIAL_DEV is [${SERIAL_DEV}])
all: $(ELF) upload
.PHONY: all
compile: $(ELF)
.PHONY: compile
$(ELF): $(SRC) $(HDRS)
arduino-cli compile -b $(FQBN) $(VFLAG)
@if which arduino-manifest.pl; \
then echo "---> Generating manifest.txt"; \
arduino-manifest.pl -b $(FQBN) $(SRC) $(HDRS) > manifest.txt; \
else echo "---> If you want to generate manifest.txt, listing used libraries and their versions,"; \
echo "---> please install arduino-manifest, see https://github.com/digiampietro/arduino-manifest"; \
fi
upload:
@if [ ! -c $(SERIAL_DEV) ] ; \
then echo "---> ERROR: Serial Device not available, please set the SERIAL_DEV environment variable" ; \
else echo "---> Uploading sketch\n"; \
arduino-cli upload -b $(FQBN) -p $(SERIAL_DEV) $(VFLAG); \
fi
ota:
@PLAT_PATH=`arduino-cli compile -b $(FQBN) --show-properties | grep '^runtime.platform.path' | awk -F= '{print $$2}'` ; \
PY_PATH=`arduino-cli compile -b $(FQBN) --show-properties | grep '^runtime.tools.python3.path' | awk -F= '{print $$2}'` ; \
IOT_IP=`avahi-browse _arduino._tcp --resolve --parsable --terminate|grep -i ';$(IOT_NAME);'|grep ';$(OTA_PORT);'| awk -F\; '{print $$8}'|head -1`; \
BINFILE=$(wildcard $(BUILD_DIR)/$(SRC)*bin); \
echo "PLAT_PATH is [$$PLAT_PATH]" ; \
echo "PY_PATH: is [$$PY_PATH]" ; \
echo "IOT_IP: is [$$IOT_IP]" ; \
echo "BINFILE: is [$$BINFILE]" ; \
if [ "$$IOT_IP" = "" ] ; \
then echo "Unable to find device IP. Check that the IOT_NAME environment variable is correctly set. Use 'make find' to search devices"; \
else echo "---> Uploading Over The Air"; \
$$PY_PATH/python3 $$PLAT_PATH/tools/espota.py -i $$IOT_IP -p $(OTA_PORT) --auth=$(OTA_PASS) -f $$BINFILE ;\
fi
clean:
@echo "---> Cleaning the build directory"
rm -rf build
find:
avahi-browse _arduino._tcp --resolve --parsable --terminate
requirements:
@if [ -e requirements.txt ]; \
then while read -r i ; do echo ; \
echo "---> Installing " '"'$$i'"' ; \
arduino-cli lib install "$$i" ; \
done < requirements.txt ; \
else echo "---> MISSING requirements.txt file"; \
fi

15
src/arduino/simwind/simwind.h Executable file
View File

@ -0,0 +1,15 @@
#ifndef _SIMWINDDATA_H
#define _SIMWINDDATA_H
#include <stdint.h>
#include <stdbool.h>
typedef struct
{
uint32_t velocity;
float fanpower;
}
SimWindData;
#endif

View File

@ -1,7 +1,7 @@
#include <Adafruit_MotorShield.h> #include <Adafruit_MotorShield.h>
#include "simdata.h" #include "simwind.h"
#define BYTE_SIZE sizeof(SimData) #define BYTE_SIZE sizeof(SimWindData)
#define KPHTOMPH .621317 #define KPHTOMPH .621317
#define FANPOWER .6 #define FANPOWER .6
@ -10,7 +10,7 @@ Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *myMotor1 = AFMS.getMotor(1); Adafruit_DCMotor *myMotor1 = AFMS.getMotor(1);
Adafruit_DCMotor *myMotor2 = AFMS.getMotor(3); Adafruit_DCMotor *myMotor2 = AFMS.getMotor(3);
SimData sd; SimWindData sd;
int velocity = 0; int velocity = 0;
void setup() { void setup() {
@ -19,10 +19,6 @@ void setup() {
Serial.println("Could not find Motor Shield. Check wiring."); Serial.println("Could not find Motor Shield. Check wiring.");
while (1); while (1);
} }
sd.rpms = 0;
sd.maxrpm = 6500;
sd.altitude = 10;
sd.pulses = 40000;
sd.velocity = 10; sd.velocity = 10;
myMotor1->setSpeed(0); myMotor1->setSpeed(0);

View File

@ -6,19 +6,42 @@
#include "arduino.h" #include "arduino.h"
#include "../../slog/slog.h" #include "../../slog/slog.h"
#define arduino_timeout 2000 #define arduino_timeout 5000
int arduino_update(SerialDevice* serialdevice, SimData* simdata) int arduino_update(SerialDevice* serialdevice, void* data, size_t size)
{ {
int result = 1; int result = 1;
if (serialdevice->port) if (serialdevice->port)
{ {
result = check(sp_blocking_write(serialdevice->port, simdata, sizeof(SimData), arduino_timeout)); slogt("copying %i bytes to arduino device", size);
result = check(sp_blocking_write(serialdevice->port, data, size, arduino_timeout));
} }
return result; return result;
} }
//int arduino_shiftlights_update(SerialDevice* serialdevice, SimData* simdata)
//{
// int result = 1;
// if (serialdevice->port)
// {
// result = check(sp_blocking_write(serialdevice->port, simdata, sizeof(SimData), arduino_timeout));
// }
//
// return result;
//}
//
//int arduino_simwind_update(SerialDevice* serialdevice, SimData* simdata)
//{
// int result = 1;
// if (serialdevice->port)
// {
// result = check(sp_blocking_write(serialdevice->port, simdata, sizeof(SimData), arduino_timeout));
// }
//
// return result;
//}
int arduino_init(SerialDevice* serialdevice, const char* portdev) int arduino_init(SerialDevice* serialdevice, const char* portdev)
{ {
slogi("initializing arduino serial device..."); slogi("initializing arduino serial device...");
@ -80,3 +103,4 @@ int check(enum sp_return result)
return result; return result;
} }
} }

View File

@ -4,7 +4,8 @@
#include "../simdevice.h" #include "../simdevice.h"
#include "../serialdevice.h" #include "../serialdevice.h"
int arduino_update(SerialDevice* serialdevice, SimData* simdata);
int arduino_update(SerialDevice* serialdevice, void* data, size_t size);
int arduino_init(SerialDevice* serialdevice, const char* portdev); int arduino_init(SerialDevice* serialdevice, const char* portdev);
int arduino_free(SerialDevice* serialdevice); int arduino_free(SerialDevice* serialdevice);
int check(enum sp_return result); int check(enum sp_return result);

View File

@ -15,10 +15,45 @@ int serialdev_update(SimDevice* this, SimData* simdata)
{ {
SerialDevice* serialdevice = (void *) this->derived; SerialDevice* serialdevice = (void *) this->derived;
arduino_update(serialdevice, simdata);
arduino_update(serialdevice, simdata, sizeof(SimData));
return 0; return 0;
} }
int arduino_shiftlights_update(SimDevice* this, SimData* simdata)
{
SerialDevice* serialdevice = (void *) this->derived;
int result = 1;
serialdevice->u.shiftlightsdata.maxrpm = simdata->maxrpm;
serialdevice->u.shiftlightsdata.rpm = simdata->rpms;
slogt("Updating arduino device rpms to %i", serialdevice->u.shiftlightsdata.rpm);
// we can add configs to set all the colors
// i can move the size to the initialization since it should not change
size_t size = sizeof(ShiftLightsData);
arduino_update(serialdevice, &serialdevice->u.shiftlightsdata, size);
return result;
}
int arduino_simwind_update(SimDevice* this, SimData* simdata)
{
SerialDevice* serialdevice = (void *) this->derived;
int result = 1;
serialdevice->u.simwinddata.velocity = simdata->velocity;
slogt("Updating arduino device speed to %i", serialdevice->u.simwinddata.velocity);
// this can be added to the config, all config dependent can be added to init
serialdevice->u.simwinddata.fanpower = 0.6;
size_t size = sizeof(SimWindData);
arduino_update(serialdevice, &serialdevice->u.simwinddata, size);
return result;
}
int serialdev_free(SimDevice* this) int serialdev_free(SimDevice* this)
{ {
SerialDevice* serialdevice = (void *) this->derived; SerialDevice* serialdevice = (void *) this->derived;
@ -43,6 +78,8 @@ int serialdev_init(SerialDevice* serialdevice, const char* portdev)
} }
static const vtable serial_simdevice_vtable = { &serialdev_update, &serialdev_free }; static const vtable serial_simdevice_vtable = { &serialdev_update, &serialdev_free };
static const vtable arduino_shiftlights_vtable = { &arduino_shiftlights_update, &serialdev_free };
static const vtable arduino_simwind_vtable = { &arduino_simwind_update, &serialdev_free };
SerialDevice* new_serial_device(DeviceSettings* ds) { SerialDevice* new_serial_device(DeviceSettings* ds) {
@ -53,6 +90,20 @@ SerialDevice* new_serial_device(DeviceSettings* ds) {
this->m.derived = this; this->m.derived = this;
this->m.vtable = &serial_simdevice_vtable; this->m.vtable = &serial_simdevice_vtable;
slogt("Attempting to configure arduino device with subtype: %i", ds->dev_subtype);
switch (ds->dev_subtype) {
case (SIMDEVTYPE_SHIFTLIGHTS):
this->devicetype = ARDUINODEV__SHIFTLIGHTS;
this->m.vtable = &arduino_shiftlights_vtable;
slogi("Initializing arduino device for shiftlights.");
break;
case (SIMDEVTYPE_SIMWIND):
this->devicetype = ARDUINODEV__SIMWIND;
this->m.vtable = &arduino_simwind_vtable;
slogi("Initializing arduino devices for sim wind.");
break;
}
int error = serialdev_init(this, ds->serialdevsettings.portdev); int error = serialdev_init(this, ds->serialdevsettings.portdev);
if (error != 0) if (error != 0)

View File

@ -3,5 +3,11 @@
#include <libserialport.h> #include <libserialport.h>
typedef enum
{
ARDUINODEV__SHIFTLIGHTS = 0,
ARDUINODEV__SIMWIND = 1,
}
SerialDeviceType;
#endif #endif

View File

@ -78,6 +78,7 @@ int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds)
simdevices[j] = sim->m; simdevices[j] = sim->m;
simdevices[j].initialized = true; simdevices[j].initialized = true;
simdevices[j].type = SIMDEV_SOUND; simdevices[j].type = SIMDEV_SOUND;
simdevices[j].tyre = ds[j].tyre;
devices++; devices++;
} }
else else

View File

@ -9,6 +9,8 @@
#include "../helper/confighelper.h" #include "../helper/confighelper.h"
#include "../simulatorapi/simapi/simapi/simdata.h" #include "../simulatorapi/simapi/simapi/simdata.h"
#include "../../arduino/simwind/simwind.h"
#include "../../arduino/shiftlights/shiftlights.h"
typedef struct SimDevice SimDevice; typedef struct SimDevice SimDevice;
@ -21,7 +23,7 @@ struct SimDevice
int id; int id;
bool initialized; bool initialized;
DeviceType type; DeviceType type;
MonocoqueTyreIdentifier tyre;
}; };
typedef struct { typedef struct {
@ -43,10 +45,17 @@ typedef struct
int id; int id;
SerialType type; SerialType type;
struct sp_port* port; struct sp_port* port;
SerialDeviceType devicetype;
union
{
SimWindData simwinddata;
ShiftLightsData shiftlightsdata;
} u;
} }
SerialDevice; SerialDevice;
int serialdev_update(SimDevice* this, SimData* simdata); int arduino_shiftlights_update(SimDevice* this, SimData* simdata);
int arduino_simwind_update(SimDevice* this, SimData* simdata);
int serialdev_free(SimDevice* this); int serialdev_free(SimDevice* this);
SerialDevice* new_serial_device(DeviceSettings* ds); SerialDevice* new_serial_device(DeviceSettings* ds);
@ -96,6 +105,7 @@ SoundDevice;
int sounddev_engine_update(SimDevice* this, SimData* simdata); int sounddev_engine_update(SimDevice* this, SimData* simdata);
int sounddev_gearshift_update(SimDevice* this, SimData* simdata); int sounddev_gearshift_update(SimDevice* this, SimData* simdata);
int sounddev_tyreslip_update(SimDevice* this, SimData* simdata);
int sounddev_free(SimDevice* this); int sounddev_free(SimDevice* this);
SoundDevice* new_sound_device(DeviceSettings* ds); SoundDevice* new_sound_device(DeviceSettings* ds);

View File

@ -38,6 +38,55 @@ int sounddev_engine_update(SimDevice* this, SimData* simdata)
slogt("set engine frequency to %i", sounddevice->sounddata.frequency); slogt("set engine frequency to %i", sounddevice->sounddata.frequency);
} }
int sounddev_tyreslip_update(SimDevice* this, SimData* simdata)
{
SoundDevice* sounddevice = (void *) this->derived;
double play = 0;
if (this->tyre == FRONTLEFT || this->tyre == FRONTS || this->tyre == ALLFOUR)
{
play += simdata->wheelslip[0];
}
if (this->tyre == FRONTRIGHT || this->tyre == FRONTS || this->tyre == ALLFOUR)
{
play += simdata->wheelslip[1];
}
if (this->tyre == REARLEFT || this->tyre == REARS || this->tyre == ALLFOUR)
{
play += simdata->wheelslip[2];
}
if (this->tyre == REARRIGHT || this->tyre == REARS || this->tyre == ALLFOUR)
{
play += simdata->wheelslip[3];
}
if (play > 0)
{
sounddevice->sounddata.curr_frequency = sounddevice->sounddata.frequency * play;
sounddevice->sounddata.curr_duration = sounddevice->sounddata.duration;
}
else
{
sounddevice->sounddata.curr_frequency = 0;
sounddevice->sounddata.curr_duration = 0;
}
}
int sounddev_absbrakes_update(SimDevice* this, SimData* simdata)
{
SoundDevice* sounddevice = (void *) this->derived;
if (simdata->abs > 0)
{
sounddevice->sounddata.curr_frequency = sounddevice->sounddata.frequency;
sounddevice->sounddata.curr_duration = sounddevice->sounddata.duration;
}
else
{
sounddevice->sounddata.curr_frequency = 0;
sounddevice->sounddata.curr_duration = 0;
}
}
int sounddev_gearshift_update(SimDevice* this, SimData* simdata) int sounddev_gearshift_update(SimDevice* this, SimData* simdata)
{ {
SoundDevice* sounddevice = (void *) this->derived; SoundDevice* sounddevice = (void *) this->derived;
@ -90,6 +139,16 @@ int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int
sounddevice->sounddata.curr_duration = duration; sounddevice->sounddata.curr_duration = duration;
streamname = "Gear"; streamname = "Gear";
break; break;
case (SOUNDEFFECT_TYRESLIP):
sounddevice->sounddata.duration = duration;
sounddevice->sounddata.curr_duration = duration;
streamname = "TyreSlip";
break;
case (SOUNDEFFECT_ABSBRAKES):
sounddevice->sounddata.duration = duration;
sounddevice->sounddata.curr_duration = duration;
streamname = "ABS";
break;
} }
#ifdef USE_PULSEAUDIO #ifdef USE_PULSEAUDIO
@ -105,6 +164,8 @@ int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int
static const vtable engine_sound_simdevice_vtable = { &sounddev_engine_update, &sounddev_free }; static const vtable engine_sound_simdevice_vtable = { &sounddev_engine_update, &sounddev_free };
static const vtable gear_sound_simdevice_vtable = { &sounddev_gearshift_update, &sounddev_free }; static const vtable gear_sound_simdevice_vtable = { &sounddev_gearshift_update, &sounddev_free };
static const vtable tyreslip_sound_simdevice_vtable = { &sounddev_tyreslip_update, &sounddev_free };
static const vtable absbrakes_sound_simdevice_vtable = { &sounddev_absbrakes_update, &sounddev_free };
SoundDevice* new_sound_device(DeviceSettings* ds) { SoundDevice* new_sound_device(DeviceSettings* ds) {
@ -126,6 +187,16 @@ SoundDevice* new_sound_device(DeviceSettings* ds) {
this->m.vtable = &gear_sound_simdevice_vtable; this->m.vtable = &gear_sound_simdevice_vtable;
slogi("Initializing sound device for gear shift vibrations."); slogi("Initializing sound device for gear shift vibrations.");
break; break;
case (SIMDEVTYPE_TYRESLIP):
this->effecttype = SOUNDEFFECT_TYRESLIP;
this->m.vtable = &tyreslip_sound_simdevice_vtable;
slogi("Initializing sound device for tyre slip vibrations.");
break;
case (SIMDEVTYPE_ABSBRAKES):
this->effecttype = SOUNDEFFECT_ABSBRAKES;
this->m.vtable = &absbrakes_sound_simdevice_vtable;
slogi("Initializing sound device for abs vibrations.");
break;
} }
slogt("Attempting to use device %s", ds->sounddevsettings.dev); slogt("Attempting to use device %s", ds->sounddevsettings.dev);

View File

@ -17,7 +17,9 @@ SoundType;
typedef enum typedef enum
{ {
SOUNDEFFECT_ENGINERPM = 0, SOUNDEFFECT_ENGINERPM = 0,
SOUNDEFFECT_GEARSHIFT = 1 SOUNDEFFECT_GEARSHIFT = 1,
SOUNDEFFECT_ABSBRAKES = 2,
SOUNDEFFECT_TYRESLIP = 3
} }
VibrationEffectType; VibrationEffectType;

View File

@ -1,4 +1,3 @@
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -6,31 +5,16 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <poll.h> #include <poll.h>
#include <termios.h> #include <termios.h>
#include <signal.h>
#include "gameloop.h" #include "gameloop.h"
#include "../helper/parameters.h" #include "../helper/parameters.h"
#include "../helper/confighelper.h" #include "../helper/confighelper.h"
#include "../devices/simdevice.h" #include "../devices/simdevice.h"
#include "../simulatorapi/simapi/simapi/simdata.h" #include "../simulatorapi/simdata.h"
#include "../simulatorapi/simapi/simapi/simmapper.h" #include "../simulatorapi/simmapper.h"
#include "../slog/slog.h" #include "../slog/slog.h"
#define DEFAULT_UPDATE_RATE 240.0 #define DEFAULT_UPDATE_RATE 120.0
#define SIM_CHECK_RATE 1.0
bool go = false;
bool go2 = false;
struct sigaction act;
void sighandler(int signum, siginfo_t* info, void* ptr)
{
sloge("caught signal");
go = false;
go2 = false;
//gfx_clear(pixels, pixels_len);
//gfx_swapbuffers();
//gfx_close();
}
int showstats(SimData* simdata) int showstats(SimData* simdata)
{ {
@ -181,64 +165,20 @@ int showstats(SimData* simdata)
fflush(stdout); fflush(stdout);
} }
int looper(SimDevice* devices[], int numdevices, Simulator simulator)
int clilooper(SimDevice* devices, int numdevices, Parameters* p, SimData* simdata, SimMap* simmap)
{ {
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
double update_rate = DEFAULT_UPDATE_RATE;
char ch;
int t=0;
int s=0;
go2 = true;
while (go2 == true && simdata->simstatus > 1)
{
simdatamap(simdata, simmap, p->sim);
showstats(simdata);
t++;
s++;
if(simdata->rpms<100)
{
simdata->rpms=100;
}
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
if( poll(&mypoll, 1, 1000.0/update_rate) )
{
scanf("%c", &ch);
if(ch == 'q')
{
go2 = false;
}
}
}
simdata->velocity = 0;
simdata->rpms = 100;
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
fprintf(stdout, "\n");
return 0;
}
int looper(SimDevice* devices, int numdevices, Parameters* p)
{
memset(&act, 0, sizeof(act));
act.sa_sigaction = sighandler;
act.sa_flags = SA_SIGINFO;
sigaction(SIGTERM, &act, NULL);
sigaction(SIGINT, &act, NULL);
sigaction(SIGTSTP, &act, NULL);
slogi("preparing game loop with %i devices...", numdevices);
SimData* simdata = malloc(sizeof(SimData)); SimData* simdata = malloc(sizeof(SimData));
SimMap* simmap = malloc(sizeof(SimMap)); SimMap* simmap = malloc(sizeof(SimMap));
int error = siminit(simdata, simmap, simulator);
if (error != MONOCOQUE_ERROR_NONE)
{
return error;
}
struct termios newsettings, canonicalmode; struct termios newsettings, canonicalmode;
tcgetattr(0, &canonicalmode); tcgetattr(0, &canonicalmode);
newsettings = canonicalmode; newsettings = canonicalmode;
@ -249,41 +189,38 @@ int looper(SimDevice* devices, int numdevices, Parameters* p)
char ch; char ch;
struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI }; struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI };
fprintf(stdout, "Searching for sim data... Press q to quit...\n"); double update_rate = DEFAULT_UPDATE_RATE;
p->simon = false; int t=0;
double update_rate = SIM_CHECK_RATE; int go = true;
go = true;
while (go == true) while (go == true)
{ {
p->simon = false; simdatamap(simdata, simmap, simulator);
getSim(simdata, simmap, &p->simon, &p->sim); showstats(simdata);
t++;
if (p->simon == true && simdata->simstatus > 1) if(simdata->rpms<250)
{ {
slogi("preparing game loop with %i devices...", numdevices); simdata->rpms=250;
}
slogi("sending initial data to devices");
simdata->velocity = 16;
simdata->rpms = 100;
for (int x = 0; x < numdevices; x++) for (int x = 0; x < numdevices; x++)
{ {
devices[x].update(&devices[x], simdata); if (devices[x]->type == SIMDEV_SERIAL)
}
sleep(3);
clilooper(devices, numdevices, p, simdata, simmap);
}
if (p->simon == true)
{ {
p->simon = false; if(t>=update_rate)
fprintf(stdout, "Searching for sim data... Press q again to quit...\n"); {
sleep(2); devupdate(devices[x], simdata);
}
}
else
{
devupdate(devices[x], simdata);
} }
if (poll(&mypoll, 1, 1000.0/update_rate) ) }
if(t>=update_rate)
{ {
if (go != false ) t=0;
}
if( poll(&mypoll, 1, 1000.0/update_rate) )
{ {
scanf("%c", &ch); scanf("%c", &ch);
if(ch == 'q') if(ch == 'q')
@ -292,8 +229,6 @@ int looper(SimDevice* devices, int numdevices, Parameters* p)
} }
} }
} }
}
fprintf(stdout, "\n"); fprintf(stdout, "\n");
fflush(stdout); fflush(stdout);
tcsetattr(0, TCSANOW, &canonicalmode); tcsetattr(0, TCSANOW, &canonicalmode);
@ -303,144 +238,3 @@ int looper(SimDevice* devices, int numdevices, Parameters* p)
return 0; return 0;
} }
int tester(SimDevice* devices, int numdevices)
{
slogi("preparing test with %i devices...", numdevices);
SimData* simdata = malloc(sizeof(SimData));
struct termios newsettings, canonicalmode;
tcgetattr(0, &canonicalmode);
newsettings = canonicalmode;
newsettings.c_lflag &= (~ICANON & ~ECHO);
newsettings.c_cc[VMIN] = 1;
newsettings.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &newsettings);
fprintf(stdout, "\n");
simdata->gear = 0;
simdata->velocity = 16;
simdata->rpms = 100;
simdata->maxrpm = 8000;
sleep(3);
fprintf(stdout, "Setting rpms to 1000\n");
simdata->rpms = 1000;
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
sleep(3);
fprintf(stdout, "Shifting into first gear\n");
simdata->gear = 2;
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
sleep(3);
fprintf(stdout, "Setting speed to 100\n");
simdata->velocity = 100;
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
sleep(3);
fprintf(stdout, "Shifting into second gear\n");
simdata->gear = 3;
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
sleep(3);
fprintf(stdout, "Setting speed to 200\n");
simdata->velocity = 200;
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
sleep(3);
fprintf(stdout, "Shifting into third gear\n");
simdata->gear = 4;
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
sleep(3);
fprintf(stdout, "Setting rpms to 2000\n");
simdata->rpms = 2000;
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
sleep(3);
fprintf(stdout, "Setting rpms to 4000\n");
simdata->rpms = 4000;
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
sleep(3);
fprintf(stdout, "Shifting into fourth gear\n");
simdata->gear = 5;
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
sleep(3);
fprintf(stdout, "Setting speed to 300\n");
simdata->velocity = 300;
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
sleep(3);
fprintf(stdout, "Setting rpms to 8000\n");
simdata->rpms = 8000;
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
sleep(3);
simdata->velocity = 0;
simdata->rpms = 100;
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
sleep(1);
fflush(stdout);
tcsetattr(0, TCSANOW, &canonicalmode);
free(simdata);
return 0;
}

View File

@ -91,6 +91,11 @@ int strtodevsubtype(const char* device_subtype, DeviceSettings* ds, int simdev)
ds->dev_subtype = SIMDEVTYPE_ABSBRAKES; ds->dev_subtype = SIMDEVTYPE_ABSBRAKES;
break; break;
} }
if ((strcicmp(device_subtype, "SLIP") == 0) || (strcicmp(device_subtype, "TYRESLIP") == 0))
{
ds->dev_subtype = SIMDEVTYPE_TYRESLIP;
break;
}
default: default:
ds->is_valid = false; ds->is_valid = false;
slogw("%s does not appear to be a valid device sub type, but attempting to continue with other devices", device_subtype); slogw("%s does not appear to be a valid device sub type, but attempting to continue with other devices", device_subtype);
@ -274,6 +279,8 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co
config_setting_lookup_int(device_settings, "pan", &ds->sounddevsettings.pan); config_setting_lookup_int(device_settings, "pan", &ds->sounddevsettings.pan);
config_setting_lookup_float(device_settings, "duration", &ds->sounddevsettings.duration); config_setting_lookup_float(device_settings, "duration", &ds->sounddevsettings.duration);
const char* temp; const char* temp;
int found = 0; int found = 0;
found = config_setting_lookup_string(device_settings, "devid", &temp); found = config_setting_lookup_string(device_settings, "devid", &temp);
@ -285,6 +292,38 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co
{ {
ds->sounddevsettings.dev = strdup(temp); ds->sounddevsettings.dev = strdup(temp);
} }
if (ds->dev_subtype == SIMDEVTYPE_TYRESLIP)
{
found = config_setting_lookup_string(device_settings, "tyre", &temp);
ds->tyre = ALLFOUR;
if (strcicmp(temp, "FRONTS") == 0)
{
ds->tyre = FRONTS;
}
if (strcicmp(temp, "REARS") == 0)
{
ds->tyre = REARS;
}
if (strcicmp(temp, "FRONTLEFT") == 0)
{
ds->tyre = FRONTLEFT;
}
if (strcicmp(temp, "FRONTRIGHT") == 0)
{
ds->tyre = FRONTRIGHT;
}
if (strcicmp(temp, "REARLEFT") == 0)
{
ds->tyre = REARLEFT;
}
if (strcicmp(temp, "REARRIGHT") == 0)
{
ds->tyre = REARRIGHT;
}
}
} }
} }

View File

@ -1,6 +1,7 @@
#ifndef _CONFIGHELPER_H #ifndef _CONFIGHELPER_H
#define _CONFIGHELPER_H #define _CONFIGHELPER_H
#include <pulse/channelmap.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
@ -25,7 +26,8 @@ typedef enum
SIMDEVTYPE_SIMWIND = 3, SIMDEVTYPE_SIMWIND = 3,
SIMDEVTYPE_ENGINESOUND = 4, SIMDEVTYPE_ENGINESOUND = 4,
SIMDEVTYPE_GEARSOUND = 5, SIMDEVTYPE_GEARSOUND = 5,
SIMDEVTYPE_ABSBRAKES = 6 SIMDEVTYPE_ABSBRAKES = 6,
SIMDEVTYPE_TYRESLIP = 7
} }
DeviceSubType; DeviceSubType;
@ -52,6 +54,18 @@ typedef enum
} }
MonocoqueError; MonocoqueError;
typedef enum
{
FRONTLEFT = 0,
FRONTRIGHT = 1,
REARLEFT = 2,
REARRIGHT = 3,
FRONTS = 4,
REARS = 5,
ALLFOUR = 6
}
MonocoqueTyreIdentifier;
typedef struct typedef struct
{ {
ProgramAction program_action; ProgramAction program_action;
@ -95,6 +109,7 @@ typedef struct
TachometerSettings tachsettings; TachometerSettings tachsettings;
SerialDeviceSettings serialdevsettings; SerialDeviceSettings serialdevsettings;
SoundDeviceSettings sounddevsettings; SoundDeviceSettings sounddevsettings;
MonocoqueTyreIdentifier tyre;
} }
DeviceSettings; DeviceSettings;

@ -1 +1 @@
Subproject commit f8c4a167dda04b12c6ac5fd0fab22d490b588b46 Subproject commit 231e64e1190686dad77fcaf2e9e5e9bc8d831274