Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[Thanksgiving] New test AVR program

  • Loading branch information...
commit f970207c814b3545b832b4b4faf75f7bcefdb06e 1 parent 1e26093
@garbear authored
View
2  avr/src/FSMVector.h
@@ -85,7 +85,7 @@ class FSMVector
void QuickErase(const FiniteStateMachine &fsm) { QuickErase(GetIndex(fsm)); }
/**
- * Clear the array. Each FSM is deleted and their destructor is called.
+ * Clear the array. Each FSM is deleted and their deconstructor is called.
*/
void Clear() { while (m_size) PopBack(); }
View
11 avr_controller/CMakeLists.txt
@@ -37,6 +37,16 @@ set(BEAGLEBOARD_XM_FLAGS "-march=armv7-a -mtune=cortex-a8 -mfpu=neon -ftree-vect
#target_link_libraries(avr_node ${PROJECT_NAME})
#rosbuild_add_compile_flags(avr_node ${BEAGLEBOARD_XM_FLAGS})
+# Build Thanksgiving
+set(THANKSGIVING_SRCS src/Thanksgiving.cpp
+ src/AVRController.cpp
+ src/GPIO.cpp
+)
+rosbuild_add_executable(thanksgiving ${THANKSGIVING_SRCS})
+rosbuild_add_boost_directories()
+rosbuild_link_boost(thanksgiving system thread)
+rosbuild_add_compile_flags(thanksgiving ${BEAGLEBOARD_XM_FLAGS})
+
# Build the test
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") # fix for Ubuntu 11.10+
rosbuild_add_gtest(avrtest test/avrtest.cpp
@@ -53,3 +63,4 @@ rosbuild_add_compile_flags(avrtest ${BEAGLEBOARD_XM_FLAGS})
rosbuild_add_executable(gpio_export src/GPIOExport.cpp)
execute_process(COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/gpio_export_rights.sh)
+
View
7 avr_controller/include/AVRController.h
@@ -89,9 +89,10 @@ class AVRController : public boost::noncopyable
/**
* Interface with the MecanumMaster program running on the AVR.
*/
- bool ListFiniteStateMachines(std::vector<std::string> &fsmv);
- void DestroyFiniteStateMachine(const std::string &fsm);
- void CreateFiniteStateMachine(const std::string &fsm);
+ bool ListFSMs(std::vector<std::string> &fsmv);
+ void DestroyFSM(const std::string &fsm);
+ void CreateFSM(const std::string &fsm);
+ void ClearFSMs();
static uint16_t GetMsgLength(const void *bytes) { return *reinterpret_cast<const uint16_t*>(bytes); }
View
14 avr_controller/src/AVRController.cpp
@@ -305,7 +305,7 @@ void AVRController::ReadCallback(const boost::system::error_code &error, size_t
}
}
-bool AVRController::ListFiniteStateMachines(std::vector<std::string> &fsmv)
+bool AVRController::ListFSMs(std::vector<std::string> &fsmv)
{
fsmv.clear();
@@ -352,7 +352,7 @@ bool AVRController::ListFiniteStateMachines(std::vector<std::string> &fsmv)
return false;
}
-void AVRController::DestroyFiniteStateMachine(const std::string &fsm)
+void AVRController::DestroyFSM(const std::string &fsm)
{
struct
{
@@ -372,7 +372,7 @@ void AVRController::DestroyFiniteStateMachine(const std::string &fsm)
Send(strPrefix + fsm);
}
-void AVRController::CreateFiniteStateMachine(const std::string &fsm)
+void AVRController::CreateFSM(const std::string &fsm)
{
struct
{
@@ -392,6 +392,14 @@ void AVRController::CreateFiniteStateMachine(const std::string &fsm)
Send(strPrefix + fsm);
}
+void AVRController::ClearFSMs()
+{
+ vector<string> fsmv;
+ ListFSMs(fsmv);
+ for (vector<string>::const_iterator it = fsmv.begin(); it != fsmv.end(); it++)
+ DestroyFSM(*it);
+}
+
bool AVRController::SetDTR(bool level)
{
int fd = m_port.native();
View
192 avr_controller/src/Thanksgiving.cpp
@@ -0,0 +1,192 @@
+/*
+ * Copyright (C) 2112 Garrett Brown <gbruin@ucla.edu>
+ *
+ * This Program is free software; you can redistribute it and/or modify it
+ * under the terms of the Modified BSD License.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the organization nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * This Program is distributed AS IS in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#include "Thanksgiving.h"
+#include "GPIO.h"
+#include "BeagleBoardAddressBook.h"
+#include "ParamServer.h"
+
+#include <unistd.h> // for usleep
+#include <string>
+
+#include <iostream>
+
+#define ARDUINO_PORT "/dev/ttyACM0"
+#define BUTTON_TIMEOUT 5000000UL // 5.0s
+//#define POWER_TIMEOUT 5000000UL // 5.0s
+
+#define RED_FADE 1000
+
+using namespace std;
+
+int main(int argc, char **argv)
+{
+ Thanksgiving turkey;
+ turkey.Main();
+}
+void Thanksgiving::Main()
+{
+ // Connect to the Arduino
+ arduino.Open(ARDUINO_PORT);
+
+ // Wait 2 seconds
+ usleep(2000000L);
+
+ // Rev up the threads
+ m_bRunning = true;
+
+ boost::thread tempGreen(boost::bind(&Thanksgiving::GreenThreadRun, this));
+ m_greenThread.swap(tempGreen);
+
+ //boost::thread tempYellow(boost::bind(&Thanksgiving::YellowThreadRun, this));
+ //m_yellowThread.swap(tempYellow);
+
+ boost::thread tempRed(boost::bind(&Thanksgiving::RedThreadRun, this));
+ m_redThread.swap(tempRed);
+
+ //boost::thread tempThumbwheel(boost::bind(&Thanksgiving::ThumbwheelThreadRun, this));
+ //m_thumbwheelThread.swap(tempThumbwheel);
+
+ //boost::thread tempIMU(boost::bind(&Thanksgiving::IMUThreadRun, this));
+ //m_IMUThread.swap(tempIMU);
+
+ // Run until all threads have completed
+ m_greenThread.join();
+ m_redThread.join();
+}
+
+void Thanksgiving::GreenThreadRun()
+{
+ GPIO gpio(BUTTON_GREEN);
+ gpio.Open();
+
+ gpio.SetDirection(GPIO::IN);
+ gpio.SetEdge(GPIO::BOTH);
+
+ ParamServer::BatteryMonitor bm;
+ string fsm = bm.GetString();
+ enum STATE
+ {
+ ENABLED = 0,
+ DISABLED = 1
+ } state = DISABLED;
+
+ unsigned long duration = 0;
+ unsigned int post_value = 0;
+
+ while (m_bRunning)
+ {
+ if (gpio.Poll(BUTTON_TIMEOUT, duration, true, post_value))
+ {
+ // No timeout
+ if (post_value == 0)
+ {
+ // Pressed
+ if (state == DISABLED)
+ {
+ // Enable
+ cout << "Creating Battery FSM" << endl;
+ arduino.CreateFSM(fsm);
+ state = ENABLED;
+ }
+ else
+ {
+ // Disable
+ cout << "Destroying Battery FSM" << endl;
+ arduino.DestroyFSM(fsm);
+ state = DISABLED;
+ m_bRunning = false;
+ }
+ }
+ else
+ {
+ // Depressed
+ }
+ }
+ else
+ {
+ // Timeout
+ }
+ }
+ arduino.DestroyFSM(fsm);
+}
+
+void Thanksgiving::RedThreadRun()
+{
+ GPIO gpio(BUTTON_RED);
+ gpio.Open();
+
+ gpio.SetDirection(GPIO::IN);
+ gpio.SetEdge(GPIO::BOTH);
+
+ ParamServer::ChristmasTree xmastree;
+ string strXmastree = xmastree.GetString();
+ ParamServer::Fade fade;
+ fade.SetPin(LED_EMERGENCY);
+ fade.SetPeriod(RED_FADE);
+ fade.SetDelay(50);
+ fade.SetCurve(1);
+ string strFade = fade.GetString();
+ arduino.DestroyFSM(strFade); // Make sure FSM isn't running before we start
+
+ unsigned long duration = 0;
+ unsigned int post_value = 0;
+
+ while (m_bRunning)
+ {
+ if (gpio.Poll(BUTTON_TIMEOUT, duration, true, post_value))
+ {
+ // No timeout
+ if (post_value == 0)
+ {
+ // Pressed
+ cout << "Creating Fade FSM" << endl;
+ arduino.CreateFSM(strFade);
+ }
+ else
+ {
+ // Depressed
+ cout << "Destroying Fade FSM" << endl;
+ arduino.DestroyFSM(strFade);
+ }
+ }
+ else
+ {
+ // Timed out
+ if (gpio.GetValue() == 0)
+ {
+ // Timed out on press waiting for depress
+ m_bRunning = false;
+ arduino.DestroyFSM(strFade);
+ int result = system("poweroff");
+ (void)result;
+ // or system("init 0");
+ }
+ else
+ {
+ // Timed out on depress waiting for press
+ cout << "Timed out waiting for RED press" << endl;
+ }
+ }
+ }
+ arduino.DestroyFSM(strFade);
+}
View
52 avr_controller/src/Thanksgiving.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2112 Garrett Brown <gbruin@ucla.edu>
+ *
+ * This Program is free software; you can redistribute it and/or modify it
+ * under the terms of the Modified BSD License.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the organization nor the
+ * names of its contributors may be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * This Program is distributed AS IS in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+ */
+#pragma once
+
+#include "AVRController.h"
+#include <boost/thread.hpp>
+
+class Thanksgiving
+{
+public:
+ Thanksgiving() : m_bRunning(false) { }
+ void Main();
+
+ void GreenThreadRun();
+ void YellowThreadRun() { };
+ void RedThreadRun();
+ void ThumbwheelThreadRun() { }
+ void IMUThreadRun() { }
+
+private:
+ AVRController arduino;
+
+ boost::thread m_greenThread;
+ boost::thread m_yellowThread;
+ boost::thread m_redThread;
+ boost::thread m_thumbwheelThread;
+ boost::thread m_IMUThread;
+
+ // Used for threads waiting on file descriptors (where condition variables
+ // can't be used unless you turn the variable into a fd pipe and wait on that)
+ bool m_bRunning;
+};
+
View
36 avr_controller/test/avrtest.cpp
@@ -42,7 +42,7 @@ using namespace std;
bool bTestButtons = true;
bool bTestAVR = true;
bool bTestIMU = true;
-/*
+/**/
void TestButton(const char *color, unsigned int expansionPin)
{
GPIO gpio(expansionPin);
@@ -113,20 +113,20 @@ TEST(AVRTest, fsm)
ASSERT_TRUE(arduino.IsOpen());
vector<string> fsmv;
- EXPECT_TRUE(arduino.ListFiniteStateMachines(fsmv));
+ EXPECT_TRUE(arduino.ListFSMs(fsmv));
// Assume at least 1 FSM is loaded on startup
EXPECT_GT(fsmv.size(), 0);
for (vector<string>::const_iterator it = fsmv.begin(); it != fsmv.end(); it++)
- arduino.DestroyFiniteStateMachine(*it);
+ arduino.DestroyFSM(*it);
- EXPECT_TRUE(arduino.ListFiniteStateMachines(fsmv));
+ EXPECT_TRUE(arduino.ListFSMs(fsmv));
EXPECT_EQ(fsmv.size(), 0);
ParamServer::ChristmasTree xmastree;
- arduino.CreateFiniteStateMachine(xmastree.GetString());
+ arduino.CreateFSM(xmastree.GetString());
- EXPECT_TRUE(arduino.ListFiniteStateMachines(fsmv));
+ EXPECT_TRUE(arduino.ListFSMs(fsmv));
EXPECT_EQ(fsmv.size(), 1);
}
}
@@ -143,14 +143,14 @@ void TestBridge(unsigned int beaglePin, unsigned int arduinoPin)
// Record the initial number of FSMs
vector<string> fsmv;
- EXPECT_TRUE(arduino.ListFiniteStateMachines(fsmv));
+ EXPECT_TRUE(arduino.ListFSMs(fsmv));
size_t initialLength = fsmv.size();
// Create a toggle FSM. On creation it will pull the pin low
ParamServer::Toggle toggle;
toggle.SetPin((unsigned char)arduinoPin);
- arduino.CreateFiniteStateMachine(toggle.GetString());
- EXPECT_TRUE(arduino.ListFiniteStateMachines(fsmv));
+ arduino.CreateFSM(toggle.GetString());
+ EXPECT_TRUE(arduino.ListFSMs(fsmv));
ASSERT_EQ(fsmv.size(), initialLength + 1); // Don't continue if the FSM hasn't been installed
usleep(1000);
@@ -165,8 +165,8 @@ void TestBridge(unsigned int beaglePin, unsigned int arduinoPin)
EXPECT_EQ(gpio.GetValue(), 1);
// Remove the FSM, pin is pulled low as a post-condition
- arduino.DestroyFiniteStateMachine(toggle.GetString());
- EXPECT_TRUE(arduino.ListFiniteStateMachines(fsmv));
+ arduino.DestroyFSM(toggle.GetString());
+ EXPECT_TRUE(arduino.ListFSMs(fsmv));
ASSERT_EQ(fsmv.size(), initialLength);
usleep(1000);
EXPECT_EQ(gpio.GetValue(), 0);
@@ -175,8 +175,8 @@ void TestBridge(unsigned int beaglePin, unsigned int arduinoPin)
ParamServer::DigitalPublisher digitalPub;
digitalPub.SetPin((unsigned char)arduinoPin);
digitalPub.SetDelay(100000);
- arduino.CreateFiniteStateMachine(digitalPub.GetString());
- EXPECT_TRUE(arduino.ListFiniteStateMachines(fsmv));
+ arduino.CreateFSM(digitalPub.GetString());
+ EXPECT_TRUE(arduino.ListFSMs(fsmv));
ASSERT_EQ(fsmv.size(), initialLength + 1);
// Test the pin
@@ -290,8 +290,8 @@ TEST(MotorController, setSpeed)
motors.SetSpeed(-20, -20, -20, -20);
usleep(10 * 1000);
}
-*/
+/*
TEST(Sentry, seek)
{
if (!arduino.IsOpen())
@@ -300,7 +300,7 @@ TEST(Sentry, seek)
// Create a sentry FSM
ParamServer::Sentry sentry;
- arduino.CreateFiniteStateMachine(sentry.GetString());
+ arduino.CreateFSM(sentry.GetString());
// Wait for it to collect data
string strResponseLeft;
@@ -320,19 +320,25 @@ TEST(Sentry, seek)
cout << "SEEK RIGHT - ticks: " << sentryResR.GetTicks() << ", microseconds: " << sentryResR.GetMicroseconds() << endl;
}
+=======
+/**/
+
int main(int argc, char **argv)
{
cout << "Test hardware buttons? (y/n)" << endl;
//bTestButtons = (cin.get() == 'y');
+ bTestButtons = false;
cin.ignore(1000, '\n');
cout << "Test AVR? (y/n)" << endl;
//bTestAVR = (cin.get() == 'y');
+ bTestAVR = false;
cin.ignore(1000, '\n');
cout << "Test IMU? (y/n)" << endl;
//bTestIMU = (cin.get() == 'y');
+ bTestIMU = false;
cin.ignore(1000, '\n');
testing::InitGoogleTest(&argc, argv);
Please sign in to comment.
Something went wrong with that request. Please try again.