Author Topic: Raspberry Pi Or Arduino  (Read 4175 times)

0 Members and 2 Guests are viewing this topic.

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4032
  • Country: nz
Re: Raspberry Pi Or Arduino
« Reply #25 on: October 05, 2018, 11:58:35 pm »
In this case 'large' is an integer somewhere between 1 and (2*32) - 1.

Until I started digging I didn't realize that the Arduino processors are mostly eight bit and the ability to handle numbers larger than 255 is done through the compiler!

Technically true, but not exactly challenging.

If the program has a 32 bit variable stored in registers A,B,C,D and the compiler wants to add a 32 bit variable stored in registers a,b,c,d to it then it's just a matter of emitting:

Code: [Select]
add A,a
adc B,b
adc C,c
adc D,d

Four instructions, eight bytes of code, four clock cycles at your 16 MHz (usually).

Compare that to the good old 6502, where those things are in, at *best* zero-page memory locations, not registers.

Code: [Select]
clc
lda A
adc a
sta A
lda B
adc b
sta B
lda C
adc c
sta C
lda D
adc d
sta D

Thirteen instructions, *twenty five* bytes of code, thirty eight clock cycles at typically 1 MHz.

The AVR in the Arduino is 8-bit, but it's (for this operation) 152 times faster than the 6502. And the code is three times smaller.

