Difference between revisions of "S18: Team Nemesis(SJSU Cam)"

From Embedded Systems Learning Academy
Jump to: navigation, search
(Created page with "== Team Nemesis(SJSU Cam) == == Abstract == A simple video streaming over TFT LCD using CMOS camera using SJONE board. == Objectives & Introduction == With this project, w...")
 
(Algorithm:)
 
(243 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Team Nemesis(SJSU Cam) ==
+
== '''SJSU Cam''' ==
  
== Abstract ==
+
[[File:CMPE244_cam_top.png|800px|SJSU_CAM|center]]
  
A simple video streaming over TFT LCD using CMOS camera using SJONE board.
+
== '''Abstract''' ==
 +
This project records live stream through camera and displays it on LCD through SJ One microcontroller which is LPC 1758. This camera has longer battery as it runs through portable battery powering microcontroller. As there are many High Quality Cameras available in market, purpose of this camera is found in applications where streaming of medium quality video/photo is required for longer period of time and camera has to survive longer on single battery, which can’t be provided by handy digital camera. Another functionality provided by this project is to enable user to capture images on SD card and view it on LCD providing different resolution and modes as option through interactive UI designed on LCD. Project Hardware enclosed in small box 3D designed for this project enables portability and ease of access to user.
  
== Objectives & Introduction ==
+
== '''Objectives & Introduction''' ==
 +
The objective of this project was to learn how timing and buffer play an important role in capturing the pixels from the camera and displaying them on the LCD screen. The following objectives were set for the project:
  
With this project, we aim to design and build a camera streaming project using SJONE board.   
+
<br/>1. Write a driver for the SPI camera ov2640, which has a better rate of capturing the image with the SJONE board.
 
+
<br/>2. Write the driver for the LCD display (Adafruit 5' TFT) and integrate it by choosing the right resolution.
The objectives of the project are as follows:
+
<vr/>3. Implement a menu class for different options of the camera on the LCD and program external buttons for navigating across the menu.
-
+
<br/>3. Integrate the camera and LCD by displaying the output of the camera on the LCD.
-
+
<br/>4. Store the output of the camera on the SD card, if capture mode is selected from LCD menu.
-
+
<br/>5. Read the image from the SD card and display on the screen.
 
 
<br>
 
  
  
 
=== Team Members & Responsibilities ===
 
=== Team Members & Responsibilities ===
'''Part A'''
+
*  Chhavi Mehta
[Saurabh]
+
**  Camera implementation, integration with SD card
'''Part B'''
+
Deeksha Prakash Kankalale
[https://www.linkedin.com/in/spoorthims Spoorthi Mysore Shivakumar] 
+
**  PCB Design, Camera driver implementation.
 +
Parth Gujar
 +
**  LCD Driver, 3D design.
 +
*  Sagar Kalathia
 +
**  LCD interfacing, implementing graphics library
 +
*  Saurabh Badenkal
 +
**  LCD Driver, Integration of camera and LCD and data pipelining.
  
== Schedule ==
+
== '''Schedule''' ==
  
'''Team Schedule'''
 
{| class="wikitable"
 
|-
 
! scope="col"| SI No.
 
! scope="col"| Start Date
 
! scope="col"| End Date
 
! scope="col"| Task
 
! scope="col"| Status
 
! scope="col"| Actual Completion Date
 
|-
 
! scope="row"| 1
 
| 03/14/2017
 
| 03/21/2017
 
|
 
* Project proposal submission.
 
* Dividing module ownership among the team members.
 
* Decide on components required.
 
| Completed
 
| 03/21/2017
 
|-
 
! scope="row"| 2
 
| 03/21/2017
 
| 03/28/2017
 
|
 
* Brainstorm on the hardware and software design.
 
* List the interfaces and order the components.
 
| Completed
 
| 03/28/2017
 
|-
 
! scope="row"| 3
 
| 03/29/2017
 
| 04/04/2017
 
|
 
* Study the datasheets of the components and understand the pin connections.
 
* Prepare pinout diagrams and prepare the hardware design
 
* Prepare the software design flow of each of the modules.
 
| Completed
 
| 04/04/2017
 
|-
 
! scope="row"| 4
 
| 04/05/2017
 
| 04/12/2017
 
|
 
* Design the PCB and place order for fabrication.
 
| Completed
 
| 04/12/2017
 
|-
 
! scope="row"| 5
 
| 04/13/2017
 
| 04/18/2017
 
|
 
* Interface Stepper Motor with SJOne Board and get the functionality working.
 
* Implement a basic Android application.
 
* Implement UART driver for interfacing the Bluetooth module with the SJOne board.
 
| Completed
 
| 04/18/2017
 
|-
 
! scope="row"| 6
 
| 04/19/2017
 
| 04/25/2017
 
|
 
* Interface DC Motor with SJOne Board and get the functionality working.
 
* Check of communication between Android application and SJOne board.
 
* Implement the control of the camera via Bluetooth.
 
| Completed
 
| 04/25/2017
 
|-
 
! scope="row"| 7
 
| 04/26/2017
 
| 05/02/2017
 
|
 
* Build the robot with all interfaces integrated.
 
* Complete the development and testing of all modules stand-alone.
 
| Completed
 
| 05/02/2017
 
|-
 
! scope="row"| 8
 
| 05/03/2017
 
| 05/09/2017
 
|
 
* Integrate all the modules and test for functionality as a unit.
 
* Ensure that the motor responds to the commands provided by the application accurately.
 
| Completed
 
| 05/09/2017
 
|-
 
! scope="row"| 9
 
| 05/10/2017
 
| 05/15/2017
 
|
 
* Final testing and debugging phase.
 
* Update project report and prepare for demo.
 
| Completed
 
| 05/25/2017
 
|-
 
|}
 
  
== Parts List & Cost ==
 
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
! scope="col"| Item#
+
! scope="col"| Week#
! scope="col"| Part Name
+
! scope="col"| Date
! scope="col"| Vendor
+
! scope="col"| Task
! scope="col"| Cost $
+
! scope="col"| Actual
 
|-
 
|-
 
! scope="row"| 1
 
! scope="row"| 1
| SJOne Board
+
| 4/17
| [http://www.socialledge.com/sjsu/index.php?title=Main_Page From Preet]
+
| 1. Placing order for the components required in the project. <br/> 2. Division of module development.
| 80$
+
| Completed! <br/> Problems Encountered: Finding the right components after going through various datasheets.
 
|-
 
|-
 
! scope="row"| 2
 
! scope="row"| 2
| PCB Fabrication
+
| 4/24
| [http://www.4pcb.com/ 4PCB]
+
| 1. Making interface with LCD.<br/>2. Drawing simple shapes. <br/>3. Work on the overall design of the menu for LCD.
| 66$
+
| Completed! <br/> Problems Encountered: Setting appropriate initial frequency for the LCD to light up.
 
|-
 
|-
 
! scope="row"| 3
 
! scope="row"| 3
| Bluetooth Module
+
| 5/1
| [https://www.sparkfun.com/products/11601 Sparkfun]
+
| 1. Interfacing the camera <br/>2. Implementing menu class with sub-menus and items. <br/>3. Designing hardware prototype for 3D printing.
| 25$
+
| Completed! <br/>  Problems Encountered: Not enough RAM for displaying more than 1 picture of size 100x100, Using interrupts for UART receive instead of normal function call.
 
|-
 
|-
 
! scope="row"| 4
 
! scope="row"| 4
| Chassis
+
| 5/8
| [https://www.amazon.com/gp/product/B01BXPETQG/ref=oh_aui_detailpage_o00_s00?ie=UTF8&psc=1 Amazon]
+
| 1. Design PCB and place order. <br/>2. Integrating file explorer of the SD card onto LCD with GUI.<br/>3. Capturing image from the camera, saving it on the SD card.  
| 15$
+
| Completed! <br/> Problems Encountered: Heavy resource utilization while saving and retrieving the file from SD card.
 
|-
 
|-
 
! scope="row"| 5
 
! scope="row"| 5
| Battery - 1000mAh
+
| 5/15
| [https://www.amazon.com/gp/product/B00MVQ8T6I/ref=oh_aui_detailpage_o03_s00?ie=UTF8&psc=1 Amazon]
+
| 1. Displaying the image on the LCD. <br/>2. Integrating the modules together with the 3D case having the LCD on one side and SJONE and camera on the other with buttons.
| 14$
+
| Completed! <br/> Problems Encountered: Resource constraints only allow a resolution of 320p. Extremely low frame rate due to SD read speeds. 
 
|-
 
|-
 
! scope="row"| 6
 
! scope="row"| 6
| Motor Wheel Couplers
+
| 5/22
| [https://www.amazon.com/gp/product/B01M8QXY8N/ref=oh_aui_detailpage_o02_s00?ie=UTF8&psc=1 Amazon]
+
| 1. Final testing and debugging of the system as a whole.
| 10$
+
| Completed! <br/> Problems Encountered: PCB arrived late.
|-
 
! scope="row"| 7
 
| Corner Braces
 
| [https://www.amazon.com/gp/product/B00004Z0R1/ref=oh_aui_detailpage_o02_s00?ie=UTF8&psc=1 Amazon]
 
| 6$
 
|-
 
! scope="row"| 8
 
| Lipo Battery Charger
 
| [https://www.amazon.com/gp/product/B01HXPJOTC/ref=oh_aui_detailpage_o03_s01?ie=UTF8&psc=1 Amazon]
 
| 12$
 
|-
 
! scope="row"| 9
 
| Motor Drive Shield
 
| [https://www.amazon.com/gp/product/B00813HBBO/ref=oh_aui_detailpage_o07_s00?ie=UTF8&psc=1 Amazon]
 
| 5.5$
 
|-
 
! scope="row"| 10
 
| Stepper motor
 
| [https://www.amazon.com/gp/product/B00PNEQI7W/ref=oh_aui_detailpage_o07_s01?ie=UTF8&psc=1 Amazon]
 
| 13$
 
 
|}
 
|}
  
== Design & Implementation ==
+
== '''Parts List & Cost''' ==
=== Hardware Design ===
 
This section includes schematics and hardware interface of the components used in the project.
 
  
The hardware components used in this project include:
 
* Motor Shield
 
* Stepper Motor
 
* DC Motor
 
* Bluetooth module.
 
 
 
[[File:CmpE244 S17 CamBot Hardware.png|600px|thumb|center|Hardware Interface]]
 
 
<br>
 
=== PCB Design ===
 
 
Eagle 8.1.0 software was used for PCB design. We created a two-layer PCB of size 10cm x 8cm.
 
 
The steps involved in the PCB design process are discussed below:
 
<BR/>
 
====PCB Schematic Design====
 
 
As a first step, we designed a circuit using the components available in the Eagle library. The two important considerations were:
 
 
* '''Electrical Characteristics:''' Based on our requirement of voltage or current rating, we had to select the components which were suitable for our board. For example, if we wanted to connect a switch to conduct 5A current, we should not select a switch with 2A current rating from the library because the copper width for a 2A switch will not be able to conduct 5A current.
 
* '''Component Selection:''' We had to to carefully select the components because each component has specific dimension as defined in their library. So, once we convert this schematic into board layout, the size of the pads printed on the board depends on the component selection. We cannot change the size if we select the library components. But there is an option to create our custom components with required electrical characteristics and dimensions. For our design, we used most of the parts from the Sparkfun library as it is very reliable and some of the parts which are not available in the library were created manually using Eagle. 
 
     
 
[[File:CmpE244 S17 CamBot PCB Schematic.png|700px|thumb|center|PCB Schematics]]
 
<br>
 
'''EAGLE Components'''
 
 
{| class="wikitable"
 
{| class="wikitable"
 
|-
 
|-
! scope="col"| Parts#
+
! scope="col"| Components
! scope="col"| Part Name
+
! scope="col"| Model Number
! scope="col"| DEVICE
+
! scope="col"| Cost
! scope="col"| LIBRARY
 
! scope="col"| PACKAGE
 
! scope="col"| Quantity
 
|-
 
! scope="row"| 1
 
| LED
 
| LED3MM
 
| SparkFun
 
| LED_3MM
 
| 1
 
|-
 
! scope="row" | 2
 
| Toggle Switch
 
| TOGGLE_SWITCH
 
| toggle_switch(Custom library) 
 
| TOGGLE_SWITCH(Custom Package)
 
| 2
 
|-
 
! scope="row" | 3
 
| Famale Connectors
 
| CONN_08"
 
| SparkFun-Connectors
 
| 1X08
 
| 2
 
 
|-
 
|-
! scope="row" | 4
+
! scope="row"| SJONE board
| Female Connectors
+
| LPC 1758
| 6_PIN_SERIAL_TARGET
+
|       80$
| SparkFun-Connectors
 
| 1X06
 
| 2
 
 
|-
 
|-
! scope="row" | 5
+
! scope="row"| LCD
| Male Connectors
+
| 5 inch TFT LCD screen
| CONN_17 
+
|       32$
| SparkFun-Connectors
 
| 1X17
 
| 4
 
 
|-
 
|-
! scope="row" | 6
+
! scope="row"| LCD driver
| Male Connectors
+
| RA8875
| CONN_02 
+
|       10$
| SparkFun-Connectors
 
| 1X02
 
| 6
 
 
|-
 
|-
! scope="row" | 7
+
! scope="row"| Camera
| Terminal headers
+
| OV 2640
| M023.5MM
+
|       20$
| SparkFun
 
| SCREWTERMINAL-3.5MM-2
 
| 2
 
|-
 
! scope="row" | 8
 
| Capacitors
 
| C-US025-025X050
 
| rcl
 
| C025-025X050
 
| 2
 
|-
 
! scope="row" | 9
 
| Resistors
 
| R-US_0207/10
 
| rcl
 
| 0207/10
 
| 1
 
|-
 
! scope="row" | 10
 
| Voltage Regulator
 
| V_REG_78XX-TO-220
 
| SparkFun-IC-Power
 
| TO-220-ALT
 
| 1
 
 
|-
 
|-
 +
! scope="row"| PCB
 +
|    -
 +
| 50$
 
|}
 
|}
  
====PCB Board Layout====
+
== Design & Implementation ==
 +
This design section outlines the hardware and software design of the project. 
  
As a second step, we worked on the PCB Board Layout. PCB board layout mainly involves placing components in the layout and routing them together. We can do manual routing or auto-routing to connect the components together with the copper lines. But sometimes auto-routing does not work or it will not be able to route all the connectors in an efficient manner. Hence, manual routing is the best way as it would allow us to plan and perform the routing.
+
=== Hardware Design ===
In our project, the main challenges we faced during PCB design was placing the connectors and components in the appropriate place. Since we used a motor driver shield which had 14 pin male connectors on the one side and 12 pin male connectors on the other side, it was very important that we measured the proper dimension to fit this board on the female connectors placed on the PCB. We used the dimension tool of Eagle to fix the accurate placement of the connectors.
 
The other challenge involved in the PCB design was routing the 34-pin connector for connecting the SJOne board to the PCB. We decided to leave an extra set of 34-pin connector on the PCB corresponding to SJOne board as it would provide us the flexibility to add any additional features to our project if decided at a later point.
 
 
 
In this process, we used Eagles' add-on features to check for any error in our design. It was important to follow all the strict PCB design guidelines before generating the Gerber files for PCB manufacturing.
 
  
 +
The main hardware components used in the project are discussed in detail below.
  
<div><ul>
+
''LCD Display:''
[[File:CmpE244 S17 CmpE244 S17 CamBot PCB Board.png|760px|thumb|center|PCB board design]]
+
This is a 5.0" TFT screen with 800x480 pixels. These screens are commonly seen in consumer electronics, such as miniature TV's, GPS's, handheld games car displays, etc. A 40-pin connector has 8 red, 8 green, and 8 blue parallel pins, for 24-bit color capability. The display is supposed to be constantly refreshed, at 60Hz, with a pixel clock. The backlight requires a constant-current mode boost converter that can go as high as 24V. Hence along with the display we use an RA8875 driver board.
[[File:CmpE244 S17 CamBot Top layer routing.png|415px|thumb|left|Top layer routing]]
 
<li style="display: inline-block;"> [[File:CmpE244 S17 CamBot PCB bottom layer routing.png|415px|thumb|right|Bottom layer routing]] </li>
 
</ul></div>
 
  
<br>
+
[[File:CMPE244_nemesis_l.jpg|thumb|400px|center|Figure 1: 5 inch LCD screen]]
The features of PCB include:
 
  
* Power supply circuit with 12V,5V and 3.3V connectors.
 
* Switch to control power to SJOne board and Switch to control power to Motor driver shield.
 
* Female connectors to place Adafruit Motor Driver shield.
 
* 34-pin male connectors to connect SJOne board to PCB.
 
* Extra 34-pin male connectors covering all 34 pins of SJOne board for future use or if any extra features were added.
 
  
 +
''LCD Driver :''
 +
The RA8875 is a powerful TFT driver chip, it has 768KB of RAM, so it can buffer the display (and depending on the screen size also have double overlaying).The RA8875 can also handle standard 4-wire over the SPI interface.
  
Overall, PCB design was a great learning experience and we would like to offer following tips to readers who are new to PCB design:
+
[[File:CMPE244_nemesis_ld.jpg|thumb|400px|Figure 2: RA8875 Driver|center]]
1. Follow Sparkfun Eagle tutorial or a similar one to learn the guidelines and rules for PCB design.
 
2. Try to select components from standard libraries like Sparkfun to make sure that we get the proper components according to the PCB specification.
 
3. It is better to select Through Hole components because it is easy to solder and is easily available in the market.
 
4. Give careful attention to components ratings and size because it will determine pad size for this components.
 
5. Carefully select the thickness of copper wire for routing. High current lines like supply line need thicker wires and GPIOs need thinner ones.
 
6. Use the dimension tool properly to place the components in the desired place.
 
7. Also, if it is a two-layer PCB, please make sure that there are fewer copper wires passing near the component pads in the back layer.It will make the soldering process easy without any damage to the PCB.
 
8. Before giving the PCB for manufacturing, please check with the guidelines provided by the vendors to make sure we meet all the PCB design rules.
 
 
  
 
<div><ul>
 
[[File:CmpE244 S17 CamBot PCB without Components.JPG|410px|thumb|center|PCB without Components]]
 
<br>
 
<li style="display: inline-block;"> [[File:CmpE244 S17 CamBot PCB with Components.JPG|435px|thumb|left|PCB with Components]] </li>
 
<li style="display: inline-block;"> [[File:CmpE244 S17 CamBot PCB with Motor Driver.JPG|415px|thumb|right|PCB with Motor Driver]] </li>
 
</ul></div>
 
  
=== Hardware Interface ===
 
  
The hardware components used in this project include:
 
  
* Motor Shield
+
''Camera:''
* Stepper Motor
+
The OV2640 Camera Module is a small image sensor, works at low operating voltages. It provides all functions of a single chip of VGA camera and image processor. Through Serial Camera bus control, the sensor can output the whole frame, sampling at various resolution 8 bits of data.  The product VGA image can reach up to a maximum of 30 frames per second. We can control the image quality, data format, and transmission mode. All the process of image processing functions can through the SCCB programming interface, including gamma curve, white balance, saturation, and chrome.
* DC Motor
 
* Bluetooth module.
 
  
 +
[[File:CMPE244_nemesis_c.jpg|thumb|300px|Figure 3: OV2640 camera|center]]
  
==== Motor Shield ====
+
 
 +
[[File:CMPE244_nemesis_Cam_Module.jpg|thumb|400px|center|Figure 4: Block diagram of the camera module]]
  
The Adafruit motor shield is used in this project.  
+
OV2640 support 4 output format: YcbCr422,YCbCr420, RGB565, Bayer raw RGB and GRB, YUV422 JPEG. How to choose the right output format for camera phone design or other applications? The data format at LCD driver are always RGB. For example, RGB444, RGB565, RGB555, RGB888 etc. The data format and memory interface are always JPEG. The JPEG data is compressed from YCbCr data. So Both RGB and YCbCr data are needed inside the back-end chip. The “X” block is different for different back-end chips.
  
[[File:CmpE244 S17 CamBot MotorShieldImage.jpg|400px|thumb|center|Adafruit Motor Shield]]
+
The general diagram of back-end chip is as below:
  
<br>'''Why use a motor shield?'''
+
[[File:CMPE244_nemesis_backend-cam.png|thumb|400px|center|Figure 5: Back-end chip ]]
* Stepper motor requires huge current. The micro-controller does not cater to this requirement. Motor shield IC is capable of handling such high currents.
 
* Since the stator coils behave as an inductor, a change in the direction of current results in huge spikes. This can damage the microcontroller. The H bridge can proetct the microcontroller in such a case.
 
  
<br>The Adafruit motor shield comprises of two L293D dual H-bridge motor driver ICs. It has four digital inputs which are in pairs. Each pair controls a motor. The motor shield has a shift register which facilitates the host controller to send data in serial manner which saves some of the pins of controller. The operation can be explained with an example. If pin 7 is set low and pin 2 is set high, motor 1 is turned on. Reversing the inputs will reverse the motor.
 
  
In order to conserve pins, the motor driver IC pins are wired to the outputs of 74HCT595N IC which is an 8-bit shift register. The data is fed in serially into this IC which then outputs the data in the parallel form.
+
'''System block diagram :''' The integral part of the entire system is the SJOne board which is the main controller for the LCD as well as the camera. Both the LCD and camera are interfaced via an SPI bus with the SJOne board. Apart from the power supply and ground pins, the LCD has one pins, namely, reset which is connected to the general purpose Input-Output pin of the controller. This pin is used while initializing the display.
  
The inputs to the shift register consisting of:
 
* DIR_EN: Enable Input (Active Low)
 
* DIR_SER: Data Input
 
* DIR_CLK: Rising edge results in shifting in of the input data bit
 
* DIR_LATCH: Rising edge results in internal bits to be moved out as parallel data
 
  
[[File:CmpE244_S17_CamBot_MotorShield.png|350px|thumb|center|Motor Shield Schematic]]
+
[[File:CMPE244_nemesis_block.png|thumb|800px|center|System Block diagram]]
  
 +
=== Hardware Interface ===
  
===== Motor Shield and SJOne Interface =====
+
The PCB design was done on Dip Trace, which is a CAD software for creating schematic diagrams and printed circuit boards. The SPI interface is done to both the camera and the driver board of the LCD display.
 
 
{| class="wikitable"
 
|-
 
! scope="col"| Motor Shield Pin
 
! scope="col"| SJOne Pin
 
|-
 
| 4 (CLK)
 
| P2.4
 
|-
 
| 7 (EN)
 
| P2.5
 
|-
 
| 8 (DATA)
 
| P2.6
 
|-
 
| 12 (LATCH)
 
| P2.7
 
|-
 
|}
 
 
 
==== Stepper Motor ====
 
  
Stepper motor is used to obtain the rotation of the phone for capturing a time-lapse.
+
The PCB was designed in such a way that it can be powered by two sources, USB from PC or from PowerBank, by flipping a switch. Using this feature SJONE can be directly connected to PC via the PCB without any direct connection from PC to SJONE, for easier deployment of code once the whole project was packaged inside the camera box.
  
Stepper motors consist of a rotor and stationary electromagnets around the rotor. The movement of the rotor is controlled by energizing the electromagnets in a particular sequence as required. The movement of the rotor occurs as the rotor is attracted towards the electromagnets.
 
  
[[File:CmpE244 S17 CamBot StepperMotor.gif|500px|thumb|center|Stepper Motor Functionality]]
+
[[File:CMPE244_nemesis_Schemetic.jpeg|thumb|1000px|Schematic|center]]
  
  
In this project, we use the NEMA 17 stepper motor which has a precision of 200 steps per revolution. Based on the angle required, the stepper motor can be configured to take the required number of steps in the provided direction.
 
NEMA 17 is a bipolar stepper motor.  In the case of bipolar stepper motor, in order to reverse the polarity of stator poles, the current needs to be reversed. It is a 2-phase stepper motor and has one winding per phase. It has four leads. Each pair of leads is connected to one winding.
 
  
 +
[[File:CMPE244_nemesis_pcb.jpeg|thumb|1000px|PCB Design|left]]
  
[[File:CmpE244 S17 CamBot NemaStepperMotor.jpg|300px|thumb|left|NEMA 17 Stepper Motor]]
+
[[File:Cmpe244_S18_NEMESIS_PCB_3D.jpeg|thumb|500px|3D view of PCB Design|right]]
[[File:CmpE244 S17 CamBot StepperMotorInternals.jpg|300px|thumb|center|Stepper Motor Windings]]  
 
<br>
 
  
<br>'''Verifying the leads for proper connection'''
 
<br>Each pair corresponds to one winding.
 
* If the resistance measured between two wires of the lead is infinite, the two wires belong to different windings.
 
* If the resistance measured between two wires is a definite value, the two wires in the pair belong to the same winding.
 
<br>
 
Each pair of these leads is connected to the motor input pins of the Motor Shield.
 
<br>
 
===== Stepper Motor and Motor Shield Interface =====
 
  
{| class="wikitable"
 
|-
 
! scope="col"| Stepper Motor
 
! scope="col"| Motor Shield
 
|-
 
| 2A and 2B
 
| M2
 
|-
 
| 3A and 3B
 
| M3
 
|-
 
|}
 
  
===== SJOne PWM for Stepper Motor =====
 
  
{| class="wikitable"
 
|-
 
! scope="col"| Stepper Motor
 
! scope="col"| SJOne Pins
 
|-
 
| 2A and 2B
 
| P2.0 (PWM1)
 
|-
 
| 3A and 3B
 
| P2.3 (PWM4)
 
|-
 
|}
 
  
==== DC Motor ====
 
  
DC motor is used in this project to move the robot forward and backward. Two DC motors are used each connected to a wheel.
 
  
The DC motor converts electrical energy to mechanical energy. It consists of a current carrying conductor placed in an electromagnetic field. When current is passed through the conductor, a mechanical force is generated due to the electromagnetic effect.
 
  
[[File:CmpE244 S17 CamBot DCMotorFunctionality.gif|400px|thumb|center|DC Motor Functionality]]
 
  
===== DC Motor and Motor Shield Interface =====
 
  
{| class="wikitable"
+
 
|-
 
! scope="col"| DC Motor
 
! scope="col"| Motor Shield
 
|-
 
| DC Motor 1
 
| M1
 
|-
 
| DC Motor 2
 
| M4
 
|-
 
|}
 
  
===== SJOne PWM for DC Motor =====
 
  
{| class="wikitable"
 
|-
 
! scope="col"| DC Motor
 
! scope="col"| SJOne Pins
 
|-
 
| DC Motor 1
 
| P2.1 (PWM2)
 
|-
 
| DC Motor 2
 
| P2.2 (PWM3)
 
|-
 
|}
 
  
<br>
 
  
==== Bluetooth Module ====
 
  
[[File:CmpE244_S17_cambot_RN42XV_Bluetooth_Module.jpg‎|right|200px|thumb|RN42XV Bluetooth Module]]
 
  
RN42XV is built around Microchip's RN42 low power Bluetooth module. Some features of this module are as follows
 
  
*Based on the popular 2 x 10 (2mm) socket footprint.
 
*Voltage range: (3-3.6)Volts
 
*Current range: 26 μA sleep, 3 mA connected, 30 mA transmit.
 
*UART data connection interface.
 
*Sustained data rates: 240 Kbps (slave), 300 Kbps (master)
 
*Transmission range up to 60 feet (20 m) distance, +4 dBm output transmitter, -80 dBm typical receive sensitivity.
 
*FHSS/GFSK modulation, 79 channels at 1-MHz intervals.
 
  
The Bluetooth module uses simple UART communication protocol for sending/receiving data from the Micro-controller board.
 
we are using UART2 to communicate with the Bluetooth module.
 
  
[[File:CmpE244_S17_cambot_Connections_Bluetooth_Module.jpg|center|400px|thumb|Hardware Interface of Bluetooth Module]]
 
  
<br>
+
 
=== Software Design ===
 
  
The software design consists of 3 tasks:
 
* Bluetooth Task
 
* Stepper Motor Task
 
* DC Motor Task
 
  
The Bluetooth task is of high priority since the user data needs to be received and acted upon. The Stepper Motor and the DC Motor tasks are run with medium priority.
 
  
  
==== Algorithm ====
 
* In the idle state, the DC Motor and Stepper Motor tasks wait for command from the bluetooth application.
 
* Application commands are received using the Bluetooth task which performs UART data send and receive functionalities.
 
* The application sends:
 
:* Direction for movement (forward or backward) to the DC Motor.
 
:* Direction for rotation (clockwise or anticlockwise) and angle of rotation to the Stepper Motor.
 
* Based on the data received, the motors are configured.
 
:* DC Motor: PWM is provided from the SJOne board which is routed to the DC motor through the Motor Shield.
 
:* Stepper Motor: Specific bits are latched into the Motor Shield in order to energize the coil in the required sequence.
 
::::::PWM for stepper motor is configured using SJOne board. The rotation is configured as step angles based on the angle provided.
 
* The Start and Stop commands from the application are used to trigger or stop the motors.
 
  
  
==== Flowchart ====
 
[[File:CMPE244 S17 CamBot FlowChart.png|700px|thumb|center|FlowChart]]
 
<br>
 
  
=== Implementation ===
 
  
The software implementation can be divided across the following modules:
 
* Motor Shield
 
* Stepper Motor
 
* DC Motor
 
* UART interface
 
* Android Application Development
 
<br>
 
  
==== Motor Shield Software  ====
 
  
The shift register in the motor shield requires 4 inputs. They are configured using SJOne GPIOs as below:
 
  
<pre>
+
 
GPIO MOTORLATCH(P2_7);
 
GPIO MOTORDATA(P2_6);
 
GPIO MOTORCLK(P2_4);
 
GPIO MOTORENABLE(P2_5);
 
  
MOTORLATCH.setAsOutput();
 
MOTORENABLE.setAsOutput();
 
MOTORDATA.setAsOutput();
 
MOTORCLK.setAsOutput();
 
</pre>
 
  
Latch enabling is required every time a data is set since it controls the data moving into the shift register and the parallel data being sent out of the motor shield.
 
This functionality of latching motor state data is implemented as follows:
 
  
<pre>
 
for(i=0; i<8; i++)
 
{
 
    setMotorClockLow();
 
   
 
    if(latch_state & (1 << (7-i))
 
    {
 
          setMotorDataHigh();
 
    }
 
    else
 
    {
 
          setMotorDataLow();
 
    }
 
    setMotorClkHigh();
 
}
 
setMotorLatchHigh();
 
</pre>
 
  
  
==== Stepper Motor Task  ====
 
  
'''PWM Initialization:'''
 
  
PWMs 1 and 4 are used for Stepper Motor.
 
  
<pre>
 
GPIO PWM1(P2_0);
 
GPIO PWM4(P2_3);
 
  
PWM1.setAsOutput();
+
 
PWM4.setAsOutput();
 
</pre>
 
  
<br>
+
=== Software Design and Flowchart ===
'''Latching:'''<br>
 
Latching is required since the input is taken serially by the Motor Shield and rendered as parallel output. This design aims at reducing the number of pins.<br>
 
In order to enable the stepper motor, the coils need to be energized in a particular sequence based on requirement. Energizing each of the coils is controlled by using a bit to represent the state of each coil. This bit is latched into the latch_state variable. This data is used to enable the energizing of the required coil.
 
  
<br>
+
The general flow of our project can be explained by the flowchart shown below:
The Stepper motor coils are energized in the following sequence based on the command received from the application:
 
<br>
 
'''Latching for clockwise direction:'''
 
<pre>
 
latch_state |= (1 << MOTOR3_A) | (1 << MOTOR2_A);
 
latch_state |= (1 << MOTOR2_A) | (1 << MOTOR3_B);
 
latch_state |= (1 << MOTOR3_B) | (1 << MOTOR2_B);
 
latch_state |= (1 << MOTOR2_B) | (1 << MOTOR3_A);
 
</pre>
 
This sequence results in clockwise rotation.
 
  
  
'''Latching for anti-clockwise direction:'''
+
[[File:CMPE244_Nemesis_Flowchart.jpg|thumb|600px|center|Program Flow]]
<pre>
 
latch_state |= (1 << MOTOR2_B) | (1 << MOTOR3_A);
 
latch_state |= (1 << MOTOR3_B) | (1 << MOTOR2_B);
 
latch_state |= (1 << MOTOR2_A) | (1 << MOTOR3_B);
 
latch_state |= (1 << MOTOR3_A) | (1 << MOTOR2_A);
 
</pre>
 
  
This sequence results in anti-clockwise rotation.
+
=== Implementation ===
 +
The camera is interfaced to the board where Image Sensor Configuration is done through I2C Protocol and Sending pixel data through SPI communication protocol. The raw pixels are read from the camera to the SJONE board. There are five buttons on the camera. One of them used to capture an image and store it on the SD card. The LCD screen is interfaced through SPI communication protocol. The raw pixels received from the camera are displayed on to the LCD screen.
  
<br>
+
===  Algorithm: ===
  
==== DC Motor Task  ====
+
The first step for making the LCD and the camera work is the initialization of these components. The algorithm for it is as described:
  
'''PWM Initialization:'''  
+
'''1. Initialization of the LCD: '''  
 +
* According to the desired resolution(as the LCD driver supports two resolutions 800x480 & 480x272), the resolution is first set and the basic touch.non-touch config is made. In our case the LCD is non-touch.
 +
* After setting the chip-select and reset pins as outputs, the SPI is initialized. SSP1 was configured to be used for LCD interfacing.
 +
* Register at address 0x00 is read for getting the version of the LCD over SPI.
 +
* PLL is initialized and the HSYNC, VSYNC and Pixel clock([https://www.nxp.com/wcm_documents/techzones/microcontrollers-techzone/Presentations/graphics.lcd.technologies.pdf terminologies described here]) parameters are set according to the resolution chosen. (as per the [https://cdn-shop.adafruit.com/datasheets/RA8875_DS_V19_Eng.pdf RA8875 ADafruit Datasheet])
 +
* Active window X and window Y are set by writing to the appropriate registers.
 +
* Clear the entire window.
 +
* Set SPI clock frequency.{There's a [https://github.com/saurabhrb/CMPE_244_Project_CAM/blob/master/FINAL%20DEMOed%20Code/LCD.cpp#L87 trick] in this as well, the adafruit RA8875 first responds to 2MHz SPI only, and after initializing the PLL and other timing related configs, it listens to up to 30MHz SPI}
  
PWMs 2 and 3 are used for DC Motor.
+
'''2. Initialization of the Camera: '''
 +
* I2C is initialized for Image Sensor Configuration, this configuration was retrieved from the [Software notes of OV2640 Camera https://github.com/saurabhrb/CMPE_244_Project_CAM/blob/master/OV2640_fifo/OV2640%20Camera%20Module%20Software%20Application%20Notes.pdf].
 +
* SPI is initialized with the desired frequency(only up to 8MHz) for receiving the pixel data from the buffer.
 +
* According to the resolution, appropriate I2C sensor registers are set later.
  
<pre>
 
GPIO PWM2(P2_1);
 
GPIO PWM3(P2_2);
 
  
PWM2.setAsOutput();
 
PWM3.setAsOutput();
 
</pre>
 
  
The DC motor is activated based on the command received from the application:
+
In Terms of Pseudo Code & Algorithm, the following is the methodology we followed which consists of API's and its functions. The design is mainly of two parts:
  
'''Latching for forward direction:'''
 
<pre>
 
latch_state |= (1 << MOTOR1_A);
 
latch_state &= ~(1 << MOTOR1_B);
 
  
latch_state |= (1 << MOTOR4_A);
+
'''1. Design for LCD''' :
latch_state &= ~(1 << MOTOR4_B);
+
LCD would continuously display a menu on its right with different options based on the currently selected menu. Menu driver is programmed dynamically using pointers & function Pointer to create sub-menus and sub-items inside sub-menus. A main menu object pointer is created in the main function which is propagated along the menu-tree to choose which should be the menu which needs to be displayed on the LCD currently.
</pre>
 
This sequence results in a forward motion.  
 
<br>
 
<pre>
 
latch_state |= (1 << MOTOR1_B);
 
latch_state &= ~(1 << MOTOR1_A);
 
  
latch_state |= (1 << MOTOR4_B);
+
Various API & functions as follows:
latch_state &= ~(1 << MOTOR4_A);
 
</pre>
 
This sequence results in a backward motion.
 
  
<br>
+
* ''Add_Menu'' API, whenever gets called would add one more node to the linked list.
  
==== Bluetooth Task  ====
+
* ''Add_Submenu'' API would create Linked List inside that Node.  
The bluetooth task initializes UART2 for the communication with Zigbee Bluetooth module with the baud rate of 115200. It waits for a byte from UART2 in its run() function (which is always run in a loop as long as the task is running). The byte it receives can be decoded from the enumeration datatype declared to represent the commands from the app. Below is the definition of the Bluetooth task used along with the enum datatype.
 
<pre>
 
class bluetooth_task : public scheduler_task
 
{
 
private:
 
Uart2 &Bluetooth_uart_2;
 
static const int Rx_Q_Size = 3;
 
static const int Tx_Q_Size = 3;
 
static const int Baud_Rate_Bluetooth_Uart2 = 115200;
 
char buff;
 
public:
 
bluetooth_task(uint8_t priority) :
 
scheduler_task("Bluetooth Task", 2000, priority),Bluetooth_uart_2(Uart2::getInstance())
 
        {
 
Bluetooth_uart_2.init(Baud_Rate_Bluetooth_Uart2,Rx_Q_Size,Tx_Q_Size);
 
buff = 0;
 
        }
 
  
bool init(void);
+
* "Navigate" API enables a user to navigate through Menus. This API navigates pointer to different nodes and calling respective functions such as capture image or going inside sub_menu.
bool run(void *p);
 
};
 
  
enum command
+
[[File:Cmpe244_S18_NEMESIS_LCD_MENU_DRIVER.jpg|thumb|600px|LCD Menu working|center]]
{
 
  init,
 
  start,
 
  stop,
 
  clockwise,
 
  anticlockwise,
 
  angle30,
 
  angle60,
 
  angle90,
 
  angle120,
 
  angle150,
 
  angle180,
 
  forward,
 
  backward,
 
  
};
+
With this dynamic functionality, we can create menus as required inside which we can call the respective function to be performed according to the submenu.
</pre>
 
  
<br>
 
  
==== Control of Stepper and DC Motor ====
 
Stepper Motor steps can be controlled based on input from Android Application.
 
To rotate the stepper motor, it has to go through phase 1 to 4 to energize the 4 coils in particular sequence based on desired angle and direction.
 
Similarly, DC motor movement is controlled in forward or backward direction.
 
  
Structure has all the necessary control variables as below
+
''Psuedo Code for LCD_Menu - ''
  
<pre>
+
A menu was designed for the LCD screen using an LCD_MENU class. This class included functions like adding sub-menu, displaying them or loading previous ones. It helped in dynamically adding options for the innumerous camera settings like changing image resolution and format.The private members of this class are as shown:
struct motor_state
 
{
 
    uint8_t direction;
 
    uint8_t fwd_reverse;
 
    uint8_t start_stop;
 
    uint8_t steps_degree;
 
    bool steps_set;
 
    bool angleinfinite;
 
    int8_t value;
 
}</pre>
 
  
Based on the values sent from application,
+
    private:
* 'direction' is set to clockwise/anticlockwise for Stepper Motor
+
    type myType;                        //to know if current object is itemType or menuType
* 'fwd_rev' to control the direction for DC Motor
+
    str myName;                          //name of this object
* 'start_stop' to start or stop the motor
+
    LCD_MENU *ParentMenu;                //parent menu of current menu object
* 'steps_degree' to control the desired amount of rotation that needs to be achieved by the Stepper Motor
+
    LCD_MENU *CurrentMenu;              //reference to global CurrentMenuG
* 'steps_set' to track whether an angle is set for the stepper motor to move
+
    VECTOR<Element> all_elements;        //to store all elements by Element .name .id and .type
* 'angleinfinite' to continuously rotate the stepper motor shaft
+
    VECTOR<str> all_elements_name;      //to store all elements by name to use by Find function
* 'value' to control the direction of energizing the motor for clockwise and anticlockwise direction
+
    int highlighted;                    //to track currently highlighted sub-element
  
==== Android Application ====
+
Similar to this class, a file_explorer class was implemented for navigating through the SD card files.
  
[[File:CmpE244_S17_cambot_App_Screenshot.png‎|right|220px|thumb|Android Application UI]]
 
  
The android application to control our CamBot is a simple one screen application. The screenshot showing the button layout (UI) is given on the right. The table with the button descriptions is given below. Any button press will result in sending of the command byte via Bluetooth to board module which will be received via UART. The Bluetooth task and android app communication will receive all and any message sent. The motor task will decide what to do with the received commands and whether it is valid or not, with respect to the software flow.
 
<br>
 
<br>
 
'''Steps involved in the Android Application'''
 
  
* The android code starts at MainActivity.java where an UUID is initialized for the Bluetooth adapter objects created in the MainActivity class.
+
'''2. Design for SJOne & CAM''' :
* After this, a Bluetooth socket is created using the above mentioned UUID.
+
The controller has to Configure camera, receive data from the camera and send this to LCD as soon as it gets the data.  
* A list is created which is initialized with the paired devices of the Application host.
 
* Clicking on any of these would start a connection with the device ID via the socket object.
 
* After connection is established, any command sent would be done so via this created socket object.
 
* The next function call would be based on what button is pressed.
 
* There are onClickListeners for all these buttons which would send the respective commands on button press.
 
  
<br>An important point to note here is that the app uses the MAC address (like 00:06:66:6E:09:D6 for the device RNBT-09D6) to connect. Hence before connection thread is started, you need to give the device MAC address to the Bluetooth socket. You can either hardcode the address in the program, or as we have implemented, use an OnClickListener to check which device was selected through touch, retrieve its respective MAC address from the created list and then give it to the Bluetooth socket.
+
Function includes :
  
The android UI can be designed in two ways. One is purely through GUI, where you can drag and drop the required items like a button or text field. Another way is through text where you manually type in the parameters for the UI object. The way these objects are placed are described through their layout. You can choose between two kinds of layouts: Constraint Layout and Relative Layout. A relative layout is where you define the position of the UI objects with respect to the screen edges. A constraint layout is similar to a relative layout but with the additional availability of a bias where you can position it by adding more rules by adding constraints like the distance between two buttons etc. We have used Relative Layout to design the UI of our app.
+
* Configuration of Camera Module by configuring different registers specified in datasheet & setting image format.
  
{| class="wikitable"
+
* To receive Data from Camera at 8 MHz through SPI. After receiving One Frame, it is sent to LCD via SPI. In both the case, Controller is Master and another device is a slave.
|-
 
! scope="col"| Button
 
! scope="col"| Functionality
 
|-
 
| TURN ON
 
| Turn on the Bluetooth on the mobile
 
|-
 
| TURN OFF
 
| Turn off the Bluetooth on the mobile
 
|-
 
| GET VISIBLE
 
| Makes your mobile device visible to other Bluetooth devices
 
|-
 
| LIST DEVICES
 
| Lists all the Bluetooth devices paired with the mobile. You can click on any of the listed devices to connect to it
 
|-
 
| START
 
| After giving appropriate commands and parameters, pressing this button will set the cambot in motion
 
|-
 
| STOP
 
| This stops the motion of the CamBot
 
|-
 
| ANTICLOCKWISE
 
| Press this button to set the direction of rotation of the camera holder stepper motor to anticlockwise direction
 
|-
 
| CLOCKWISE
 
| Press this button to set the direction of rotation of the camera holder stepper motor to clockwise direction
 
|-
 
| FORWARD
 
| Press this button to set the direction of movement of the dc motor to forward direction
 
|-
 
| BACKWARD
 
| Press this button to set the direction of movement of the dc motor to backward direction
 
|-
 
| SET ANGLE
 
| First select appropriate angle for the above discussed stepper motor using the list to the left of this button. Then press this button to set the angle of rotation of the stepper motor
 
|-
 
|}
 
  
== Testing & Technical Challenges ==
+
* To Store Image in SD card, the whole frame is sent to SD card through SPI, and a new text file with a unique name is created and pixel values in that text file would be ASCII characters coming from 16-bit image buffer.
  
 +
== '''Testing & Technical Challenges''' ==
  
===Testing===
 
  
====Bluetooth Module - Android App====
+
''' Problem 1: ''' Making any camera work on SJOne can be little tricky, due to its less RAM to store one full B/W or RGB frame. Also interfacing the regular CMOS camera modules with Pixel Clks requires perfect timing to get a proper or any image at all.
  
We tested the effective communication between the android app on smartphone and the SJOne Board with Zigbee Bluetooth module by just running the Bluetooth task on SJOne board which waits for data from Bluetooth module and prints the data whenever it receives it. With this, we were able to do regression testing of all button presses without any regard to the functional flow for the order of button press. All button presses generated and sent the command data to the board and were successfully printed.
+
''' Solution: ''' The solution was to use the already buffered and stored camera frame and then transfer it one by one based on SJONE's reading speed(via SPI/I2C) to display it on the LCD pixel by pixel. After trying a number of cameras like OV7670 without fifo and VC0706 serial camera(which had unsupported output format), we were able to solve the problem by using OV2640 SPI camera with fifo buffer which made capturing and retrieving the image easier because the buffer saved the whole frame, and cleared only after SJONE has finished reading the frame.
  
====Stepper motor Control using App====
+
''' Problem 2: ''' The camera displayed two frames on the LCD screen for resolutions greater than 352x288. This was because the buffer size of the camera could only hold upto 16bits pixels of max 352x288 resolution, as the buffer size is [http://www.arducam.com/arducam-mini-released/ ~384KBytes].
  
The stepper motor was tested first by manually sending signals from SJOne board. Then we switched over to control it using the Android app by removing all other modules and checking if the angle and direction that was given by the app was received by the board and decoded (by printing the received command on the terminal). Then, we verified that the direction of rotation of the mount on the stepper motor and the angle it rotated was as per the command provided through the app.
+
''' Solution: ''' This could be avoided by upgrading the buffer size on the camera module or by using an external buffer to store the whole image before previewing on LCD.
  
====DC Motor control using App====
+
''' Problem 2: ''' The camera was displaying image in form of random pixel values.
  
This testing procedure followed was similar to the testing of the stepper motor where we first tested the motor standalone by signalling from the board and then proceeded to test using the control from the Android app.
+
''' Solution: ''' After taking a closer look at datasheet we realized that we were configuring the CMOS registers for wrong/unsupported format (by LCD) and not RGB565 format. After configuring the registers for RGB565 with different resolution types, the LCD displayed proper images.
  
====Overall Testing====
+
== '''Conclusion''' ==
  
We tested the final assembled CamBot by connecting the board with Android app and sending commands for Stepper motor and DC motor and then pressing Start. The CamBot proceeds to move as commanded and comes to halt when the Stop command is given from the app. We also checked the stability of the mount and vibrations encountered during motion and made some mechanical adjustments to reduce the vibrations.
+
We have successfully designed and implemented a digital camera with a number of features. This project has helped us gain insight knowledge of the implementation of FreeRTOS concepts like prioritizing tasks and using semaphore and mutex for SD card reading and writing. We learned to implement communication protocol drivers such as SPI. Working with pixel data helped us go into deeper details of the SPI protocol as each byte had to be handled accurately. We have tried the PCB design on both Eagle and Dip Trace, we were able to complete the PCB design starting from schematic diagram to 3D imaging of the PCB. We were also able to learn the CAD design required for 3D printing.  
<br>
 
<br>
 
===Technical Challenges===
 
  
==== Issue 1: Mounting camera on the Stepper Motor ====
+
Apart from the technical aspects of the project, a sense of cordial teamwork was also developed amongst the team members which led to the successful accomplishment of the project's objectives.
  
We faced an issue while placing the camera mount on the stepper motor. A coupler was used to connect the stepper motor shaft and the camera mount. We tried many couplers but there always existed a small mismatch on either at the camera mount end or the stepper motor shaft end. This resulted in amplification of even a small vibration during stepper motor rotation resulting in severe vibration of the camera mount when the stepper motor was activated. The mount also consisted of several moving parts which added to the issue. This was a critical issue as camera needs stable platform to capture the time-lapse.
+
=== Project Video ===
 
 
'''Solution:'''
 
We bought couplers suiting the motor shaft diameter and then tried several screw sizes and washers to obtain a stable mount. We adjusted the coupler placement on the motor shaft for minimal shakiness. Also, we replaced the initial mount which had several moving parts with a mount which a single solid piece.
 
 
 
==== Issue 2: Connecting Android Application with Zigbee module  ====
 
We had a problem connecting the android app with Zigbee module via Bluetooth. The app was initialising connection but was not able to finish the connection process and get connected.
 
  
'''Solution:'''
+
*  [https://youtu.be/GtHGJm6FQZo SJSU_CAM Video Link]
The RFComm socket was not getting created properly. Even when the RFComm socket was created, it was not created with the accurate device UUID that needed to be setup. After manually setting up the device UUID and then creating the RFComm socket, we could start the Bluetooth connection with that socket object and then it would successfully connect with the Bluetooth module.
 
 
 
==== Issue 3: Toggle Switch Selection during PCB design ====
 
 
 
We faced problem in choosing a small SPST toggle switch in Eagle library during PCB schematic design as the required specification and dimension was not available in the library.
 
 
 
'''Solution:'''
 
We decided and developed our own customized switch using Eagle with the required dimension and specification that fit our board perfectly.
 
 
 
== Conclusion ==
 
The CamBot project taught us several aspects, both in terms of technical knowledge and project management. We went through design, development and testing phases. We gained knowledge regarding the hardware interfaces and FreeRTOS programming. Working on the motor interfaces, we had to reverse engineer the motor shield connections due to the lack of datasheets. We worked on Android application and bluetooth interface which was new and exciting for us. We had the opportunity to learn the application and driver interface. An important learning from this project was the difficulty involved in hardware assembly. The project involved mounting the camera on the stepper motor. We realized how a minor vibration at the coupler used to connect the stepper motor and the camera mount could amplify at the camera mount. It would not be an exaggeration to say that we did spend a considerable amount of time fixing this. The suggestion that we would like to provide to all teams planning on projects that involve a lot of hardware assembly is to start early and test tremendously. We had fun designing the PCB for our project. We enjoyed the extensive learning that the process imparted. The exhaustive lectures and the challenging assignments that had helped us understand the FreeRTOS concepts aided in the effective design of our software flow. CamBot made us understand how to work in a team and our mistakes taught us an important lesson of time management. Overall, it was a great learning experience.
 
 
 
=== Project Video ===
 
CamBot Demo Video: https://www.youtube.com/watch?v=lykHS3HZx9c&feature=youtu.be
 
  
 
=== Project Source Code ===
 
=== Project Source Code ===
 
+
*  [https://github.com/saurabhrb/CMPE_244_Project_CAM SJSU_CAM Github Link]
https://gitlab.com/aajna.karki/cmpe244_CamBot
 
  
 
== References ==
 
== References ==
 
=== Acknowledgement ===
 
=== Acknowledgement ===
We would like to thank '''Professor Preetpal Kang''' for providing a great platform towards learning Real-Time Operating Systems. The well-modularized course enabled us to understand and implement the concepts of FreeRTOS. We learnt significantly while working on our project. We would like to express our gratitude for his continued guidance throughout the semester. Also, we appreciate the Student Assistants of this course for their immense support and the knowledge shared.
+
We would like to thank our professor, Preetpal Kang for designing such a course where learning was his guidance and consistent feedback. We would also like to thank the ISA team for their valuable inputs as per their own experience of the course when taken and their support and motivation at every phase of this project. There were some unsung, selfless classmates who helped us whenever necessary, whenever any immediate hardware was required or any other guidance needed in spite of being from the rival teams. We would like to thank each one of them who forgot their differences or considered the course and project as a competition to selflessly help us when in need.
  
 
=== References Used ===
 
=== References Used ===
[1] Preetpal Kang's lecture notes of CMPE 244, Computer Engineering, San Jose State University, Feb-May 2017
+
[1] Preetpal Kang's lecture notes of CMPE 244, Computer Engineering, San Jose State University, Jan-May 2018.
[2] https://docs.google.com/document/d/1OdGS0QHKCdbrbAs_x62gmfSx_UdUPFPgZjwZnVDVHDg/preview
+
 
[3] https://developer.android.com/training/basics/firstapp/building-ui.html
+
[2] [http://www.arducam.com/camera-modules/2mp-ov2640/ OV2640 Technical Description]
[4] https://www.tutorialspoint.com/android/android_bluetooth.htm
+
 
[5] https://learn.sparkfun.com/tutorials/tags/eagle
+
[3] [https://www.adafruit.com/product/1680 5.0" LCD & Datasheet]
 +
 
 +
[4] [https://cdn-shop.adafruit.com/datasheets/KD50G21-40NT-A1.pdf/ RA8875 LCD Driver Description]

Latest revision as of 18:20, 27 May 2018

SJSU Cam

SJSU_CAM

Abstract

This project records live stream through camera and displays it on LCD through SJ One microcontroller which is LPC 1758. This camera has longer battery as it runs through portable battery powering microcontroller. As there are many High Quality Cameras available in market, purpose of this camera is found in applications where streaming of medium quality video/photo is required for longer period of time and camera has to survive longer on single battery, which can’t be provided by handy digital camera. Another functionality provided by this project is to enable user to capture images on SD card and view it on LCD providing different resolution and modes as option through interactive UI designed on LCD. Project Hardware enclosed in small box 3D designed for this project enables portability and ease of access to user.

Objectives & Introduction

The objective of this project was to learn how timing and buffer play an important role in capturing the pixels from the camera and displaying them on the LCD screen. The following objectives were set for the project:


1. Write a driver for the SPI camera ov2640, which has a better rate of capturing the image with the SJONE board.
2. Write the driver for the LCD display (Adafruit 5' TFT) and integrate it by choosing the right resolution. <vr/>3. Implement a menu class for different options of the camera on the LCD and program external buttons for navigating across the menu.
3. Integrate the camera and LCD by displaying the output of the camera on the LCD.
4. Store the output of the camera on the SD card, if capture mode is selected from LCD menu.
5. Read the image from the SD card and display on the screen.


Team Members & Responsibilities

  • Chhavi Mehta
    • Camera implementation, integration with SD card
  • Deeksha Prakash Kankalale
    • PCB Design, Camera driver implementation.
  • Parth Gujar
    • LCD Driver, 3D design.
  • Sagar Kalathia
    • LCD interfacing, implementing graphics library
  • Saurabh Badenkal
    • LCD Driver, Integration of camera and LCD and data pipelining.

Schedule

Week# Date Task Actual
1 4/17 1. Placing order for the components required in the project.
2. Division of module development.
Completed!
Problems Encountered: Finding the right components after going through various datasheets.
2 4/24 1. Making interface with LCD.
2. Drawing simple shapes.
3. Work on the overall design of the menu for LCD.
Completed!
Problems Encountered: Setting appropriate initial frequency for the LCD to light up.
3 5/1 1. Interfacing the camera
2. Implementing menu class with sub-menus and items.
3. Designing hardware prototype for 3D printing.
Completed!
Problems Encountered: Not enough RAM for displaying more than 1 picture of size 100x100, Using interrupts for UART receive instead of normal function call.
4 5/8 1. Design PCB and place order.
2. Integrating file explorer of the SD card onto LCD with GUI.
3. Capturing image from the camera, saving it on the SD card.
Completed!
Problems Encountered: Heavy resource utilization while saving and retrieving the file from SD card.
5 5/15 1. Displaying the image on the LCD.
2. Integrating the modules together with the 3D case having the LCD on one side and SJONE and camera on the other with buttons.
Completed!
Problems Encountered: Resource constraints only allow a resolution of 320p. Extremely low frame rate due to SD read speeds.
6 5/22 1. Final testing and debugging of the system as a whole. Completed!
Problems Encountered: PCB arrived late.

Parts List & Cost

Components Model Number Cost
SJONE board LPC 1758 80$
LCD 5 inch TFT LCD screen 32$
LCD driver RA8875 10$
Camera OV 2640 20$
PCB - 50$

Design & Implementation

This design section outlines the hardware and software design of the project.

Hardware Design

The main hardware components used in the project are discussed in detail below.

LCD Display: This is a 5.0" TFT screen with 800x480 pixels. These screens are commonly seen in consumer electronics, such as miniature TV's, GPS's, handheld games car displays, etc. A 40-pin connector has 8 red, 8 green, and 8 blue parallel pins, for 24-bit color capability. The display is supposed to be constantly refreshed, at 60Hz, with a pixel clock. The backlight requires a constant-current mode boost converter that can go as high as 24V. Hence along with the display we use an RA8875 driver board.

Figure 1: 5 inch LCD screen


LCD Driver : The RA8875 is a powerful TFT driver chip, it has 768KB of RAM, so it can buffer the display (and depending on the screen size also have double overlaying).The RA8875 can also handle standard 4-wire over the SPI interface.

Figure 2: RA8875 Driver



Camera: The OV2640 Camera Module is a small image sensor, works at low operating voltages. It provides all functions of a single chip of VGA camera and image processor. Through Serial Camera bus control, the sensor can output the whole frame, sampling at various resolution 8 bits of data. The product VGA image can reach up to a maximum of 30 frames per second. We can control the image quality, data format, and transmission mode. All the process of image processing functions can through the SCCB programming interface, including gamma curve, white balance, saturation, and chrome.

Figure 3: OV2640 camera


Figure 4: Block diagram of the camera module

OV2640 support 4 output format: YcbCr422,YCbCr420, RGB565, Bayer raw RGB and GRB, YUV422 JPEG. How to choose the right output format for camera phone design or other applications? The data format at LCD driver are always RGB. For example, RGB444, RGB565, RGB555, RGB888 etc. The data format and memory interface are always JPEG. The JPEG data is compressed from YCbCr data. So Both RGB and YCbCr data are needed inside the back-end chip. The “X” block is different for different back-end chips.

The general diagram of back-end chip is as below:

Figure 5: Back-end chip


System block diagram : The integral part of the entire system is the SJOne board which is the main controller for the LCD as well as the camera. Both the LCD and camera are interfaced via an SPI bus with the SJOne board. Apart from the power supply and ground pins, the LCD has one pins, namely, reset which is connected to the general purpose Input-Output pin of the controller. This pin is used while initializing the display.


System Block diagram

Hardware Interface

The PCB design was done on Dip Trace, which is a CAD software for creating schematic diagrams and printed circuit boards. The SPI interface is done to both the camera and the driver board of the LCD display.

The PCB was designed in such a way that it can be powered by two sources, USB from PC or from PowerBank, by flipping a switch. Using this feature SJONE can be directly connected to PC via the PCB without any direct connection from PC to SJONE, for easier deployment of code once the whole project was packaged inside the camera box.


Schematic


PCB Design
3D view of PCB Design






 






 






 





 

Software Design and Flowchart

The general flow of our project can be explained by the flowchart shown below:


Program Flow

Implementation

The camera is interfaced to the board where Image Sensor Configuration is done through I2C Protocol and Sending pixel data through SPI communication protocol. The raw pixels are read from the camera to the SJONE board. There are five buttons on the camera. One of them used to capture an image and store it on the SD card. The LCD screen is interfaced through SPI communication protocol. The raw pixels received from the camera are displayed on to the LCD screen.

Algorithm:

The first step for making the LCD and the camera work is the initialization of these components. The algorithm for it is as described:

1. Initialization of the LCD:

  • According to the desired resolution(as the LCD driver supports two resolutions 800x480 & 480x272), the resolution is first set and the basic touch.non-touch config is made. In our case the LCD is non-touch.
  • After setting the chip-select and reset pins as outputs, the SPI is initialized. SSP1 was configured to be used for LCD interfacing.
  • Register at address 0x00 is read for getting the version of the LCD over SPI.
  • PLL is initialized and the HSYNC, VSYNC and Pixel clock(terminologies described here) parameters are set according to the resolution chosen. (as per the RA8875 ADafruit Datasheet)
  • Active window X and window Y are set by writing to the appropriate registers.
  • Clear the entire window.
  • Set SPI clock frequency.{There's a trick in this as well, the adafruit RA8875 first responds to 2MHz SPI only, and after initializing the PLL and other timing related configs, it listens to up to 30MHz SPI}

2. Initialization of the Camera:


In Terms of Pseudo Code & Algorithm, the following is the methodology we followed which consists of API's and its functions. The design is mainly of two parts:


1. Design for LCD : LCD would continuously display a menu on its right with different options based on the currently selected menu. Menu driver is programmed dynamically using pointers & function Pointer to create sub-menus and sub-items inside sub-menus. A main menu object pointer is created in the main function which is propagated along the menu-tree to choose which should be the menu which needs to be displayed on the LCD currently.

Various API & functions as follows:

  • Add_Menu API, whenever gets called would add one more node to the linked list.
  • Add_Submenu API would create Linked List inside that Node.
  • "Navigate" API enables a user to navigate through Menus. This API navigates pointer to different nodes and calling respective functions such as capture image or going inside sub_menu.
LCD Menu working

With this dynamic functionality, we can create menus as required inside which we can call the respective function to be performed according to the submenu.


Psuedo Code for LCD_Menu -

A menu was designed for the LCD screen using an LCD_MENU class. This class included functions like adding sub-menu, displaying them or loading previous ones. It helped in dynamically adding options for the innumerous camera settings like changing image resolution and format.The private members of this class are as shown:

   private:
   type myType;                         //to know if current object is itemType or menuType
   str myName;                          //name of this object
   LCD_MENU *ParentMenu;                //parent menu of current menu object
   LCD_MENU *CurrentMenu;               //reference to global CurrentMenuG
   VECTOR<Element> all_elements;        //to store all elements by Element .name .id and .type
   VECTOR<str> all_elements_name;       //to store all elements by name to use by Find function
   int highlighted;                     //to track currently highlighted sub-element

Similar to this class, a file_explorer class was implemented for navigating through the SD card files.


2. Design for SJOne & CAM : The controller has to Configure camera, receive data from the camera and send this to LCD as soon as it gets the data.

Function includes :

  • Configuration of Camera Module by configuring different registers specified in datasheet & setting image format.
  • To receive Data from Camera at 8 MHz through SPI. After receiving One Frame, it is sent to LCD via SPI. In both the case, Controller is Master and another device is a slave.
  • To Store Image in SD card, the whole frame is sent to SD card through SPI, and a new text file with a unique name is created and pixel values in that text file would be ASCII characters coming from 16-bit image buffer.

Testing & Technical Challenges

Problem 1: Making any camera work on SJOne can be little tricky, due to its less RAM to store one full B/W or RGB frame. Also interfacing the regular CMOS camera modules with Pixel Clks requires perfect timing to get a proper or any image at all.

Solution: The solution was to use the already buffered and stored camera frame and then transfer it one by one based on SJONE's reading speed(via SPI/I2C) to display it on the LCD pixel by pixel. After trying a number of cameras like OV7670 without fifo and VC0706 serial camera(which had unsupported output format), we were able to solve the problem by using OV2640 SPI camera with fifo buffer which made capturing and retrieving the image easier because the buffer saved the whole frame, and cleared only after SJONE has finished reading the frame.

Problem 2: The camera displayed two frames on the LCD screen for resolutions greater than 352x288. This was because the buffer size of the camera could only hold upto 16bits pixels of max 352x288 resolution, as the buffer size is ~384KBytes.

Solution: This could be avoided by upgrading the buffer size on the camera module or by using an external buffer to store the whole image before previewing on LCD.

Problem 2: The camera was displaying image in form of random pixel values.

Solution: After taking a closer look at datasheet we realized that we were configuring the CMOS registers for wrong/unsupported format (by LCD) and not RGB565 format. After configuring the registers for RGB565 with different resolution types, the LCD displayed proper images.

Conclusion

We have successfully designed and implemented a digital camera with a number of features. This project has helped us gain insight knowledge of the implementation of FreeRTOS concepts like prioritizing tasks and using semaphore and mutex for SD card reading and writing. We learned to implement communication protocol drivers such as SPI. Working with pixel data helped us go into deeper details of the SPI protocol as each byte had to be handled accurately. We have tried the PCB design on both Eagle and Dip Trace, we were able to complete the PCB design starting from schematic diagram to 3D imaging of the PCB. We were also able to learn the CAD design required for 3D printing.

Apart from the technical aspects of the project, a sense of cordial teamwork was also developed amongst the team members which led to the successful accomplishment of the project's objectives.

Project Video

Project Source Code

References

Acknowledgement

We would like to thank our professor, Preetpal Kang for designing such a course where learning was his guidance and consistent feedback. We would also like to thank the ISA team for their valuable inputs as per their own experience of the course when taken and their support and motivation at every phase of this project. There were some unsung, selfless classmates who helped us whenever necessary, whenever any immediate hardware was required or any other guidance needed in spite of being from the rival teams. We would like to thank each one of them who forgot their differences or considered the course and project as a competition to selflessly help us when in need.

References Used

[1] Preetpal Kang's lecture notes of CMPE 244, Computer Engineering, San Jose State University, Jan-May 2018.

[2] OV2640 Technical Description

[3] 5.0" LCD & Datasheet

[4] RA8875 LCD Driver Description