Install the Arduino IDE in Ubuntu 12.04

转自http://blog.markloiseau.com/2012/05/install-arduino-ubuntu/

Install the Arduino IDE in Ubuntu 12.04

Installing the Arduino IDE in Ubuntu only takes a few minutes. As usual, it’s a better idea to install the Arduino straight from the source, instead of relying on the version in Ubuntu’s repository. While it wasn’t hard to install the Arduino IDE, I noticed that the Arduino GUI was really slow and laggy. This is because the startup script tells Java to use Ubuntu’s GTK look and feel, which makes everything run slowly. It’s easily fixed by changing a single line in the startup script.

Update: In the Arduino 1.0.1 update, “Serial Port” is greyed out. The fix is outlined in the troubleshooting section.

Install the Arduino IDE in Ubuntuarduino_logo

  1. Install gcc-avr, avr-libc and openjdk-6-jre if you don’t have it already.
  2. Plug in the board, see where it’s connected
  3. Download and unpack the Arduino IDEtarball
  4. Run the IDE
  5. Select your board model and serial port
  6. Run a sample program
  7. Fix the buggy interface (optional)
  8. Troubleshooting

Everything worked out of the box, other than the interface.

Install gcc-avr and avr-libc

Gcc-avr and avr-libc give your system the tools it needs to compile c into AVR machine code:

If you don’t have openjdk-6-jre already, install and configure that too:

Once those are installed plug in your board and type  $ dmesg . It will print the kernel’s ring buffer and show you what USB port your Arduino is plugged into:

According to dmesg, our board is plugged into ttyACM0.

Download and Run the Arduino IDE

Go to the downloads page on Arduino’s download page to get the latest Arduino IDE tarball (.tgz file) for your architecture. My laptop is 64-bit, so I chose accordingly. Once the file was finished downloading, I unzipped and ran it with the following command:

Select your board model and serial port

I’m using an Arduino MEGA 2560, so I went to Tools>Board>”Arduino Mega 2560 or Mega ADK.” At this point, I noticed that the GUI was really slow and hard to use. If you want to fix it before proceeding, exit the IDE and skip to the  ”Fixing the Interface” section before proceeding.

The IDE flashed an error about how my board wasn’t accessible over the COM1 port. COM1 usually refers to a 9-pin serial port, and my laptop doesn’t even have one. I went to Tools>Serial Port and selected /dev/ttyACM0, which reflected the output I saw when I checked dmesg.

The errors went away, and I went to File>Examples>Basics>Blink and clicked upload. Sure enough, the LED started blinking. You should be ready to start writing and running Arduino programs!

Fix the Arduino IDE to make it run more smoothly in Ubuntu

Exit the Arduino IDE and go to the installation folder (the folder you unzipped from the .tgz file). Edit the “arduino” script in your favorite text editor. To make Arduino use the native Swing windowing instead of forcing the GTK look and feel, which is the cause of the bugginess, change the following line:

Just delete the -D flag and its argument. Personally, I get nostalgic about the old-school Swing look and feel, but either way, it fixed all of the lagginess issues I was experiencing.arduino_ide_ubuntu

My Arduino IDE, running in Ubuntu (using SWT instead of GTK)

Troubleshooting USB and the grayed out Serial Port

When I got the Arduino 1.0.1 update, “Serial Port” was grayed out in the tools menu. Running arduino as root ( sudo ./arduino) resolved the issue, but it’s not an acceptable solution. In my case, serial port was grayed out because my user didn’t have permission to read and write to the device.

I added my user to the dialout group with the command  sudo usermod -a -G dialout mark . Usually, that would have fixed it but iserial port was still grayed out.

Changing the permissions on /dev/ttyACM0 to world readable and writeable fixed the grayed out serial port. I ran  sudo chmod a+rw /dev/ttyACM0 and the serial port menu worked again.

I’ve noticed that running programs that send lots of data over USB can cause issues with the arduino programming software, making it give errors while uploading code. Holding down the reset button fixed my upload and USB errors in most cases.

Further Reading

If you read nothing else before you start writing programs, look at the official Arduino Reference page. It might be the most concise, complete language reference I’ve ever seen.

Ubuntu 13.04 Android Studio "Unrecognized VM option '+UseCodeCacheFlushing'"

下载完成最新的 Android Studio 结果执行 “./android-studio/bin/studio.sh” 的时候报告

“Unrecognized VM option '+UseCodeCacheFlushing'
Could not create the Java virtual machine.”

解决方法为 在 “/android-studio/bin” 目录下面找到 “studio.vmoptions”,如果你是 64未系统 就打开 “studio64.vmoptions”然后删除里面的 “-XX:+UseCodeCacheFlushing”

