Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitrit committed Jan 3, 2017
0 parents commit 1f24548
Show file tree
Hide file tree
Showing 17 changed files with 5,566 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# lego-railway
21 changes: 21 additions & 0 deletions controllers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 2.8.4)
set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/cmake/ArduinoToolchain.cmake)
set(PROJECT_NAME lego_railway)
project(${PROJECT_NAME})

set(ARDUINO_SKETCH_FOLDER ~/Documents/Arduino)
link_directories(${ARDUINO_SKETCH_FOLDER}/libraries)

link_directories(~/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/libraries/)
link_directories(~/Applications/Arduino.app/Contents/Java/hardware/arduino/avr/cores/arduino/)

set(Servo_RECURSE True)
set(PowerFunctions_RECURSE True)

set(ARDUINO_DEFAULT_BOARD nano328b)
set(ARDUINO_DEFAULT_PORT /dev/cu.wchusbserial1420)

set(${CMAKE_PROJECT_NAME}_SKETCH lego_railway.ino)

generate_arduino_firmware(${CMAKE_PROJECT_NAME})
target_link_libraries(${CMAKE_PROJECT_NAME} ${ARDUINO_DEFAULT_BOARD}_SignalController ${ARDUINO_DEFAULT_BOARD}_DispatchQueue ${ARDUINO_DEFAULT_BOARD}_Points ${ARDUINO_DEFAULT_BOARD}_Servo ${ARDUINO_DEFAULT_BOARD}_TrainController ${ARDUINO_DEFAULT_BOARD}_PowerFunctions)
40 changes: 40 additions & 0 deletions controllers/DispatchQueue/DispatchQueue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2016 Plastic Objects Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "DispatchQueue.h"

DispatchQueue::DispatchQueue(): _head(0), _tail(0), _count(0) { }

bool DispatchQueue::queue(uint8_t loop, tag_t tag) {
if (_count == QUEUE_MAX_LENGTH) {
return false;
}

_queue[_head].loop = loop;
memcpy(_queue[_head].tag.id, tag.id, sizeof(struct tag_t));

_head = (_head + 1) % QUEUE_MAX_LENGTH;
_count++;

return true;
}

void DispatchQueue::dispatch(bool(*dispatch_handler)(request_t request)) {
if (_count > 0 && dispatch_handler(_queue[_tail])) {
_tail = (_tail + 1) % QUEUE_MAX_LENGTH;
_count--;
}
}
45 changes: 45 additions & 0 deletions controllers/DispatchQueue/DispatchQueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2016 Plastic Objects Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef LEGO_RAILWAY_DISPATCH_H
#define LEGO_RAILWAY_DISPATCH_H

#include <Arduino.h>
#include "TrainController.h"

#define QUEUE_MAX_LENGTH 6

struct request_t {
uint8_t loop;
struct tag_t tag;
};

class DispatchQueue {
public:
DispatchQueue();

bool queue(uint8_t loop, tag_t tag);

void dispatch(bool(*dispatch_handler)(request_t));

private:
struct request_t _queue[QUEUE_MAX_LENGTH];
uint8_t _head = 0;
uint8_t _tail = 0;
uint8_t _count = 0;
};

#endif //LEGO_RAILWAY_DISPATCH_H
44 changes: 44 additions & 0 deletions controllers/Points/Points.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2016 Plastic Objects Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "Points.h"

Points::Points(uint8_t pin, uint8_t power, Direction direction) : _servo(), _power(power), _direction(direction), _position(NORMAL) {
_servo.attach(pin);

pinMode(power, OUTPUT);
digitalWrite(_power, HIGH);

delay(30);

set(_position);
}

void Points::set(Position position) {
digitalWrite(_power, HIGH);

if (_direction == RIGHT) {
_servo.write(position);
} else {
_servo.write(_SERVO_TRAVEL - position);
}

digitalWrite(_power, LOW);
}

Position Points::get() {
return _position;
}
51 changes: 51 additions & 0 deletions controllers/Points/Points.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2016 Plastic Objects Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef LEGO_RAILWAY_POINTS_H
#define LEGO_RAILWAY_POINTS_H

