├── 20140403_171912.jpg ├── FindTarget.slx ├── LICENSE ├── README.md ├── Raspberry Pi Robot Demo.pdf ├── SerialPort.c ├── formatMotorMessage.m └── identifyTarget.m /20140403_171912.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omcaree/RaspberryPiRobot/3d41711baaa85280a41fc82bf37c06af3c3712f5/20140403_171912.jpg -------------------------------------------------------------------------------- /FindTarget.slx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omcaree/RaspberryPiRobot/3d41711baaa85280a41fc82bf37c06af3c3712f5/FindTarget.slx -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, The MathWorks, Inc. 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 18 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 21 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 22 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 | 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | RaspberryPiRobot 2 | ================ 3 | 4 | Control a Raspberry Pi powered robot with MATLAB and Simulink 5 | -------------------------------------------------------------------------------- /Raspberry Pi Robot Demo.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/omcaree/RaspberryPiRobot/3d41711baaa85280a41fc82bf37c06af3c3712f5/Raspberry Pi Robot Demo.pdf -------------------------------------------------------------------------------- /SerialPort.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2014 The MathWorks, Inc. */ 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int setupSerialPort(const char* device, unsigned int baud) { 8 | /* Open the serial port */ 9 | int spHandle = open(device, O_RDWR | O_NOCTTY | O_NDELAY); 10 | if (spHandle <= 0) { 11 | fprintf(stderr,"Error opening '%s': ", device); 12 | perror(""); 13 | return spHandle; 14 | } 15 | 16 | /* Determine baud rate */ 17 | speed_t baudRate; 18 | switch(baud) { 19 | case 1200: 20 | baudRate = B1200; 21 | break; 22 | case 1800: 23 | baudRate = B1800; 24 | break; 25 | case 2400: 26 | baudRate = B2400; 27 | break; 28 | case 4800: 29 | baudRate = B4800; 30 | break; 31 | case 9600: 32 | baudRate = B9600; 33 | break; 34 | case 19200: 35 | baudRate = B19200; 36 | break; 37 | case 38400: 38 | baudRate = B38400; 39 | break; 40 | case 57600: 41 | baudRate = B57600; 42 | break; 43 | case 115200: 44 | baudRate = B115200; 45 | break; 46 | default: 47 | printf("Unsupported baudrate %d for '%c', using 57600\n", baud, device); 48 | baudRate = B57600; 49 | } 50 | struct termios options; 51 | tcgetattr(spHandle, &options); 52 | options.c_cflag = baudRate | CS8 | CLOCAL | CREAD; 53 | options.c_iflag = IGNPAR; 54 | options.c_oflag = 0; 55 | options.c_lflag = 0; 56 | tcflush(spHandle, TCIFLUSH); 57 | tcsetattr(spHandle, TCSANOW, &options); 58 | 59 | return spHandle; 60 | } -------------------------------------------------------------------------------- /formatMotorMessage.m: -------------------------------------------------------------------------------- 1 | function [ data ] = formatMotorMessage( left, right, pan, tilt ) 2 | %FORMATMOTORMESSAGE Create the message buffer to send to the motor 3 | %controller 4 | % Byte 1: 255 (start byte 1) 5 | % Byte 2: 255 (start byte 2) 6 | % Byte 3: Message ID (value 0x02 = command) 7 | % Byte 4: Message Length (Number of bytes being sent, 9 in this case) 8 | % Byte 5: Command for left motor (0-255, 128 is stopped) 9 | % Byte 6: Command for right motor 10 | % Byte 7: Command for pan servo (0-180, degrees) 11 | % Byte 8: Command for tilt servo 12 | % Byte 9: Checksum (complement of sum of bytes) 13 | % 14 | % Copyright 2014 The MathWorks, Inc. 15 | messageID = 2; 16 | messageLength = 9; 17 | 18 | tilt = tilt + 80; 19 | pan = pan + 90; 20 | 21 | data = uint8([255 255 messageID messageLength left right pan tilt 0]); 22 | 23 | % Checksum is the complement of the sum of each byte 24 | % Excluding start characters 25 | sum = uint32(0); 26 | for i=3:(messageLength-1) 27 | sum = sum + uint32(data(i)); 28 | end 29 | data(end) = uint8(bitcmp(sum,'uint32')); 30 | 31 | end 32 | 33 | -------------------------------------------------------------------------------- /identifyTarget.m: -------------------------------------------------------------------------------- 1 | %% Detect the presence of a target with the Raspberry Pi Camera 2 | % Copyright 2014 The MathWorks, Inc. 3 | 4 | %% Connect to the Raspberry Pi 5 | myPi = raspi('192.168.0.1','pi','raspberry') 6 | 7 | %% Connect to the camera board 8 | myCam = cameraboard(myPi,'Resolution', '320x240') 9 | 10 | %% Set up GPIO as input 11 | configureDigitalPin(myPi, 18, 'input') 12 | 13 | %% Connect to the serial port 14 | mySP = serialdev(myPi,'/dev/ttyAMA0', 57600) 15 | 16 | %% Send a message to the motor controller 17 | pan = 0; 18 | tilt = -20; 19 | data = formatMotorMessage(128, 128, pan, tilt); 20 | write(mySP,data); 21 | 22 | %% Keep reading images from the camera until we press the button 23 | figure 24 | subplot(1,3,1); 25 | 26 | while (~readDigitalPin(myPi,18)) 27 | mySnapshot = snapshot(myCam); 28 | imshow(mySnapshot); 29 | title('Live'); 30 | end 31 | hold on; 32 | title('Snapshot'); 33 | %% Display green channel 34 | red = mySnapshot(:,:,1); 35 | green = mySnapshot(:,:,2); 36 | blue = mySnapshot(:,:,3); 37 | 38 | subplot(1,3,2); 39 | imshow(green); 40 | title('Green Channel'); 41 | 42 | %% Calculate green intensity 43 | greenIntensity = green - red/2 - blue/2; 44 | 45 | subplot(1,3,2); 46 | imshow(greenIntensity); 47 | title('Green Intensity'); 48 | 49 | %% Threshold the intensity image to create a binary image 50 | greenThreshold = 40; 51 | greenBinary = greenIntensity > greenThreshold; 52 | 53 | subplot(1,3,3); 54 | imshow(greenBinary) 55 | title('Green Binary'); 56 | 57 | %% Determine how many pixels big the target is 58 | numberOfGreenPixels = sum(greenBinary(:)) 59 | 60 | %% Close connection to camera and Raspberry Pi 61 | clear myCam myPi mySP --------------------------------------------------------------------------------