Ubuntu12.10 Broadcom (BCM4311)无线网卡驱动问题

DELL E5400 电脑安装 Ubuntu 12.10 ,但是不能识别无线网卡,在这个时候,查看默认的驱动,默认使用的驱动存在问题。

执行

命令,然后重启机器,保证最后的结果为下图所示就可以了。

2013-05-26 00:47:45的屏幕截图

Ubuntu 13.04 用户安装 Flash plugin for Firefox

转自 http://imcn.me/html/y2012/11199.html

今天有童鞋在 Ubuntu QQ群中请教如果为火狐浏览器安装 flashplayer 插件问题,其实这个很简单,此教程以ubuntu 12.04 为列,其实Ubuntu 10.04 之后的版本都可以按照这样的方式安装,因为在ubuntu 软件中没有Flash plugin,那么我么需要手动添加源,去adobe官方网站就可以,使用方便,打开网站:http://get.adobe.com/cn/flashplayer/

然后如图选择“APT,适用于ubuntu 10.04 +”意思就是ubuntu10.04以上版本,再点击“立即下载”按钮。

flashplayerforfirefox01

之后弹出下面的窗口,点击“OK”就行了

flashplayerforFF02

软件中心会为你自动添加源地址并更新,所以你不用管,知道出现下面的的窗口,窗口被你点没有了,没关系,现在你可以在软件中心直接搜索插件了,然后点击“”install”安装就可以了,安装完毕之后别忘了重新启动火狐浏览器!

flashplayerforFF03

Arduino小车更换锂电池导致过载的问题分析

一直是用四节镍氢电池来作为小车的电源,某天在网上看到了一个 5V,9V,12V的多用锂电池电源,忍不住就买了个过来,本以为很轻松的换上就可以了,结果当接到12V的电源口子上面的时候,总是自动断开,进入保护模式,感觉很奇怪。

电源图片

image

该电源提供1.5A的极限电流,过流以后会自动切断电源。

感觉尽管小车有四个马达,但是功率不至于达到 12V*1.5A 这么恐怖的地步。

最后用我的直流电源来分析,得到惊人的发现平时的电流也就在300mA左右,但是,某个瞬间,会直接飙升到2.24A以上,这个电流实在是有些恐怖。

直流电源如下:

image

百思不地其解,究竟是为什么导致如此大的瞬间过载电流,幸好电源有过流保护,否则,非出事不可,锂电池可是标准的易燃易爆啊。

细想之下,发觉问题出在如下的代码上面

这段代码看似没有问题,当时也是为了方便才这样写的。

请注意 “停车”这两个字,当时为了使得小车在获得超声传感器的反馈后才会动作,因此要求小车运动一段距离以后就要停下来等待传感器。

但是如此操作导致一个严重的问题,那就是电机的启动负载问题,电机的启动电流非常大,频繁启动,尤其是多电机同步,会导致不可预测的瞬时启动功率过载,非常危险。小车可是有四个驱动电机啊!

虽然知道原因了,但是,却不好解决,原因在于,直流电机的软启动,国内的技术,唉,不提也罢,随便一个配件都是进口的,都要几张红的,关键是还没有给小车这么小的电机的专用的驱动模块,太难了,也不合适。

没办法,不过可以通过降低启动电压的方式来降低瞬时功率,代价就是电机输出功率的下降。于是把电压切换到9V,瞬时最大峰值电流一下子降到了1.2A左右。

勉强撮合。

不过最根本的解决方法,恐怕还是PWM来降低速度,电机一旦启动,就不要停下来,反而是最节能,最不容易烧毁控制芯片的。

的确是个问题。

VBScript 中读取环境变量

最近在编译的时候,需要用到VBScript,发现里面的某些调用路径是写死的实路径,因此修改了一下,提高灵活性一下。

Eclipse使用SVN插件报Failed to load JavaHL Library.错误

最近在使用Eclipse的SVN插件时总是弹出一个大大的对话框,报一个Failed to load JavaHL Library.错误

虽然不是很影响正常使用,但是当你编着编着代码就偶尔弹出这么个大框来着实不爽。

于是在网络上找了答案,按照其中一种方法操作,果然不再报错。

其法为:

1.windows->preferences->Team->SVN->SVN接口

2.选择SVNKit (Pure Java) xxxxxx

如下图所示

image

Android 中的FrameLayout 布局

最近在研究 Cocos2dX ,一直没弄明白 GLSurfaceView 是如何工作的,研究代码发现了动态创建了FrameLayout(单帧布局) ,特意研究了一下这个布局,信息,发现果然比较适合 OpenGL ES 之流的东西的绘制。