A heck of a lot of software on 6502 machines such as the Apple ][ used not only 32 bit arithmetic, but 32 bit FLOATING POINT. Done in software.

And almost nothing was actually done directly in the bulky machine code, but instead common operations were wrapped into some kind of simple interpreter, whether BASIC, Pascal, Sweet16, or threaded code (Forth/Logo style).
 
The following users thanked this post: GeorgeOfTheJungle

Offline martinjaymckee

  • Contributor
  • Posts: 16
Re: Raspberry Pi Or Arduino
« Reply #26 on: October 06, 2018, 06:42:31 am »
I think the standard Arduino is a good fit for this.  Over the years I have done a number of projects (using both the Aruduino IDE and the GCC compiler/libraries directly) which have done control with loop speeds over 100Hz and done a fair amount of processing (filters and PID) per loop.  The Arduino libraries can certainly get annoying at times and there are things (like single pin digital read/write) which are insanely inefficient.  But it is possible to get a project up and running very quickly.  One thing to remember is that you can always grow beyond the "basic" Arduino libraries.  The compiler is just standard C++ with a custom preprocessor at the front.  Now, granted, the preprocessor causes trouble with some C++ constructs sometimes.... Anyhow, I think you've made a good choice for your application. 

I would certainly recommend looking at Arduino tutorials (and the built-in examples).  I would not, however, recommend viewing them as examples of good programming.  As has been said before here, Arduino tends to train poor programmers.  Honestly, when learning the language I think it's better to have a C++ compiler on your computer so that you can write short test programs with specific constructs.  Then you can move that syntax into Arduino.  One of the biggest problems with programming in "Arduino" style is the rampant use of delays.  In the workshops I taught I would start with delay() on the first LED blink and then immediately show ways to get the same blink effect without blocking the processor.  Unfortunately, Arduino doesn't always play well with using the hardware interrupts (as mentioned above) as the run-time already uses a couple of the timers.  There are ways to do things well though, even within the constraints of the Arduino environment.

Cheers,
Martin Jay McKee
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4032
  • Country: nz
Re: Raspberry Pi Or Arduino
« Reply #27 on: October 06, 2018, 08:46:15 am »
I think the standard Arduino is a good fit for this.  Over the years I have done a number of projects (using both the Aruduino IDE and the GCC compiler/libraries directly) which have done control with loop speeds over 100Hz and done a fair amount of processing (filters and PID) per loop.  The Arduino libraries can certainly get annoying at times and there are things (like single pin digital read/write) which are insanely inefficient.

It's not *that* bad. The main problem I think is converting from logical pin number to port and offset very time. It's a few us at worst.

Quote
But it is possible to get a project up and running very quickly.  One thing to remember is that you can always grow beyond the "basic" Arduino libraries.  The compiler is just standard C++ with a custom preprocessor at the front.  Now, granted, the preprocessor causes trouble with some C++ constructs sometimes....

What problems?

As far as I know, all the Wiring preprocessor for C++ does is gather up all the function prototypes and put forward declarations at the start of the program, so you don't have to worry about order of functions. And then jam a #include on the start.

The Wiring preprocessor for Java just wraps everything in a class and puts an import at the top (as method order already doesn't matter in Java).

Quote
One of the biggest problems with programming in "Arduino" style is the rampant use of delays.  In the workshops I taught I would start with delay() on the first LED blink and then immediately show ways to get the same blink effect without blocking the processor.  Unfortunately, Arduino doesn't always play well with using the hardware interrupts (as mentioned above) as the run-time already uses a couple of the timers.  There are ways to do things well though, even within the constraints of the Arduino environment.

Yes, it's a shame. It's really not that hard to convert the standard...

Code: [Select]
void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {

    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);
}

... to ...

Code: [Select]
const int LEDOnTime = 1000;
const int LEDOffTime = 1000;
int LEDState = LOW;
long nextLEDTime = 0;

void setup() {
    pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
    long now = millis();

    if (now - nextLEDTime >= 0){
        if (LEDState == LOW){
            LEDState = HIGH;
            nextLEDTime += LEDOnTime;
        } else {
            LEDState = LOW;
            nextLEDTime += LEDOffTime;
        }
        digitalWrite(LED_BUILTIN, LEDState);
    }
}

(there's a slight hack here .. millis() actually returns unsigned long, but to get proper behaviour on the 49.7 day wrap-around we need a signed result from the subtraction, so it's easiest just to jam millis() into a signed long.)

This is easily extendable to an arbitrary number of things that need to be timed. If there are really a lot of them then it would be better to build a binary heap-based priority queue. That's pretty easy but for some reason no one seems to have bothered to do it for Arduino. You could even make it interrupt driven,.
 
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4032
  • Country: nz
Re: Raspberry Pi Or Arduino
« Reply #28 on: October 06, 2018, 08:53:08 am »
Unless you are doing some heavy number crunching, native 32 bit is not that big a deal, but there is Arduino Due which is 32 bit Cortex M3 at 84MHz. I think you will be pleasantly surprised by the power and ease of the Arduino ecosystem if you are coming from a PICAXE background.

If you want some real number crunching power then there is the HiFive1 with a 32 bit RISC-V processor at 320 MHz. It has 6x less SRAM than the Due (16 kB) but 32x more flash for your program code (16 MB).

https://www.crowdsupply.com/sifive/hifive1

https://www.digikey.com/products/en?keywords=hifive1

https://nz.mouser.com/ProductDetail/Crowd-Supply/cs-hifive1-01?qs=%2fha2pyFaduiYWq09B7sm7wDmAaLBQ5QCD7BSHMmGALI%3d

 

Offline donotdespisethesnake

  • Super Contributor
  • ***
  • Posts: 1093
  • Country: gb
  • Embedded stuff
Re: Raspberry Pi Or Arduino
« Reply #29 on: October 06, 2018, 09:31:03 am »
It has 6x less SRAM than the Due (16 kB)

That's a total waste of time then! But thanks for the spam.
Bob
"All you said is just a bunch of opinions."
 

Offline GeorgeOfTheJungle

  • Super Contributor
  • ***
  • !
  • Posts: 2699
  • Country: tr
Re: Raspberry Pi Or Arduino
« Reply #30 on: October 06, 2018, 09:40:47 am »
Unless you are doing some heavy number crunching, native 32 bit is not that big a deal, but there is Arduino Due which is 32 bit Cortex M3 at 84MHz. I think you will be pleasantly surprised by the power and ease of the Arduino ecosystem if you are coming from a PICAXE background.
If you want some real number crunching power then there is the HiFive1 with a 32 bit RISC-V processor at 320 MHz. It has 6x less SRAM than the Due (16 kB) but 32x more flash for your program code (16 MB).

The ARM "bluepill" arduino is hard to beat at ~ $2:

http://wiki.stm32duino.com/index.php?title=Blue_Pill
http://wiki.stm32duino.com/index.php?title=Installation
https://robotdyn.com/stm32f103-stm32-arm-mini-system-dev-board-stm-firmware.html

Quote
The F103C8 ST microcontroller is declared to have 64 KB of flash, but virtually all the C8 microcontrollers tested have 128 KB of flash instead
« Last Edit: October 06, 2018, 09:44:35 am by GeorgeOfTheJungle »
The further a society drifts from truth, the more it will hate those who speak it.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4032
  • Country: nz
Re: Raspberry Pi Or Arduino
« Reply #31 on: October 06, 2018, 10:06:56 am »
It has 6x less SRAM than the Due (16 kB)

That's a total waste of time then! But thanks for the spam.

Still a lot of RAM for an Arduino, the majority of which sold have 2 KB -- which is itself enough for one hell of a state machine. It all depends on what your task at hand is. Choice is good.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 4032
  • Country: nz
Re: Raspberry Pi Or Arduino
« Reply #32 on: October 06, 2018, 10:09:40 am »
Certainly that's very nice for $2 or $3.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf