diff --git a/source/include/propeller/Channel.h b/source/include/propeller/Channel.h new file mode 100644 index 0000000..9e96ec3 --- /dev/null +++ b/source/include/propeller/Channel.h @@ -0,0 +1,94 @@ +/** +* __ +* _________ / /_ ____ ________ ____________ _____ +* /___/ __ \/ __ \/ __ \/ ___/ _ \/ ___/ ___/ / / / _ \ +* / / / /_/ / /_/ / /_/ / / / __(__ ) /__/ /_/ / __/ +* /_/ \____/_.___/\____/_/ \___/____/\___/\__,_/\___/ +* +* +* @file Channel.h +* @date Created: 16-4-2015 +* @version 1.0 +* +* @author Nathan Schaaphuizen +* +* @section LICENSE +* License: newBSD +* +* Copyright © 2015, HU University of Applied Sciences Utrecht. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +* - 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. +* - Neither the name of the HU University of Applied Sciences Utrecht nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE HU UNIVERSITY OF APPLIED SCIENCES UTRECHT +* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**/ + +#ifndef _CHANNEL_H +#define _CHANNEL_H + +/// @brief Class for passing data synchronously between cogs. +/// +/// The Channel is a class to pass data synchronously between cogs. +/// It has a fixed size. The channel makes use of the FIFO principle. +template +class Channel{ +private: + T buffer[S]; + volatile int index; +public: + + /// @brief Creates new channel object. + Channel(): + index{0} + {} + + /// @brief Write data in channel. + /// + /// This function will block if channel is full. + void write(T t) volatile{ + //Spinlock the cog if channel is full. + //We can't write in a full channel. + while(index >= S); + //Write new data in buffer and up the buffer index. + buffer[index++] = t; + } + + + /// @brief Read data from the channel. + /// + /// This function will block if channel is empty. + T read() volatile{ + //Spinlock the cog if channel is empty. + //we can't read from a empty channel. + while(index <= 0); + //Temporary item that we're going to return later. + T tmp = buffer[0]; + //Lower the buffer index. + index--; + //Copy all data one to the left. + //This array will go out of bounds. + //But that's ok, it will fill the channel with we junk data + //that will be overwritten by the next write(). + for(int i=0;i +#include "Channel.h" + +/// @brief Class for communication between propeller and pc (or PI). +/// +/// The Uart class is a half buffer full duplex communication class. +/// It makes use of the UART protocol to send data over the debug port to a +/// attached computer. The UART specifications are (8-n-1) at a baud of 115200 bps. +/// All incoming data is buffered up to 128 bytes. All data after that is lost. +/// All outgoing data is not buffered but send directly. +/// Note that all data is written and read in a binary format. +/// Note this class makes use of a cog. +class Uart{ +private: + // Read buffer size of 128 chars. + Channel readChannel; + // This is the minimum stack size required to for the cog (thread) to run. + // Don't set it below 192. + char stack[192]; + int cogId; + + static void readBuffer(void*); + +public: + /// @brief Creates a Uart object. + /// + /// Initializes a free cog for use. + Uart(); + + /// @brief Destroys the Uart object. + /// + /// Frees the used cog. + ~Uart(); + + /// @brief Read a character. + /// + /// Read one character from the buffer. + /// @return Next character in buffer. + char readChar(); + + /// @brief Read a integer. + /// + /// Read the next four characters from the buffer and + /// converts these to a integer. + /// @return Next integer in buffer. + int readInt(); + + /// @brief Send a character. + /// + /// Directly sends a character. + /// @param data The character to be sent. + void send(char data); + + /// @brief Send a integer. + /// + /// Converts an integer to four characters and directly sends these. + /// @param data The integer to be sent. + void send(int data); + +}; + +#endif // _UART_H \ No newline at end of file diff --git a/source/include/propeller/UltrasonicSensor.h b/source/include/propeller/UltrasonicSensor.h new file mode 100644 index 0000000..4a9ad8d --- /dev/null +++ b/source/include/propeller/UltrasonicSensor.h @@ -0,0 +1,77 @@ +/** +* __ +* _________ / /_ ____ ________ ____________ _____ +* /___/ __ \/ __ \/ __ \/ ___/ _ \/ ___/ ___/ / / / _ \ +* / / / /_/ / /_/ / /_/ / / / __(__ ) /__/ /_/ / __/ +* /_/ \____/_.___/\____/_/ \___/____/\___/\__,_/\___/ +* +* +* @file UltrasonicSensor.h +* @date Created: 25-4-2015 +* @version 2.1 +* +* @author Edwin Koek +* +* @section LICENSE +* License: newBSD +* +* Copyright © 2015, HU University of Applied Sciences Utrecht. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +* - 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. +* - Neither the name of the HU University of Applied Sciences Utrecht nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE HU UNIVERSITY OF APPLIED SCIENCES UTRECHT +* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**/ + +#ifndef ULTRASONICSENSOR_H +#define ULTRASONICSENSOR_H + +/** + * @brief The UltraSonicSensor class + * This class creates objects that can be used to read data from a ultrasonic sensor. + */ +class UltraSonicSensor{ +public: + /** + * @brief UltraSonicSensor constructor + * @param pin The pin that wil communicate with an ultrasonic sensor. + */ + UltraSonicSensor(int pin); + + /** + * @brief getDistance + * getDistance calculates and returns a distance based on the reading of an ultrasonic sensor. + * More explanation on how the distance is calculated can be found on the roborescue wiki page. + * @return Distance in centimeters. The reading wil be seen as invalid and return -1 if the + * calculated distance is not between 2 and 330 centimeters. + */ + int getDistance(); + + /** + * @brief setTemperature + * setTemperature sets the temperature that is used by the distance calculations to the + * temperature that is passed to the function. The temperature is in Celcius. + * @param temp Temperature in Celcius + */ + void setTemperature(float temp); + +private: + //! The pin on the propeller that communicates with the sensor. + int pin; + //! The temperature that is used in the distance calculations. + float temperature; +}; + +#endif diff --git a/source/src/propeller/Qik.cpp b/source/src/propeller/Qik.cpp new file mode 100644 index 0000000..de92023 --- /dev/null +++ b/source/src/propeller/Qik.cpp @@ -0,0 +1,191 @@ +/** +* __ +* _________ / /_ ____ ________ ____________ _____ +* /___/ __ \/ __ \/ __ \/ ___/ _ \/ ___/ ___/ / / / _ \ +* / / / /_/ / /_/ / /_/ / / / __(__ ) /__/ /_/ / __/ +* /_/ \____/_.___/\____/_/ \___/____/\___/\__,_/\___/ +* +* +* @file Qik.cpp +* @date Created: 13-4-2015 +* @version 1.0 +* +* @author Nathan Schaaphuizen +* +* @section LICENSE +* License: newBSD +* +* Copyright © 2015, HU University of Applied Sciences Utrecht. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +* - 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. +* - Neither the name of the HU University of Applied Sciences Utrecht nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE HU UNIVERSITY OF APPLIED SCIENCES UTRECHT +* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**/ + +#include "Qik.h" +#include + +Qik::Qik(int pinTx, int pinRx, int baudRate): +tx{pinTx}, +rx{pinRx}, +bitWidth{(US/baudRate)*us} +{ + //Configure pin tx as a output pin. + set_direction(tx, OUTPUT); + high(tx); + //Configure pin rx as a input pin. + set_direction(rx, INPUT); + //Send the synchronisation byte. + //The Qik 2s12v10 expects this to be the first byte and uses it + //to determine the baud rate used for now on. + TX(cSYN); +} + +void Qik::TX(unsigned char byte){ + //Start bit: + //Make output pin low. The output pin is high in rest. + low(tx); + //Wait the length of 1 bit. + waitcnt(CNT+bitWidth); + //8 data bits: + //Send 8 data bits LSB. + for(int i=0;i<8;++i){ + //Check the LSB (most right bit). + //If bit is 1: + if (byte & 0x01 != 0){ + //Make ouput pin high. + //1 = high + high(tx); + } + //If bit is 0: + else{ + //Make ouput pin low. + //0 = low + low(tx); + } + //Shift all bits 1 position to the right. + //This will give a new LSB to send. + byte >>= 1; + //Wait the length of 1 bit. + waitcnt(CNT+bitWidth); + } + //Stop bit: + //Make output pin high. The output pin is high in rest. + high(tx); + //Wait the length of 1 bit. + waitcnt(CNT+bitWidth); +} + +unsigned char Qik::RX(){ + //Char to fill. + unsigned char byte = 0; + //Wait for the start bit to be over. + waitcnt(CNT+bitWidth); + //Read 8 data bits: + for(int i=0;i<8;++i){ + //Check the input pin. + if (input(rx) == 1){ + //Write a 1 on the MSB. + byte |= 0x80; + } + //Shift all bits 1 position to the right. + byte >>= 1; + //Wait the length of 1 bit. + waitcnt(CNT+bitWidth); + } + //Wait for the stop bit to be over. + waitcnt(CNT+bitWidth); + //Return the newly filled byte. + return byte; +} + + +int Qik::getFirmwareVersion(){ + //Send the control byte that requests the firmware version. + TX(cFWV); + //Read and return the response. + return RX(); +} + +int Qik::getError(){ + //Send the control byte that requests the error. + TX(cERR); + //Read and return the response. + return RX(); +} + + +void Qik::setMotorSpeed(Motor motor, signed char speed){ + //Flag holding the direction the motor needs to turn. + //0x00 = CCW + //0x02 = CW + char dirFlag = 0x00; + //Check if speed is negative. + //If so we need to turn the motor the other way around. + if (speed < 0){ + //Set motor direction clockwise. + dirFlag = 0x02; + } + //Make the speed absolute. Does nothing if it already was. + //Speed is expected in a range from 0 to 127 included. + speed = abs(speed); + + if(speed > 127) speed = 127; + //Flag holding the motor that needs to change. + //0x00 = motor 0 + //0x04 = motor 1 + char motorFlag = 0x00; + //Check if motor is motor 1. If not we assume motor 0. + if(motor == Motor::M1){ + //Set the motor to motor 1. + motorFlag = 0x04; + //Invert the motor direction. + //Please note that the motors are placed as each other's mirror image. + //Causing the motors set in the same direction to turn in each other's opposite direction instead. + //To solve this problem we invert one of the directions provided by the user. + if(dirFlag) dirFlag = 0x00; + else dirFlag = 0x02; + } + //The base motor command. + char motorCommand = 0x88; + //Add the direction flag. + motorCommand |= dirFlag; + //Add the motor flag. + motorCommand |= motorFlag; + //Send the motor command byte. + TX(motorCommand); + //Send the motor speed byte. + TX(speed); +} + + +void Qik::setBrakePower(Motor motor, unsigned char strength){ + //Check if strength exceeds the maximum if so set it + //to the highest allowed value. + //The motor brake strength is expected between 0 and 127 included. + if(strength > 127) strength = 127; + //Motor command. + char motorCommand = cM0B; + //Check if motor is motor 1. If not we assume motor 0. + if(motor == Motor::M1){ + //Set motor command to motor 1. + motorCommand = cM1B; + } + //Send motor brake strength command byte. + TX(motorCommand); + //Send motor brake strength. + TX(strength); +} \ No newline at end of file diff --git a/source/src/propeller/Uart.cpp b/source/src/propeller/Uart.cpp new file mode 100644 index 0000000..3c32fa9 --- /dev/null +++ b/source/src/propeller/Uart.cpp @@ -0,0 +1,97 @@ +/** +* __ +* _________ / /_ ____ ________ ____________ _____ +* /___/ __ \/ __ \/ __ \/ ___/ _ \/ ___/ ___/ / / / _ \ +* / / / /_/ / /_/ / /_/ / / / __(__ ) /__/ /_/ / __/ +* /_/ \____/_.___/\____/_/ \___/____/\___/\__,_/\___/ +* +* +* @file Uart.cpp +* @date Created: 13-5-2015 +* @version 1.1 +* +* @author Nathan Schaaphuizen +* +* @section LICENSE +* License: newBSD +* +* Copyright © 2015, HU University of Applied Sciences Utrecht. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +* - 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. +* - Neither the name of the HU University of Applied Sciences Utrecht nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE HU UNIVERSITY OF APPLIED SCIENCES UTRECHT +* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**/ + +#include "Uart.h" +#include +#include "Channel.h" + +Uart::Uart(){ + //Claim a cog. + cogId = cogstart(&Uart::readBuffer, this, stack, sizeof(stack)); +} + +Uart::~Uart(){ + //Free the cog. + cogstop(cogId); +} + +void Uart::readBuffer(void* obj){ + //Convert the obj to a Uart object. + //Since the obj was originally a Uart obj this is legal. + Uart* enc = reinterpret_cast(obj); + //Run forever + while(true){ + //Read the next incoming character and put it in the buffer. + //getchar() will block if there is nothing to read. + enc->readChannel.write(getchar()); + } +} + +char Uart::readChar(){ + //Read and return the next character from the buffer. + return readChannel.read(); +} + +int Uart::readInt(){ + //The four character we're going to read and convert into a int. + char intChar[4]; + //Read the four characters. + for(int i =0; i<4; ++i){ + intChar[i] = readChannel.read(); + } + //Cast the four read chars to a int. + //This is faster and easier than bit shifting. + int data = *(reinterpret_cast(intChar)); + //Return the int. + return data; +} + + void Uart::send(char data){ + //Send the character. + putChar(data); + } + + void Uart::send(int data){ + //Cast the int to four chars. + //This is faster and easier the bit shifting and copying. + char *intChar = reinterpret_cast(&data); + //Send all four bytes. + for(int i =0; i<4; ++i){ + putChar(intChar[i]); + } + } + \ No newline at end of file diff --git a/source/src/propeller/UltrasonicSensor.cpp b/source/src/propeller/UltrasonicSensor.cpp new file mode 100644 index 0000000..55571f8 --- /dev/null +++ b/source/src/propeller/UltrasonicSensor.cpp @@ -0,0 +1,64 @@ +/** +* __ +* _________ / /_ ____ ________ ____________ _____ +* /___/ __ \/ __ \/ __ \/ ___/ _ \/ ___/ ___/ / / / _ \ +* / / / /_/ / /_/ / /_/ / / / __(__ ) /__/ /_/ / __/ +* /_/ \____/_.___/\____/_/ \___/____/\___/\__,_/\___/ +* +* +* @file UltrasonicSensor.cpp +* @date Created: 25-4-2015 +* @version 2.1 +* +* @author Edwin Koek +* +* @section LICENSE +* License: newBSD +* +* Copyright © 2015, HU University of Applied Sciences Utrecht. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +* - 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. +* - Neither the name of the HU University of Applied Sciences Utrecht nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE HU UNIVERSITY OF APPLIED SCIENCES UTRECHT +* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**/ + +#include "UltrasonicSensor.h" +#include "simpletools.h" + +UltraSonicSensor::UltraSonicSensor(int pin): +pin{pin}, +temperature{22} +{ + low(pin); +} + +int UltraSonicSensor::getDistance(){ // Distance is L = C * T / 2 where L = Length, C = Speed of sound in air and T = Time difference transmission from transmitter to receiver. All this is divided by 2 because of the two-directions the sound travels. + pulse_out(pin,10); // Sends a pulse out for 10 microseconds (see http://www.ouhk.edu.hk/~sctwww/computing/robotics/Learn/Simple%20Libraries/Utility/libsimpletools/html/simpletools_8h.html#a2608f553978382b7abdccfc62be3e75c) + int velocity = 331.5 + (0.6 * temperature); // TODO: Correct calculation for temperature and define C (speed of sound in air) + int travelTime = pulse_in(pin,1); // Returns time while pin is high + print("Travel time: %d\n", travelTime); + int distance = (velocity * travelTime * 100)/(2 * 1000000); + if(2 < distance && distance < 330){ + return distance; + }else{ + return -1; + } +} + + +void UltraSonicSensor::setTemperature(float temp){ + temperature = temp; +} diff --git a/source/src/propeller/UltrasonicSensorTestCode.cpp b/source/src/propeller/UltrasonicSensorTestCode.cpp new file mode 100644 index 0000000..546a420 --- /dev/null +++ b/source/src/propeller/UltrasonicSensorTestCode.cpp @@ -0,0 +1,149 @@ +/** +* __ +* _________ / /_ ____ ________ ____________ _____ +* /___/ __ \/ __ \/ __ \/ ___/ _ \/ ___/ ___/ / / / _ \ +* / / / /_/ / /_/ / /_/ / / / __(__ ) /__/ /_/ / __/ +* /_/ \____/_.___/\____/_/ \___/____/\___/\__,_/\___/ +* +* +* @file Rosbee.cpp +* @date Created: 16-3-2015 +* @version 2.0 +* +* @author Nathan Schaaphuizen +* +* @section LICENSE +* License: newBSD +* +* Copyright © 2015, HU University of Applied Sciences Utrecht. +* All rights reserved. +* +* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: +* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. +* - 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. +* - Neither the name of the HU University of Applied Sciences Utrecht nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, +* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE HU UNIVERSITY OF APPLIED SCIENCES UTRECHT +* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE +* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**/ + +#include +#include "Qik.h" +#include "UltrasonicSensor.h" +#include + +/// Entry point of application. +int main(){ + //Pin connected to the rx pin of the Qik motor controller. + int qTx = 25; + //Pin connected to the tx pin of the Qik motor controller. + int qRx = 26; + //Baud rate used to communicate with the Qik motor controller. + int qBaud = 115200; + + //Pin connected to the ultrasonic sensor at the front of the rosbee on the left side. + int ussPin1 = 5; + //Pin connected to the ultrasonic sensor at the front of the rosbee in the middle. + int ussPin2 = 6; + //Pin connected to the ultrasonic sensor at the front of the rosbee on the right side. + int ussPin3 = 7; + //Pin connected to the ultrasonic sensor at the back of the rosbee on the left side. + int ussPin4 = 8; + //Pin connected to the ultrasonic sensor at the back of the rosbee on the right side. + int ussPin5 = 4; + //Pin connected to the ultrasonic sensor at the back of hte rosbee int the middle. + int ussPin6 = 9; + + //If you wish to send debug information to the console you need to make the propeller wait a sec. + //The propeller is faster then the startup of the console. This will result in data being missed. + //Uncomment while debugging. + //sleep(1); + + //Uart object for communication. + Uart uart; + //Qik object for motor control. + Qik qik{qTx,qRx,qBaud}; + //Stop motor 1. + //This is done so the rosbee won't drive away and/or stop while the + //program is rebooted. + qik.setMotorSpeed(Qik::Motor::M0,0); + + //Stop motor 2. + //This is done so the rosbee won't drive away and/or stop while the + //program is rebooted. + qik.setMotorSpeed(Qik::Motor::M1,0); + + //Ultrasonic sensor object for the sensor front left. + UltraSonicSensor uss1(ussPin1); + //Ultrasonic sensor object for the sensor front middle. + UltraSonicSensor uss2(ussPin2); + //Ultrasonic sensor object for the sensor front right. + UltraSonicSensor uss3(ussPin3); + //Ultrasonic sensor object for the sensor back left. + UltraSonicSensor uss4(ussPin4); + //Ultrasonic sensor object for the sensor front right. + UltraSonicSensor uss5(ussPin5); + + //Variables used for communcation. + //cmd = command byte received. + //value = follow byte received. + //rtn = byte to be send. + //intRtn = int(4 bytes) to be send. + //speed = motor speed (can be negative). + char cmd, value, rtn; + int intRtn; + signed char speed; + + //Run forever. + //The rosbee is expected to work as long as it has power. + //Therefore this loop never needs to end. + while(true){ + //Get the command byte. + //This will block if no byte is available. + cmd = uart.readChar(); + + //Check which command to execute. + //This is just a epic long switch case. + //There was honestly no better way to do this that does + //not require making infinite classes. + switch(cmd){ + //Ultrasonic Sensors + //Commands regarding the ultrasonic sensors. + case '1': + intRtn = uss1.getDistance(); + //uart.send(intRtn); + print("Distance #1: %d\n", intRtn); + break; + case '2': + intRtn = uss2.getDistance(); + //uart.send(intRtn); + print("Distance #2: %d\n", intRtn); + break; + case '3': + intRtn = uss3.getDistance(); + //uart.send(intRtn); + print("Distance #3: %d\n", intRtn); + break; + case '4': + intRtn = uss4.getDistance(); + //uart.send(intRtn); + print("Distance #4: %d\n", intRtn); + break; + case '5': + intRtn = uss5.getDistance(); + //uart.send(intRtn); + print("Distance #5: %d\n", intRtn); + break; + } // End switch. + } // End while. + //The program should never come here, but it's required by the compiler. + return 0; +} \ No newline at end of file