FrameLayout是五大布局中最简单的一个布局, 在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置,它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的 子元素之上,将前面的子元素部分和全部遮挡。显示效果如下,第一个TextView被第二个TextView完全遮挡,第三个TextView遮挡了第二 个TextView的部分位置。

image

参考的XML 信息

这样子,就可以保证,前面的Java控件的绘制,不会阻挡了后面的C++ 代码的绘制,加上JNI 的协助,基本上实现了本地代码跟其他Java的近乎实时的无缝交互。

Subclipse 1.6 卸载后Team菜单仍然存在的解决

最近从Ubuntu 12.04 升级到了 12.10,发现以前在Eclipse下面的SVN Subclipse 插件不能正常运行了。看了看原因,发现系统自带的SVN从1.6X升级到1.7X 了,当年哥在12.04下面折腾了半天,也没能方便升级到1.7X 啊,想不到,想不到。

去Subclipse官网看了下,似乎这个插件N久未曾更新,至少是人气不旺盛啊。于是想更换 成Eclipse自带的subversion,没想到卸载了所有的名字与subversion 相关的东西之后,竟然在Team菜单 中仍然存在快捷方式,另外就是装完subversion 以后在Window->Preferences->Team下面竟然有两个SVN ,明显是Subclipse 没有卸载干净。

找了半天,终于找到一个叫     CollabNet Merge Client    的东东,看名字,没有任何关系与 Subclipse,不过他的简介里面暴露了他,原来这个东东就算与Eclipse菜单对应的东东,吧这个卸载就可以了。

感觉是Eclipse自带的subversion 好些,当时是不知道怎么安装这个,才安装的Subclipse。这个安装完成后会提示安装相应的SVNkit 等等的依赖库,比较方便安装和使用。呵呵,强烈推荐这个。

安装方法:

  • Run Eclipse and select Help > Install New Software... from the main menu.
  • On the dialog that appears, select a pre-configured simultaneous release update site in the Work with combo-box. For example, for the Juno release, select the "Juno - http://download.eclipse.org/releases/juno" update site.
  • Wait a few seconds until the content of the selected update site is displayed under the combo-box.
  • Expand the Collaboration group and select the Subversive features that you would like to install. Certain Subversive features are required if you want to work with SVN, others are optional and offer some additional functionality. You can skip the optional features, if you wish.
  • Follow the next steps to install the selected Subversive features using the standard plug-in installation procedure. Reboot Eclipse after installation is complete.

当下图出现的时候,强烈建议选择SVNkit,请注意 SVNKit 1.3.8 对应的SVN 是 1.6X 的版本,SVNKit 1.7.X 是对应的SVN 1.7X的版本,JavaHL似乎跟不上Java的更新步伐,问题比较多,不推荐。

image

Arduino PWM 控制逻辑

对于直流电机的速度控制,目前流行的控制逻辑是PWM,关于 Arduino 的PWM,看看图片

image

板子上标注了“PWM”的区域就是管脚均可以用于这种输出。使用的函数是:

注意value值的范围是0~255。

PWM 英文是“Pulse-width modulation” ,PWM是用占空比不同的方波,来模拟“模拟输出”的一种方式。简而言之就是电脑只会输出0和1,那么想输出0.5怎么办呢?于是输出01010101….,平均之后的效果就是0.5了。

现在看看电机的常规驱动电路,H桥

image

使用单片机来控制直流电机的变速,一般采用调节电枢电压的方式,通过单片机控制PWM1,PWM2,产生可变的脉冲,这样电机上的电压也为宽度可变的脉冲电压。根据公式

U=aVCC

其中:U为电枢电压;a为脉冲的占空比(0<a<1);VCC直流电压源,这里为5V。

电动机的电枢电压受单片机输出脉冲控制,实现了利用脉冲宽度调制技术(PWM)进行直流电机的变速。

image

因为在H桥电路中,只有PWM1与PWM2电平互为相反时电机才能驱动,也就是PWM1与PWM2同为高电平或同为低电平时,都不能工作,所以上图中的实际脉冲宽度为B,

我 们把PWM波的周期定为1ms,占空比分100级可调(每级级差为10%),这样定时器T0每0.01ms产生一次定时中断,每100次后进入下一个 PWM波的周期。上图中,占空比是60%,即输出脉冲的为0.6ms,断开脉冲为0.4ms,这样电枢电压为5*60%=3V。

我们讨论的是可以正转反转的,如果只按一个方向转,我们就只要把PWM1置为高电平或低电平,只改变另一个PWM2电平的脉冲变化即可,,如下图(Q4导通,Q3闭合,电机只能顺时针调整转动速度)

image

下面看看摘抄的部分原理

直流电动机转速n=(U-IR)/Kφ

