Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikrobitti-rakentelija/91e017a249f6d4546bb17291f9f3c881 to your computer and use it in GitHub Desktop.
Save mikrobitti-rakentelija/91e017a249f6d4546bb17291f9f3c881 to your computer and use it in GitHub Desktop.
Parkkikiekko - Testikoodit
/*
* Kiihtyvyysmittarin testauskoodi. Koodi mittaa
* kiihtyvyyttä kolmella suunnalla (xyz) ja tulostaa
* mittaukset sarjaporttiin tab-eroteltuna, sekä
* ilmaisee kokonaiskiihtyvyyttä ledirenkaalla.
*
* Sarjaporttiviestintä mahdollistaa tietojen tallennuksen
* tiedostoon. Alkuvaiheessa tallensimme mittaukset tab-eroteltuna
* tiedostona putty:llä ja avasimme Excelillä.
*
* Myöhemmin käytimme pientä Python-skriptiä InfluxDB
* aikasarjatietokantaan tallennusta varten.
*/
#include <FastLED.h>
#define NUM_LEDS 24
#define LIGHT_WIDTH 5
#define DATA_PIN 2
#define BRIGHTNESS 8
CRGB leds[NUM_LEDS];
const int xpin = A0;
const int ypin = A1;
const int zpin = A2;
const int led_driving = 5;
const int led_engine = 4;
const int button_driving = 6;
const int button_engine = 7;
int sampleDelay = 10;
float zero_G = 512.0;
float scale = 102.3;
float forceX = 0;
float forceY = 0;
float forceZ = 0;
float total = 0;
int numberOfLightLeds = 0;
int driving = 0;
int engine_on = 0;
void setup()
{
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness( BRIGHTNESS );
Serial.begin(57600);
pinMode(xpin, INPUT);
pinMode(ypin, INPUT);
pinMode(zpin, INPUT);
pinMode(button_driving, INPUT);
pinMode(button_engine, INPUT);
pinMode(led_driving, OUTPUT);
pinMode(led_engine, OUTPUT);
}
int prev_driving = 0;
int prev_engine = 0;
void loop()
{
int x = analogRead(xpin);
int y = analogRead(ypin);
int z = analogRead(zpin);
int curr_engine = digitalRead(button_engine);
int curr_driving = digitalRead(button_driving);
if (curr_engine == 1 && curr_engine != prev_engine) {
engine_on = !engine_on;
if (engine_on)
digitalWrite(led_engine, HIGH);
else
digitalWrite(led_engine, LOW);
}
prev_engine = curr_engine;
if (curr_driving == 1 && curr_driving != prev_driving) {
driving = !driving;
if (driving)
digitalWrite(led_driving, HIGH);
else
digitalWrite(led_driving, LOW);
}
prev_driving = curr_driving;
Serial.print(x);
Serial.print("\t");
Serial.print(y);
Serial.print("\t");
Serial.print(z);
forceX = ((float)x - zero_G)/scale;
forceY = ((float)y - zero_G)/scale;
forceZ = ((float)z - zero_G)/scale;
total = sqrt(forceX * forceX + forceY * forceY + forceZ * forceZ);
Serial.print("\t");
Serial.print(forceX);
Serial.print("\t");
Serial.print(forceY);
Serial.print("\t");
Serial.print(forceZ);
Serial.print("\t");
Serial.print(driving);
Serial.print("\t");
Serial.print(engine_on);
Serial.println("\t");
numberOfLightLeds = int(NUM_LEDS * total / 2 - NUM_LEDS / 2);
fadeToBlackBy(leds, NUM_LEDS, 3);
for (int i = 0; i < numberOfLightLeds; i++)
{
leds[i] = CRGB::Orange;
}
FastLED.show();
delay(sampleDelay);
}
/*
* Mikrobitin parkkikiekko. Ledirenkaan kokeilemiseen
* tarkoitettu testikoodi pyörittää ledeissä sinistä
* "poliisivaloa*
*/
#include <FastLED.h>
#define NUM_LEDS 24
#define DATA_PIN 6
CRGB leds[NUM_LEDS];
uint8_t position = 0;
void setup() {
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
}
void loop() {
fadeToBlackBy(leds, NUM_LEDS, 64);
position++;
position = position % NUM_LEDS;
leds[position] = CRGB::Blue;
FastLED.show();
delay(10);
}
# Python-kripti sarjaporttimittausten kirjoittamiseen
# InfluxDB-tietokantaan. Koodin on esimerkkinä
# sarjaporttiliikenteen lukemisesta koneellisesti. Tämä
# koodi vaatii InfluxDB-tietokannan käytön, mutta vastaavalla
# tavalla tiedot voisi kirjoittaa myös tiedostoon.
#
#
# Käyttöohje
# - Asenna pip-työkalulla kirjastot pyserial ja requests
# - Tarkista sarjaportin ja tietokannan asetukset
# - Aja komennolla python serial_influxdb_bridge.py
#
import serial
import requests
import time
measurement_names = ["x","y", "z","Gx","Gy","Gz","total"]
ser = serial.Serial('COM3', 57600)
ser.flushInput()
ser.flushOutput()
lines = []
while True:
data_raw = ser.readline()
splitted = data_raw.split("\t")
if len(splitted) == 8:
values = []
for i in range(0, 8):
values.append("{}={}".format(measurement_names[i],splitted[i]))
joined_values = ",".join(values)
timestamp = int(time.time() * 1000000000)
line = "accelerometer,unit=1 {} {}".format(joined_values, timestamp)
lines.append(line)
if len(lines) > 100:
print("sending")
payload = "\n".join(lines)
lines = []
res = requests.post("http://localhost:8086/write?db=accelerometer", data=payload)
print(res.status_code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment