camera power

Download Report

Transcript camera power

Embedded Systems Programming
Application Case Study
Security Camera Controller
(with automatic pan to pre-set zones of
interest triggered by movement sensors)
Requirements analysis
Selecting sensors, inputs and outputs
I/O ports and interfacing
Timing diagrams
Configuration of internal modules (timer, ADC)
Initial situation
(camera facing zone #2)
Zone #1
The automatic pan concept
(zones are equipped with
localised movement sensors)
Zone #3
Camera
Zone
#2
Camera pivot and servo for rotation
Movement detected in zone #1,
camera is rotated to pre-set
angle facing zone #1
Field of view
Zone #1
Rotation
Zone #3
Zone
#2
Richard Anthony
2
Case study - Requirements analysis
Functional requirements of the Security Camera Controller:
• Detect movement in a number of specific zones.
• Sense light level and differentiate between day and night-time.
• Turn camera power on when movement detected.
• Turn camera servo power on when movement detected.
• Turn security light on when movement detected at night-time.
• Automatically pan camera to point at zone where movement detected.
• Turn camera servo power off when camera pan rotation completed.
• Turn camera power off when no movement detected for 5 minutes.
• Turn security light off when no movement detected for 5 minutes.
• Communicate with other components of a security system via a ZigBee
network (this aspect only described in outline).
• Provide simple power and operational status indication via single LED.
Non-functional requirements:
• Use low levels of power.
• Casing must be adequate for harsh environment (full weather exposure
to some parts, large variation in temperatures and humidity).
• Mechanical robustness and smooth rotational movement of camera.
Richard Anthony
3
Case study - Selecting sensors, inputs and outputs
Sensors, and I/O devices for the Security Camera Controller :
• PIR sensors to detect movement in zones. Requires single digital input pin
with interrupt capability per PIR sensor (up to 5 sensors to be supported).
• A light sensitive device such as a Photodiode or Light Dependent Resistor
(LDR) is needed to sense the light level. Requires single analogue input pin
(and the Analogue-to-Digital converter).
• The camera pan movement requires a servo, which is controlled by a single
PWM output (and a programmable timer).
• The servo power, camera power and spotlight power require transistors driven
each by a single digital IO pin (3 of). In addition, because the spotlight is
mains-powered, it requires that the transistor drives a relay.
• The alarm sounder requires a single digital IO pin.
• A low-intensity LED is adequate for the signal LED. It requires a single digital
output pin.
• The ZigBee network module requires a serial port USART (Rx and Tx pins).
It also requires three handshaking signals; 2 digital outputs and 1 digital input.
• The camera is a separate component which sends signals via a separate
wireless link (not part of the controller’s logic or function).
Richard Anthony
4
Case study - I/O ports and interfacing (1)
Interfacing sensors and I/O devices to specific ports (for ATmega328), ordered
in terms of most specific requirements first.
• The ZigBee module’s serial interface is connected to Port D bits 0 and 1
which provide the USART Rx and Tx functions respectively.
• The Light Dependent Resistor must be connected to the Analogue-to-Digital
converter, Port C bit 0 (ADC0) is used.
• The servo is connected to Port B bit 3 which provides the OC2A output from
Timer2.
• The PIR sensors are connected to separate digital input bits on Port D bits 26 inclusive, sharing the PCINT2 pin-change interrupt.
• The servo power, camera power and spotlight power control outputs are
connected to Port C digital output bits 1, 2, 3 respectively.
• The alarm sounder is connected to digital output Port D bit 7.
• The ZigBee module’s handshaking signals are connected to Port B digital IO
bits 0, 1 and 2.
• The signal LED is connected to digital output Port B bit 7.
• The camera data stream has its own wireless link and is not interfaced to the
microcontroller.
Richard Anthony
5
Case study - I/O ports and interfacing (2) – ATmega328p
ZigBee
Comms
PIR 1
Spotlight
power
Camera pwr
PIR 2
Servo pwr
PIR 3
Light
sensor
Signal
LED
PIR 4
PIR 5
Alarm
sounder
Camera
servo
(PWM)
ZigBee module handshaking
Richard Anthony
6
Case study - I/O ports
and interfacing –
ATmega1281 Equivalent
Light sensor
ZigBee
Comms
Camera servo
(PWM)
Alarm sounder
ATmega1281
Spotlight
power
PIR 1
PIR 2
PIR 3
PIR 4
PIR 5
Camera pwr
Servo power
Signal LED
ZigBee module
handshaking
Richard Anthony
Richard Anthony
Case study - I/O ports
and interfacing –
ATmega2560 Equivalent
Light sensor
ZigBee comms
Camera servo
(PWM)
Alarm sounder
ATmega2560
Spotlight power
Camera power
Servo power
PIR 1
…
PIR 5
Signal LED
ZigBee module
handshaking
Richard Anthony
Richard Anthony
Main
Flow chart (1)
Start
Configure ADC
(Sample light level)
Configure Timer1
(1 second clock)
Configure Timer2
(PWM for servo)
Configure PCINT
interrupt
(movement sensors)
Turn spotlight Off
Turn camera Off
SpotlightOff
SpotlightOn
Off
No
period
completed
?
On
No
period
completed
?
Yes
Yes
LightLevel
== Light
?
No
Stop
Turn
Spotlight on
Turn servo power Off
Turn sounder Off
Yes
Turn
Spotlight off
ActivateSounder
Stop
Set sounder On duration
Turn sounder On
Wait
Stop
Richard Anthony
9
Timer1 ISR
Flow chart (2)
Start (1-second
clock interval)
LED_FlashIntervalRemaining --
Signal LED Yes
pulse interval
reached
?
Pulse signal LED
No
Sounder
Yes
OffTime > 0
?
SounderOffTime -No
Sounder
Yes
OnTime > 0
?
SounderOnTime -No
Turn sounder Off
Camera
Yes
OnTime > 0
?
CameraOnTime -No
Turn camera Off
Spotlight
Yes
OffTime > 0
?
SpotlightOffTime -No
Call SpotlightOff
Spotlight
Yes
OnTime > 0
?
SpotlightOnTime -No
Start ADC conversion
Stop
10
Richard Anthony
Start (an input pin
changed value)
Flow chart (3)
PCINT ISR
(Motion sensors)
ADC ISR
No Sensor #1 Yes Set PWM value
to point camera
pin high
to zone #1
?
Sensor #2 Yes
pin high
? No
Sensor #3
pin high
?
No
Set PWM value
to point camera
to zone #2
Yes Set PWM value
to point camera
to zone #3
Turn servo power On
Start (conversion
complete)
No
LightLevel > Yes
threshold
?
LightLevel
LightLevel
= Dark
= Light
Stop
Call SpotlightOn
Turn camera On
Call ActivateSounder
Stop
Richard Anthony
11
Timing diagram 1
1-second clock and light-level sampling
1 second
Timer1 (1-second clock)
Start ADC
conversion
ADC conversion
time
ADC conversion complete
(momentary state)
ADC interrupt handler
Light
Dark
Ambient light level (as detected)
Richard Anthony
12
Timing diagram 2
Movement sensors and camera positioning
Movement sensor #1
Movement sensor #2
Movement sensor #3
PCINT ISR
Zone #1
Camera
target Zone #2
position
Zone #3
Richard Anthony
Timing diagram 3
Actuator control logic
Ambient Light
light
Dark
level
Zone #1
Camera
target Zone #2
position
Zone #3
Servo power
Camera Rotating
(worst case)
Camera power
Sounder
Spotlight
Richard Anthony
Case study - Configuration of internal module – Timer1
Timer1 is used to provide a 1-second clock pulse which is used to count-down
a number of application-level time-periods (i.e. one hardware clock is used to
govern multiple separate software-managed time intervals).
The application-level time periods include the Signal LED pulse interval and the
timing periods for {camera on-time, spotlight on-time, spotlight off-time, sounder
on-time}.
Mode of operation – to achieve a timeout after 1 second, channel A is used in
CTC mode.
Timer1 is ideal for the 1-second clock because it is a 16-bit timer. The largest
prescaler is used, i.e. 1024. This allows the timer to measure an interval of up
to 67 seconds approximately with a 1MHz clock.
The timer is configured to use interrupts (Compare Match interrupt).
15
Richard Anthony
Case study - Configuration of internal module – Timer2
Timer2 generates a PWM pulse to control the rotational position of the camera.
Interrupts are not needed for Timer2; the PWM pulse is generated directly on the OC2A
pin (Port B bit 3 of the ATmega328).
The pulse width is from 500 to 2500 microseconds (the equivalent servo rotational angle
is approximately 180 degrees). A 500µS pulse width indicates fully anticlockwise,
1500µS indicates the centre position, and 2500µS indicates fully clockwise.
The pulse is repeated at a typical rate of 18mS, as shown in the PWM timing diagram:
Pulse (P)
2500µS
500µS
Equivalent
servo
position
Interval (I) = 18 mS typical
However, Timer2 is an 8-bit timer (16-bit Timer1 is used for the 1-second clock), so the
precision of the actual timing signal produced is limited:
• For a clock speed of 1MHz, and by using the 64 prescaler, the pulse interval (given by
the counter overflow) is actually 16.384mS (works correctly with the servos tested).
• The specific prescaler and clock speed combination limits the PWM pulse granularity
to 64µS increments (where 2500µS – 500µS = 2000µS pulse width range for 180°
movement range, ≡ 11µS per degree). Therefore 64µS ≡ 5.76° movement granularity
16
(which is adequate in this application).
Richard Anthony
Case study - Configuration of internal module - ADC
Only a single analogue sensor is used; the light-level sensor. Therefore only a
single channel of the ADC is used; channel 0.
The application design requires that the light level is sampled frequently; once a
second is ideal, therefore the ADC conversion is started from within the Timer1
ISR.
The ADC unit has a 10-bit precision, but in this application it is only necessary
to detect the light-level difference between night-time and daytime, and thus an
8-bit result is adequate. The configuration registers are programmed to provide
the most-significant 8-bits in a single data register.
The ADC is configured to use interrupts (Conversion Complete interrupt).
Richard Anthony
17
Case study - Configuration of internal module – PCINT2 Interrupt
Up to five movement sensors are supported, and it is necessary to detect
signals from these via an interrupt mechanism.
The ATmega328 only has two dedicated interrupt pins (INT0 and INT1), but pinchange interrupts are supported for every I/O pin, managed separately for each
of the three ports (B, C, D).
PCINT2 can be used to detect any pin-change on any of the input pins of Port
D. The movement sensors are each connected to one of the Port D bits (bits 26 inclusive).
The PCINT2 interrupt is enabled.
When triggered, the PCINT2 ISR code must first determine which sensor has
detected movement, by examining all the input bits. The sensors work in activehigh mode (and external pull-down resistors are used in the circuit), meaning
that all bits are low when no movement is detected. Determining which sensor
was activated requires finding which pin has gone high.
The ISR must then set the camera servo PWM value appropriately so that the
camera rotates to point at the zone where movement was detected.
18
Richard Anthony