Tiny Core Linux

Tiny Core Base => Raspberry Pi => Topic started by: sodface on January 08, 2015, 07:53:52 PM

Title: Camera Module
Post by: sodface on January 08, 2015, 07:53:52 PM
I got the Raspberry Pi camera module a couple of days ago and I'm somewhat confused on how to enable it in piCore or even if it will work at all.  Most of the camera setup guides are for Raspbian which have you select "enable camera" through raspi-config which is great for Raspbian but I'm not sure what that's actually doing which makes it a bit difficult to translate to piCore.

Is it loading the V4L2 kernel modules??  Is that what's needed for the camera or is that just one of several options?
Is it possible to grab kernel modules from the 3.12.35 Raspberry Pi github area and get them to work properly with piCore of the same kernel version?  Of course I have tried and failed but is it possible and I'm just not packaging them correctly or should I stop trying??  The version magics are different on them I guess even though the kernel version is the same?

I think that the raspistill / raspivid utils are not in the piCore repo is that true?

Sorry for the poorly worded post, I guess I'm just not "getting it" and I'm not even sure what questions to ask.  If someone can point me in the right direction I'd appreciate it.
Title: Re: Camera Module
Post by: sodface on January 09, 2015, 04:51:08 AM
Looks like all raspi-config does is edit config.txt which I already did manually based on the Arch wiki (https://wiki.archlinux.org/index.php/Raspberry_Pi#Raspberry_Pi_camera_module).

raspi-config section:

Code: [Select]
# $1 is 0 to disable camera, 1 to enable it
set_camera() {
  # Stop if /boot is not a mountpoint
  if ! mountpoint -q /boot; then
    return 1
  fi

  [ -e /boot/config.txt ] || touch /boot/config.txt

  if [ "$1" -eq 0 ]; then # disable camera
    set_config_var start_x 0 /boot/config.txt
    sed /boot/config.txt -i -e "s/^startx/#startx/"
    sed /boot/config.txt -i -e "s/^start_file/#start_file/"
    sed /boot/config.txt -i -e "s/^fixup_file/#fixup_file/"
  else # enable camera
    set_config_var start_x 1 /boot/config.txt
    CUR_GPU_MEM=$(get_config_var gpu_mem /boot/config.txt)
    if [ -z "$CUR_GPU_MEM" ] || [ "$CUR_GPU_MEM" -lt 128 ]; then
      set_config_var gpu_mem 128 /boot/config.txt
    fi
    sed /boot/config.txt -i -e "s/^startx/#startx/"
    sed /boot/config.txt -i -e "s/^fixup_file/#fixup_file/"
  fi
}
Title: Re: Camera Module
Post by: sodface on January 09, 2015, 12:59:47 PM
Found this write-up (http://www.emmaanuel.com/Use-Raspberry-Camera-Module-with) for using the camera on piCore 5.3.1.  I've already done the config.txt edits based on the arch wiki so I guess what I need next is the rpi-videocore.tcz for 6.x.  I will attempt to create it myself.

I think if I want to use video utils other than the raspistill/raspivid etc rpi-videocore.tcz then that's where I'll need the v4l2 modules.  Not sure though.
Title: Re: Camera Module
Post by: sodface on January 09, 2015, 10:48:30 PM
I guess I got this working.  Raspistill seems to work, haven't tested anything else.  I compiled raspicam using the sources from the userland Raspberry Pi github location (https://github.com/raspberrypi/userland).

I cobbled together an extension for it if anyone wants to try it. (https://www.strongspace.com/shared/3vbps0705t) I'm fairly sure it doesn't follow the extension guidelines *at all* but it does seem to load and work so I guess that's better than nothing.

You have to chmod 777 /dev/vchiq or use sudo. 

Code: [Select]
tc@box:~$ raspistill -v

raspistill Camera App v1.3.8

Width 2592, Height 1944, quality 85, filename (null)
Time delay 5000, Raw no
Thumbnail enabled Yes, width 64, height 48, quality 35
Link to latest frame enabled  no
Full resolution preview No
Capture method : Single capture

Preview Yes, Full screen Yes
Preview window 0,0,1024,768
Opacity 255
Sharpness 0, Contrast 0, Brightness 50
Saturation 0, ISO 0, Video Stabilisation No, Exposure compensation 0
Exposure Mode 'auto', AWB Mode 'auto', Image Effect 'none'
Metering Mode 'average', Colour Effect Enabled No with U = 128, V = 128
Rotation 0, hflip No, vflip No
ROI x 0.000000, y 0.000000, w 1.000000 h 1.000000
Camera component done
Encoder component done
Starting component connection stage
Connecting camera preview port to video render.
Connecting camera stills port to encoder input port
Closing down
Close down completed, all components disconnected, disabled and destroyed
Title: Re: Camera Module
Post by: bmarkus on January 09, 2015, 11:10:15 PM
Complete set of kernel modules available here:

http://tinycorelinux.net/6.x/armv6/release/src/kernel/

You can find modules if you need any.

Also I will get a camera next week to try.
Title: Re: Camera Module
Post by: sodface on January 10, 2015, 06:25:25 AM
Thanks a lot bmarkus, I think I'll need the v4l2 modules if I want to use apps other than the raspicam ones.
I'm anxious for you to get your camera so that I can see the right way to set it up :)

This is a wrap-up of what I did:

- Add to config.txt:
gpu_mem=128
start_file=start_x.elf
fixup_file=fixup_x.dat

- Compiled the raspicam binaries using the github userland repo, followed bits and pieces of this write-up. (http://www.darkoperator.com/blog/2013/5/23/fixin-raspistill-and-raspivid-for-headless-streaming-on-the.html)

- Bundled up the result into raspicam.tcz (https://www.strongspace.com/shared/3vbps0705t)

- From a fresh boot:

Code: [Select]
tc@box:~$ uname -a
Linux box 3.12.35-piCore+ #1 PREEMPT Mon Dec 29 17:24:10 CET 2014 armv6l GNU/Linux
tc@box:~$ raspistill
-sh: raspistill: not found
tc@box:~$ tce-load -i raspicam.tcz
raspicam.tcz: OK
tc@box:~$ raspistill
* failed to open vchiq instance
tc@box:~$ sudo chmod 777 /dev/vchiq
tc@box:~$ raspistill
raspistill Camera App v1.3.8 [remaining output omitted]
tc@box:~$ raspistill -vf -hf -o test.jpg

Need to create a properly compiled and formatted extension for the raspicam utilities for 6.x.  You called it rpi-videcore.tcz for 5.x? Then add to onboot.lst and fix the /dev/vchiq permissions on boot (not sure what the best solution for that is).

Attached is a sample pic that I resized for upload.
Title: Re: Camera Module
Post by: sodface on January 11, 2015, 07:22:01 AM
Is there a rule about replying to your own post over and over again?  :-\

Wanted to add that I noticed that the permissions on the /dev/vchiq device were back to 600 after some time had passed which gives you an error without sudo:

Code: [Select]
tc@box:~$ raspistill
* failed to open vchiq instance
tc@box:~$ uptime
 14:57:49 up 21:54,  2 users,  load average: 0.00, 0.01, 0.04

I know by the uptime that a reboot had not happened since I last did a chmod to /dev/vchiq.  Does udevd periodically check permissions based on the rules and reset them if they are wrong?

Is this even a problem or should I just prefix the commands with sudo and move on?  It seems other people are using root:video 660 on /dev/vchiq and adding the user to the video group.
Title: Re: Camera Module
Post by: bmarkus on January 11, 2015, 07:35:05 AM

Is this even a problem or should I just prefix the commands with sudo and move on?  It seems other people are using root:video 660 on /dev/vchiq and adding the user to the video group.

There are no video group in piCore.
Title: Re: Camera Module
Post by: andyj on January 24, 2015, 06:42:26 PM
I loaded raspicam.tcz in 6.0 and chmod'ed /dev/vchiq, but I get this with raspistill -v:

raspistill Camera App v1.3.8

Width 2592, Height 1944, quality 85, filename (null)
Time delay 5000, Raw no
Thumbnail enabled Yes, width 64, height 48, quality 35
Link to latest frame enabled  no
Full resolution preview No
Capture method : Single capture

Preview Yes, Full screen Yes
Preview window 0,0,1024,768
Opacity 255
Sharpness 0, Contrast 0, Brightness 50
Saturation 0, ISO 0, Video Stabilisation No, Exposure compensation 0
Exposure Mode 'auto', AWB Mode 'auto', Image Effect 'none'
Metering Mode 'average', Colour Effect Enabled No with U = 128, V = 128
Rotation 0, hflip No, vflip No
ROI x 0.000000, y 0.000000, w 1.000000 h 1.000000
mmal: mmal_vc_component_create: failed to create component 'vc.ril.camera' (1:ENOMEM)
mmal: mmal_component_create_core: could not create component 'vc.ril.camera' (1)
mmal: Failed to create camera component
mmal: main: Failed to create camera component
mmal: Camera is not enabled in this build. Try running "sudo raspi-config" and ensure that "camera" has been enabled

I saw that MMAL is required somehow, but it didn't seem to be with the extension so do I need to get and compile it or is there an easier fix? I have a fully compiled kernel tree if needed.
Title: Re: Camera Module
Post by: sodface on January 24, 2015, 08:58:56 PM
Did you double check your ribbon cable connections?  You did the edits to config.txt?
Title: Re: Camera Module
Post by: andyj on January 25, 2015, 05:47:07 AM
The connections looked OK but I reseated them anyway. That must have been it, because it's working now. It's kinda slow when taking a picture. Is that normal?
Title: Re: Camera Module
Post by: Paul_123 on January 25, 2015, 03:50:02 PM
Taking pictures and writing to disk is quite slow. 
Title: Re: Camera Module
Post by: sodface on January 25, 2015, 05:30:53 PM
The connections looked OK but I reseated them anyway. That must have been it, because it's working now. It's kinda slow when taking a picture. Is that normal?

As I understand it, raspistill by default actually activates the camera and takes video for 5 seconds and then the "still" is the last frame of the video, which is the only part that's saved.  You can adjust the time with the -t option.
Title: Re: Camera Module
Post by: andyj on January 25, 2015, 08:29:13 PM
What I would really like is to have the camera always running and saving the last few seconds of video in a loop so when I "click" I get a second before and after. I just got the camera yesterday and I was out of town most of today so I haven't had much of a chance to play with it.
Title: Re: Camera Module
Post by: Paul_123 on January 26, 2015, 08:59:22 AM
What I would really like is to have the camera always running and saving the last few seconds of video in a loop so when I "click" I get a second before and after. I just got the camera yesterday and I was out of town most of today so I haven't had much of a chance to play with it.

That will kill an SD card quickly.   Why not just stream the video?
Title: Re: Camera Module
Post by: curaga on January 27, 2015, 01:41:06 AM
Well, he could program it so that the couple seconds is kept in RAM, and SD is only touched when saving.
Title: Re: Camera Module
Post by: andyj on January 27, 2015, 03:25:49 PM
That's pretty much the direction I've decided to go. I'll need to hack raspivid by adding a switch that basically says "record a window around an event like a SIGUSR1 and send me the file name in stdout". How hard can it be?
Title: Re: Camera Module
Post by: andyj on January 31, 2015, 05:05:01 PM
FWIW, I got it working by doing this:

set clipid [open "|raspivid -n -v -h 300 -w 400 -fps 10 -rot 180 -g 2 -sg 1000 -sn 001 -wr 300 -t 0 -pf baseline -o clips/vid%03d.h264 </dev/zero 2>@1"]

Then later I do this:

exec -ignorestderr -- cat $cfilea $cfileb $cfilec | ffmpeg -r 10 -i - -vcodec copy $vidid >/dev/null 2>/dev/null &

once I know which three clips I need. FFmpeg seams like a BFH for just concatenating three files and putting some mp4 information along with them. Is there a lighter faster way?
Title: Re: Camera Module
Post by: curaga on February 01, 2015, 01:39:56 AM
Only some codecs/containers support direct catting. Ogg Theora is one.
Title: Re: Camera Module
Post by: sodface on February 04, 2015, 07:32:13 PM
This is one of several posts (http://www.raspbian.org/RaspbianQuake3) that mentions the creation of a udev rule for the vchiq device to relax the permissions a little.  I did it like this on my picore 6.0 tonight and it seems to work fine to allow default tc user to use raspistill/raspivid without sudo.

Code: [Select]
tc@box:/opt$ cat /etc/udev/rules.d/10-vchiq-permissions.rules
SUBSYSTEM=="vchiq",GROUP="staff",MODE="0660"
Title: Re: Camera Module
Post by: sodface on February 09, 2015, 06:49:12 PM
I noticed there was a small change to the raspicam source code on github so I thought I would recompile it and make another extension just for the practice.  There are precompiled binaries there too which I tried the first time around but the first one I tried wouldn't run (raspistill) - it gave me a "file not found" message which I didn't really investigate at the time, just moved straight to compiling from source.

So now I'm a little confused.  I've got three raspistill binaries on my pi, the first is loaded from the extension I originally made several weeks ago and was compiled from source available at the time. The next is in the build directory I used to compile from the updated source a couple of days ago.  The last is an already compiled binary I downloaded from the raspberry pi section on github.

All three are slightly different sizes, 111.8k, 108.4k, and 113.8k.

They all say when executed:
raspistill Camera App v1.3.8

The precompiled binary has a different interpreter file (ld-linux.so.3) which didn't exist on my system so I symlinked to the ld-linux-armhf.so.3 file used by the other binaries in order to get rid of the "file not found" message when trying to execute it (which I suspect was the problem I had originally several weeks ago).

- Why are the file sizes different?  Slightly different source? Different compiler options?  Both?
- Why are they all v1.3.8 when I know for at least two of them that the source had changed between compilations?
- Which would be better to use in the tcz, the one I compiled from source or the precompiled one?  I'd need to put a symlink for /lib/ld-linux.so.3 ---> /lib/ld-linux-armhf.so.3 in the extension to get it to work.

I know it's probably not a huge deal, just trying to learn something along the way and understand best practices...

//edit - also the "into the core" book mentions to strip debugging symbols to make the binary smaller - is that applicable to arm also and if so is it done at compile time or after the fact? 


Code: [Select]
tc@box:~$ file /tmp/tcloop/raspicam/usr/local/bin/raspistill
/tmp/tcloop/raspicam/usr/local/bin/raspistill: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.32, not stripped

tc@box:~$ file /mnt/mmcblk0p2/userland/opt/vc/bin/raspistill
/mnt/mmcblk0p2/userland/opt/vc/bin/raspistill: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.32, not stripped

tc@box:~$ file /home/tc/raspistill
/home/tc/raspistill: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.3, for GNU/Linux 3.1.9, not stripped
Title: Re: Camera Module
Post by: sodface on February 16, 2015, 05:07:58 PM
I've been experimenting (struggling!) with video streaming on PiCore6 and for the record have managed to get the following working, both using gstreamer:

UDP unicast:
On the Pi
Code: [Select]
gst-launch-1.0 rpicamsrc ! 'video/x-h264, width=1280, height=720, framerate=30/1,profile=high' ! queue ! rtph264pay ! udpsink
host=192.168.1.2 port=5000

gstreamer windows version on the receiving side
Code: [Select]
gst-launch-1.0.exe udpsrc port=5000 caps="application/x-rtp,media=(string)video,clock-rate=(int)90000,encoding-name=(string) H264" ! rtph264depay ! decodebin ! autovideosink

And then using the gstreamer gst-rtsp-server (which seems far more useful than the unicast method):
Code: [Select]
./test-launch '( rpicamsrc vflip=true ! video/x-h264, width=1280, height=720, framerate=30/1,profile=high ! queue ! rtph264pay config-
interval=1 name=pay0 pt=96 )'

I tested the rtsp stream with vlc on windows and with rtsp player on android.  All testing done locally via wifi.  Worked ok, windows performance was better than android.  Need to keep working it.  For both methods I used the gstreamer and base plugins from the repo and compiled rpicamsrc, selected modules from from gst-plugins-good and gst-rtsp-server from source.

Title: Re: Camera Module
Post by: sodface on February 23, 2015, 09:11:14 PM
Another quick update on steaming, this time using mjpeg.  I made another raspicam extension using Silvan Melchior's userland source (https://github.com/silvanmelchior/userland) which includes the raspimjpeg command as well as raspistill, raspivid, and raspiyuv.  Then I made a small extension with the bare essentials from mjpg-streamer (http://sourceforge.net/p/mjpg-streamer/code/HEAD/tree/).

The immediate goal was to get a stream going that worked in a browser without plugins and required only minimum extensions on the pi, no apache, no php etc.

Load raspicam_sm.tcz (https://www.strongspace.com/shared/8zfy0hhzht) and mjpegstream.tcz (https://www.strongspace.com/shared/3ov4lw5fyk).

Start raspimjpeg and then mjpg-streamer and then goto {your.pi.ip.addr}:8080 in a browser (tested in chrome) to see the stream.

Code: [Select]
tc@box:/tmp$ sudo raspimjpeg &
tc@box:/tmp$ MJPEG streaming, ready to receive commands

tc@box:/tmp$ mjpg_streamer -i "/usr/local/lib/input_file.so -f /dev/shm/mjpeg -n  cam.jpg" -o "/usr/local/lib/output_http.so -w /var/www"
MJPG Streamer Version: svn rev:
 i: folder to watch...: /dev/shm/mjpeg/
 i: forced delay......: 0
 i: delete file.......: no, do not delete
 i: filename must be..: cam.jpg
 o: www-folder-path...: /var/www/
 o: HTTP TCP port.....: 8080
 o: username:password.: disabled
 o: commands..........: enabled

The newer versions of raspimjpeg use a configuration file hard coded to be located at /etc/raspimjpeg.  The extension linked above includes this file.   I was reading the core book on extension making but need to better understand what to do with config files so they are writable and you are able to save changes without having them overwritten the next time you load the extension.  I think as the extension is now you won't be able to have changes persist in /etc/raspimjpeg - haven't really tested it yet.

raspimjpeg is used in Silvan Melchior's RPi_Cam_Web_Interface (https://github.com/silvanmelchior/RPi_Cam_Web_Interface) application but it has a lot of features and needs php and apache (though people have used it with nginx also).  I wanted to just strip out the raspimjpeg binary but it still requires the /etc/raspimjpeg config file and needs some directories to exist when it starts so the extension creates them:
Code: [Select]
mkdir -p /var/www/media
mkdir -p /dev/shm/mjpeg
mknod /var/www/FIFO p
chmod 666 /var/www/FIFO

The mjpegstream extension just includes the mjpg-streamer binary, input_file.so and ouput_http.so libs and a simple index.html that sits in /var/www as that was the default path that the raspimjpeg config file defined.

So anyway, not sure if this is of any help or use but I was looking for a minimal / no bells or whistles way to do a basic stream to devices that already have a browser and wouldn't have to install anything else.   raspimjpeg is pretty cool as it uses the /var/www/FIFO pipe to take commands.  One command is "an" which changes the text annotation that is overlayed on the stream.  Right now that is defined in the /etc/raspimjpeg config file but can be changed on the fly by sending the command and the new text.  I'm going to try a cron job that runs my script to get the temperature from my w1 sensor every couple of minutes and update the stream annotation with that.

Work in progress...
Title: Re: Camera Module
Post by: sodface on February 26, 2015, 06:32:45 PM
Updated and replaced the raspicam_sm.tcz (https://www.strongspace.com/shared/8zfy0hhzht) file so that the raspimjpeg config file is writable and is in /usr/local/etc with a symlink created in /etc.
Title: Re: Camera Module
Post by: andyj on March 01, 2015, 08:15:55 PM
I don't suppose you have one built for Pi 2? Otherwise can you tarball up your files and I can build it for arm7?
Title: Re: Camera Module
Post by: sodface on March 02, 2015, 04:22:19 AM
I don't suppose you have one built for Pi 2? Otherwise can you tarball up your files and I can build it for arm7?

I thought the armv6 extensions would work on the rpi2?  I can make the files available though it might be better to just grab the updated source yourself?  Let me know if you want the official userland (at the time) or the Silvan Melchior one with raspimjpeg or both.
Title: Re: Camera Module
Post by: andyj on March 02, 2015, 04:34:00 AM
The files with the mjpeg updates. I might try to use it to make the clips I need instead of using ffmpeg to assemble short clips into one longer one, so it seems that you have what may be the best starting point. It might be cleaner for what I need. Yes, I vaguely remember trying an older version of raspicam on my Pi2 and it working, so that's not the issue.
Title: Re: Camera Module
Post by: sodface on March 02, 2015, 05:53:48 AM
andyj, here's the userland-master-SM (https://www.strongspace.com/shared/qvhdjq62vu) source.  The build sub directory I created as the target for the binaries.

This is the mjpg-streamer source (https://www.strongspace.com/shared/lpghcnr2s8), not sure if you wanted that too.
Title: Re: Camera Module
Post by: andyj on March 02, 2015, 04:45:18 PM
Thanks for the files. I changed /opt/vc to /usr/local, installed to /tmp/raspicam-v7, deleted the include, share, and src directories, added etc/udev/rules.d/10-vchiq-permissions.conf, but I couldn't find the raspimjpeg config file to put in usr/local/etc. I made the extension without it and it takes h264 video, but I haven't tried the mjpeg yet. What is this mjpeg config file?
Title: Re: Camera Module
Post by: sodface on March 02, 2015, 05:20:16 PM
/etc/raspimjpeg config file is here. (https://github.com/silvanmelchior/RPi_Cam_Web_Interface/blob/master/etc/raspimjpeg/raspimjpeg.1)

If you look at the source for raspimjpeg here (https://github.com/silvanmelchior/userland/blob/master/host_applications/linux/apps/raspicam/RaspiMJPEG.c), you can see it's looking for the config file at /etc/raspimjpeg.  I put the file in /usr/local/etc and a symlink to it in /etc, not sure if that was the best way to handle it or not.
Title: Re: Camera Module
Post by: andyj on April 14, 2015, 07:02:30 PM
I'm finally getting around to the streaming. When I try raspimjpeg the led comes on for a second and then it segfaults. There's no strace for the RPi2. Did you have this problem at some point?
Title: Re: Camera Module
Post by: andyj on April 14, 2015, 07:38:00 PM
I built an strace tcz, but it didn't help. I loaded raspicam_sc over my raspicam_v7 and now it seems to stream OK. What I would like to do is to have several streaming at once and be able to tile them in a page served up by a central server. Any experience or thoughts on this?
Title: Re: Camera Module
Post by: sodface on April 16, 2015, 04:46:41 AM
@andyj - I finished mounting my model A, camera and temp sensor in a project box almost a month ago now and then put everything away.  We haven't gone camping yet so I haven't needed to use it yet (dog monitor while we are away from the campsite).  I thought I would have had an RPi2 by now but I haven't pulled the trigger on one.  I've been meaning to revisit the project and maybe polish up a few things.  None of that answers your question though!  So no, no experience with the multiple streams setup you mentioned.  I'd be interested to hear what you come up with though.  Good luck!