#include <Arduino.h>
#include <Servo.h>

#define _SERVO_TRAVEL 30

enum Direction {
LEFT,
RIGHT
};

enum Position {
NORMAL = 0, // ie. straight ahead
REVERSE = _SERVO_TRAVEL // ie. turn
};

class Points {

public:
Points(uint8_t pin, uint8_t power, Direction direction);

void set(Position position);

Position get();

private:
uint8_t _power;
Servo _servo;
Direction _direction;
Position _position;
};

#endif //LEGO_RAILWAY_POINTS_H
55 changes: 55 additions & 0 deletions controllers/SignalController/SignalController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2016 Plastic Objects Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "SignalController.h"

SignalController::SignalController(uint8_t dataPin, uint8_t clockPin, uint8_t latchPin) : _data(dataPin), _clock(clockPin), _latch(latchPin) {
pinMode(_data, OUTPUT);
pinMode(_clock, OUTPUT);
pinMode(_latch, OUTPUT);

danger();
}

void SignalController::set(uint8_t index, uint8_t signal) {
_positions = (~(3 << (index * 2)) & _positions) | signal << (index * 2);

update();

if (signal == SIGNAL_CLEAR) {
_dangerAt = millis() + 3000;
}
}

void SignalController::poll() {
if (_dangerAt > 0 && millis() > _dangerAt) {
danger();
}
}

void SignalController::danger() {
// set all signals at danger
_positions = 0b10101010;
_dangerAt = 0;
update();
}

void SignalController::update() {

digitalWrite(_latch, LOW);
shiftOut(_data, _clock, MSBFIRST, _positions);
digitalWrite(_latch, HIGH);
}
44 changes: 44 additions & 0 deletions controllers/SignalController/SignalController.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2016 Plastic Objects Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef LEGO_RAILWAY_SIGNALCONTROLLER_H
#define LEGO_RAILWAY_SIGNALCONTROLLER_H

#include <Arduino.h>

static const uint8_t SIGNAL_CLEAR = 0x1;
static const uint8_t SIGNAL_STOP = 0x2;

class SignalController {
public:
SignalController(uint8_t dataPin, uint8_t clockPin, uint8_t latchPin);

void set(uint8_t index, uint8_t signal);
void poll();

private:
void danger();
void update();

uint8_t _data;
uint8_t _clock;
uint8_t _latch;
uint8_t _positions;
unsigned long _dangerAt;
};


#endif //LEGO_RAILWAY_SIGNALCONTROLLER_H
67 changes: 67 additions & 0 deletions controllers/TrainController/TrainController.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2016 Plastic Objects Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "TrainController.h"

TrainController::TrainController(uint8_t pin, uint8_t channel) : _pf(pin, channel), _train(0) { }

void TrainController::add(Train *train) {
Train *ptr = _train;

train->next = 0;

if (ptr == 0) {
_train = train;
} else {
while (ptr->next != 0) {
ptr = ptr->next;
}
ptr->next = train;
}
}

void TrainController::set_speed(struct tag_t tag, uint8_t speed) {
int n;
Train *ptr = _train;

if (ptr == 0) {
return;
}

while (ptr->next != 0) {
n = memcmp(ptr->tag.id, tag.id, sizeof(struct tag_t));
if (n == 0) {
break;
}
ptr = ptr->next;
}
n = memcmp(ptr->tag.id, tag.id, sizeof(struct tag_t));
if (n == 0) {
single_pwm(ptr->output, speed);
}
}

void TrainController::single_pwm(uint8_t output, uint8_t speed) {
_pf.single_pwm(output, speed);
if (speed == PWM_BRK) {
delay(30);
_pf.single_pwm(output, PWM_FLT);
}
}

train_t::train_t(uint8_t driver, byte *tag_id) : output(driver) {
memcpy(tag.id, tag_id, sizeof(struct tag_t));
}
Loading

0 comments on commit 1f24548

Please sign in to comment.