For those who already have a certain Linux Foundation, Raspberry Pi should be very simple and natural to learn. In their eyes, Raspberry Pi is a simple, cartoon version of Linux. However, it is not easy for me to compile, compile, and run a simple program for a person or beginner who is familiar with the Microsoft technology ecosystem.
It is a relatively complete Raspberry Pi Development Environment built by me, with a display (HDMI to VGA-> 7-inch display screen 800*480), a keyboard and a mouse, etc, of course, these are not necessary, but they are more intuitive for beginners.
To reduce the difficulty of learning and development, Raspberry Pi provides a variety of programming languages, such as Python, Java, C/C ++, and other development languages supported by linux. In the graphic interface, Python programming, debugging, and running can be relatively simple in the IDE environment. If it is based on a third-party support library, you can also use Python to manipulate GPIO.
Out of my love for the C/C ++ language, I chose Program Development Based on GNU C, a Raspberry Pi that has been installed on a smart car. We need to build a programming, deployment, and running debugging environment without display, mouse, and keyboard.
As for how to burn Raspberry Pi's system to the SD card, we will not continue to talk about it here. There are many introductory articles to explain and explain. This article mainly introduces how to build a programming platform that facilitates interaction with the Windows platform, and operate GPIO and serial communication using the wiringPi C/C ++ library.
Step 1: remotely log on to Raspberry Pi
We select the installed Raspberry Pi system image as Raspbian. After the system is started by default, remote SSH connections are supported. We can download a free PuTTY tool software for remote connection.
Open the connection and enter the User name: pi password: raspberry (default). The following figure is displayed.
Step 2: Install the simple FTP service
To facilitate the deployment of C Files written in Windows to Raspberry Pi, We have installed a simple FTP service. We chose an open-source, lightweight FTP server vsftpd. The installation steps are as follows:
1. Install the vsftpd service
Sudo apt-get install vsftpd
2. Enable the vsftpd service
Sudo service vsftpd start
3. modify the configuration file
Sudo nano/etc/vsftpd. conf
Locate and modify
Anonymous_enable = NO // anonymous access not allowed
Local_enable = YES // allow local users to access
Write_enable = YES // write allowed
Local_umask = 022 // sets the File Permission mask after upload.
Save the disk and exit.
4. Restart the vsftpd service.
Sudo service vsftpd restart
Open any FTP client software on windows (I have been using FlashFXP) and configure the relevant IP address, user and password (username: pi password: raspberry ).
After the connection is successful, the following screen is displayed:
Step 3: Install the wiringPi Library
WiringPi is a third-party encapsulated GPIO control library function for Raspberry Pi platform. WiringPi complies with GUN lv3. WiringPi is developed using C or C ++ and can be transferred to applications in other languages.
For more information, see this article: http://blog.csdn.net/xukai871105/article/details/17737005
With the above three steps, we can write and develop C language code in Windows, remotely deploy it to Raspberry Pi, compile it using the GCC tool on Raspberry Pi, and then execute it. Before writing the code, let's take a look at Raspberry Pi's GPIO interface, as shown in:
We need to complete three functions: 1. GPIO controls an LED flash; 2. communicates with the lingxiao Board through a serial port; 3. Outputs the PWM signal to control the steering gear.
The following describes the implementation of the above three functions.
1, GPIO control led flashing
Hardware wiring: we choose an LED light-emitting diode, Which is welded to a resistance of about 1 kb. We cut a Dupont wire and weld it to two diode pins (one of which is welded to the other end of the resistor ). The long LED pin is positive. We connect it to Raspberry Pi 11th, that is, GPIO0. The other end is connected to 25 pins (any one can be connected to a pin marked with 0 V ).
We compile the relevant code in Notepad (EverEdit is recommended), as shown in:
Upload the LED. C file to Raspberry Pi through FlashFXP. Use GCC for compilation and execution, as shown in:
If there is no problem with the hardware, you should see that the LED lights are flashing.
2Serial Communication Between Raspberry Pi and lingxiao Development Board
2.1 disable the serial port debugging Function
The serial port cannot be used directly yet, because it is bound to the debug port by default, so we must disable this function first.
Enter the following command in the command line:
Sudo nano/boot/cmdline.txt
Set the following content
Dwc_otg.lpm_enable = 0 console = ttyAMA0, 115200 kgdboc = ttyAMA0, 115200 console = tty1 root =/dev/mmcblk0p2 rootfstype = ext4 elevator = deadline rootwait
Change
Dwc_otg.lpm_enable = 0 console = tty1 root =/dev/mmcblk0p2rootfstype = ext4 elevator = deadline rootwait
Run the following command:
Sudo nano/etc/inittab
Set the following content
# Spawn a getty on Raspberry Pi serial line
T0: 23: respawn:/sbin/getty-L ttyAMA0 115200 vt100
Change
# Spawn a getty on Raspberry Pi serial line
# T0: 23: respawn:/sbin/getty-L ttyAMA0 115200 vt100
Restart Raspberry Pi.
2.2 hardware Wiring
Pin 8 is TX, and pin 10 is RX, Which is TTL level. We connect to the sub-board interface 19 and 20 of lingxiao Development Board.
Raspberry Pi 8 (TX)-lingxiao development board 19 (RX)
Raspberry Pi 10 (RX)-lingxiao Development Board 20 (TX)
Raspberry Pi 6 (0 v)-lingxiao Development Board 03 (GND)
2.3. NET Micro FrameworkSerial Programming
The required functions are relatively simple and do not require two-way communication. After receiving the key information of the control camera cloud platform, lingxiao Development Board directly sends it to Raspberry Pi (if the car is remotely controlled through the network, then, Raspberry Pi can obtain remote key control information through wifi socket programming, and then send it to lingxiao Development Board via serial port. The latter will control the car. If there is time in the future, you can do this function ).
Serial Port definition:
Static SerialPort piPort = newSerialPort ("COM4", 9600 );
In the Main function, enable the serial port:
PiPort. Open ();
Send the button information in a key event:
Static void ps2_Click (object sender, PS2.ButtonArgs e)
{
If (e. key = PS2.Key. RRocker)
{
PS2 ps2 = (PS2) sender;
PS2.ButtonArgsbutton = ps2.GetButton (PS2.Key. L2 );
If (button. state = 1) // press the L2 button to send the joystick information.
{
Byte [] buffer = newbyte [] {0xAA, (byte) e. x, (byte) e. y, 0x55 };
PiPort. Write (buffer, 0, 4 );
PiPort. Flush ();
}
}
}
2.4Raspberry Pi serial Programming
Open the serial port:
If (fd = serialOpen ("/dev/ttyAMA0", 9600) <0)
{
Fprintf (stderr, "Unable to open serial device: % s \ n", strerror (errno ));
Return 1;
}
Receive data:
While (1)
{
If (serialDataAvail (fd)> = 4) // determine the number of receiving buffers
{
If (read (fd, buffer, 4) = 4)
{
If (buffer [0] = 0xAA & buffer [3] = 0x55)
{
Printf ("x: % dy: % d \ r \ n", buffer [1], buffer [2]);
}
}
}
Else
{
Delay (10 );
}
}
Note: The WiringPi encapsulation does not provide the serial port read function, but only provides the acquisition of a single character. We directly use the read function provided by linux.
2.5 communication test
Write related files, transfer them to Raspberry Pi, and then compile the files. After running the program, we can see the following output information when operating the Sony PS2 button:
3. Raspberry Pi PWM output control
Raspberry Pi only provides one physical hardware PWM output IO, that is, GPIO1, 12th pins. The actual test shows that the cycle is about us, and the interface encapsulated by WiringPi cannot modify the size of the cycle (you can study the underlying code later), which does not meet the needs of the steering gear control, the servo generally requires a period of around 20 ms. In addition, the camera cloud platform is used to control two-way Steering gears, so one physical hardware PWM is not enough. Therefore, we adopt soft PWM, that is, using ordinary GPIO to simulate PWM square wave output through clock interruption.
We use GPIO1 (12 pins) and GPIO2 (13 pins) to simulate PWM output, therefore, the hardware wiring is also the PWM signal input pin of the steering gear and the two pins of the Raspberry Pi are connected (the steering gear provides 5 V power supply instead of directly obtaining the power from the Raspberry Pi 5 V pin ).
PWM initialization code:
# Define PWM1 1
# Define PWM2 2
SoftPwmCreate (PWM1, pwmV1, 200); // 1 = 100us 7 ~ 28
SoftPwmCreate (PWM2, pwmV2, 200); // 1 = 100us 7 ~ 28
In the serial port information receiving, the PWM is output to control the steering gear.
If (read (fd, buffer, 4) = 4)
{
If (buffer [0] = 0xAA & buffer [3] = 0x55)
{
PwmV1 = 7 + (int) (buffer [1] * 21.0/255.0); // x
PwmV2 = 7 + (int) (buffer [2] * 21.0/255.0); // y
SoftPwmWrite (PWM1, pwmV1 );
SoftPwmWrite (PWM2, pwmV2 );
}
}
Deploy to Raspberry Pi, compile and run. If we have an oscilloscope on hand, we should be able to see the Output Waveforms of GPIO1 and GPIO2.
If the above program runs together with lingxiao board, you can see the following results:
Http://v.youku.com/v_show/id_XNjY2MTE1NjQ0.html
Article Navigation:
1. Remote Control of Raspberry Pi +. net mf Smart car for Video Monitoring
2. Control of Raspberry Pi +. net mf Smart car for video monitoring (. NETMF)
3. Control of Raspberry Pi +. net mf Smart car for video monitoring (Raspberry Pi)
4. Video of Raspberry Pi +. net mf Smart car for Video Monitoring
Summary:
1. Raspberry Pi's small hardware design will make some people like Linux because of Raspberry Pi.
2. Linux is not a real-time system after all. When making soft PWM, you will find that the steering gear is jittery and the amplitude is not small (because the lingxiao system has provided 16 PWM, therefore, the lingxiao system will control all the Steering Gears in the future)
3. the Linux system image is about GB. Compared with the lingxiao system's hundreds of K images, it is quite important and will take a longer start time.