Skip to content

Commit

Permalink
Merge pull request #2 from xtruan/develop
Browse files Browse the repository at this point in the history
Merge to master
  • Loading branch information
xtruan authored Aug 14, 2018
2 parents 0a4bdd7 + 318eafb commit 4696f2b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 10 deletions.
51 changes: 43 additions & 8 deletions arduino/actuator/actuator.ino
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <MeccaBrain.h>
#include <Servo.h>
#include <Wire.h>

#ifndef cbi
Expand All @@ -11,11 +12,23 @@ const int I2C_ADDR = 0x8;
// configure pin addresses
const int LED_PIN = LED_BUILTIN;
const int MECC_LED_PIN = 3;
const int SERVO_PIN_0 = 5;
const int SERVO_PIN_1 = 6;
const int SERVO_PIN_2 = 9;
const int SERVO_PIN_3 = 10;
const int SERVO_PIN_4 = 11;

// initialize MeccaBrain
MeccaBrain meccLedChain(MECC_LED_PIN);
const int MECC_COMM_LOOPS = 20;

// initialize Servo
Servo servo0;
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
Expand All @@ -25,6 +38,13 @@ void setup() {
// initialize Meccano LED pin as an output
pinMode(MECC_LED_PIN, OUTPUT);

// initialize Servos
servo0.attach(SERVO_PIN_0);
servo1.attach(SERVO_PIN_1);
servo2.attach(SERVO_PIN_2);
servo3.attach(SERVO_PIN_3);
servo4.attach(SERVO_PIN_4);

Wire.begin(I2C_ADDR);

#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__) || defined(__AVR_ATmega328P__)
Expand Down Expand Up @@ -57,6 +77,15 @@ void loop() {
// NOP
}

// action received for test (internal Ardino) LED
void actionTestLed(char setting) {
if (setting == 0) {
digitalWrite(LED_PIN, 0);
} else {
digitalWrite(LED_PIN, 1);
}
}

// action received for Meccano LED
void actionMeccLed(int setting) {
byte red, green, blue;
Expand Down Expand Up @@ -96,13 +125,19 @@ void setMeccLedColor(byte red, byte green, byte blue, byte fadeTime)
}
}

// action received for test (internal Ardino) LED
void actionTestLed(char setting) {
if (setting == 0) {
digitalWrite(LED_PIN, 0);
} else {
digitalWrite(LED_PIN, 1);
}
// action received for servo
void actionServo(char id, char setting) {
if (id == 0) {
servo0.write(setting * 2);
} else if (id == 1) {
servo1.write(setting * 2);
} else if (id == 2) {
servo2.write(setting * 2);
} else if (id == 3) {
servo3.write(setting * 2);
} else if (id == 4) {
servo4.write(setting * 2);
}
}

// process received event
Expand All @@ -112,7 +147,7 @@ void processEvent(char type, char id, char setting) {
} else if (type == 'M') {
actionMeccLed(setting);
} else if (type == 'S') {
// NOP
actionServo(id, setting);
}
}

Expand Down
43 changes: 43 additions & 0 deletions raspi/action_recorder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from pynput.keyboard import Key, Listener
import time

import utils.transform as transform

csv_file = open('rec.csv', 'w')
start = time.time()
key_mode = 0

def on_press(key):
global csv_file, start, key_mode
if key_mode <= 0:
key_mode = 1

end = time.time()
out_str = transform.time_stringify(end - start) + ',SVO,2,90'
print(out_str)
csv_file.write(out_str + '\n')

def on_release(key):
global csv_file, start, key_mode
if key_mode >= 0:
key_mode = -1

end = time.time()
out_str = transform.time_stringify(end - start) + ',SVO,2,00'
print(out_str)
csv_file.write(out_str + '\n')

if key == Key.esc:
# Stop listener
return False
if key == Key.backspace:
print('--RESET--')
csv_file.close()
csv_file = open('rec.csv', 'w')
start = time.time()

# Collect events until released
with Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
2 changes: 1 addition & 1 deletion raspi/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def read_anim_csv(filename, delimiter=',', quotechar='"'):

for row in csv_reader:
time_str = str(row[COL_TIME_STR])
time_sec = transform.time_convert(time_str)
time_sec = transform.time_floatify(time_str)
device_type = str(row[COL_DEVICE_TYPE])
device_id = int(row[COL_DEVICE_ID])
setting = int(row[COL_SETTING])
Expand Down
9 changes: 8 additions & 1 deletion raspi/utils/transform.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import time

def time_convert(time_str):
def time_floatify(time_str):
time_list = time_str.split(':')
return float(int(time_list[0]) * 3600 + int(time_list[1]) * 60 + float(time_list[2]))

def time_stringify(time_secs):
frac = float(time_secs) - int(time_secs)
frac = int(frac * 1000)
return time.strftime('%H:%M:%S', time.gmtime(time_secs)) + '.' + str(frac)

0 comments on commit 4696f2b

Please sign in to comment.