其中U为电枢端电压,I为电枢电流,R为电枢电路总电阻,φ为每极磁通量,K为电动机结构参数。

直 流电机转速控制可分为励磁控制法与电枢电压控制法。励磁控制法是控制磁通,其控制功率小,低速时受到磁饱和限制,高速时受到换向火花和换向器结构强度的限 制,而且由于励磁线圈电感较大动态响应较差,所以这种控制方法用得很少。大多数应用场合都使用电枢电压控制法。随着电力电子技术的进步,改变电枢电压可通 过多种途径实现,其中PWM(脉宽调制)便是常用的改变电枢电压的一种调速方法。

PWM调速控制的基本原理是按一个固定频率来接通和断开电源,并根据需要改变一个周期内接通和断开的时间比(占空比)来改变直流电机电枢上电压的"占空比",从而改变平均电压,控制电机的转速。在脉宽调速系统中,当电机通电时其速度增加,电机断电时其速度减低。只要按照一定的规律改变通、断电的时间,即可控制电机转速。而且采用PWM技术构成的无级调速系统.启停时对直流系统无冲击,并且具有启动功耗小、运行稳定的特点。

设电机始终接通电源时,电机转速最大为Vmax,且设占空比为D=t/T,则电机的平均速度Vd为:

Vd=VmaxD

由公式可知,当改变占空比D=t/T时,就可以得到不同的电机平均速度Vd,从而达到调速的目的。严格地讲,平均速度与占空比D并不是严格的线性关系,在一般的应用中,可将其近似地看成线性关系。 在直流电机驱动控制电路中,PWM信号由外部控制电路提供,并经高速光电隔离电路、电机驱动逻辑与放大电路后,驱动H桥下臂MOSFET的开关来改变直流电机电枢上平均电压,从而控制电机的转速,实现直流电机PWM调速。

原理知道了,那么看看 Arduino Mega 2560 的PWM控制端口信息

1) Arduino 2560 has 12 pins supporting PWM. They are from 2 to 13 included.
2) the PWM default frequency is 490 Hz for all pins, with the exception of pin 13 and 4,whose frequency is 980 Hz (I checked with an oscilloscope).
3) In order to change frequency on pin 'A', we have to change some value in the timer (or register), controlling pin 'A'. This is the list of timers in Arduino Mega 2560:
timer 0 (controls pin 13, 4);
timer 1 (controls pin 12, 11);
timer 2 (controls pin 10, 9);
timer 3 (controls pin 5, 3, 2);
timer 4 (controls pin 8, 7, 6);

As you can see, a given timer controls more than one pin (every change about a timer will affect all pins depending on it!).

4) You can access a timer simply changing in your code (tipically in the setup()), the value of variable TCCRnB, where 'n' is the number of register. So, if we want  to change the PWM frequency of pins 10 and 9,  we will have to act on TCCR2B .

5) The TCCRnB is a 8 bit number.  The first three bits (from right to left!) are called CS02, CS01, CS00, and they are the bits we have to change.
Those bits in fact represent an integer number (from 0 to 7) called 'prescaler' , that Arduino uses to generate the frequency for PWM.

6) First of all, we have to clear these three bits, i.e they must be all set to 0:

7) now that CS02, CS01, CS00  are clear, we write on them a new value:

now we have a new PWM frequency on pin 9 and 10!

I registered those values on all PWM pins, changing the value of prescaler (the only exception are pins 13 and 14, see later):

prescaler = 1 ---> PWM frequency is 31000 Hz
prescaler = 2 ---> PWM frequency is 4000 Hz
prescaler = 3 ---> PWM frequency is 490 Hz (default value)
prescaler = 4 ---> PWM frequency is 120 Hz
prescaler = 5 ---> PWM frequency is 30 Hz
prescaler = 6 ---> PWM frequency is <20 Hz

(prescalers equal t 0  or 7 are useless).

Those prescaler values are good for all timers (TCCR1B, TCCR2B, TCCR3B, TCCR4B) except for timer 0 (TCCR0B). In this case the values are:

prescaler = 1 ---> PWM frequency is 62000 Hz
prescaler = 2 ---> PWM frequency is 7800 Hz
prescaler = 3 ---> PWM frequency is 980 Hz (default value)
prescaler = 4 ---> PWM frequency is 250 Hz
prescaler = 5 ---> PWM frequency is 60 Hz
prescaler = 6 ---> PWM frequency is <20 Hz

Note that timer 0 is the one on which rely all time functions in Arduino: i.e., if you change this timer, function like delay() or millis() will continue to work but at a different timescale (quicker or slower!!!)

明天修改一下电机的控制代码,把小车代码修改一下,哈哈