- fchao-Sinus-Wechselrichter AliExpress         
Ergebnis 1 bis 10 von 49

Thema: HaikuVM A Java VM for ARDUINO and other micros using the leJOS runtime.

Hybrid-Darstellung

Vorheriger Beitrag Vorheriger Beitrag   Nächster Beitrag Nächster Beitrag
  1. #1
    So,
    blinken mit Threads funktioniert jetzt.
    Etwas verwirrt hat mich deine Frage nach der Einstellung von TCCR0B. Der nibobee ist standardmäßig mit einem atmega16 ausgestattet, der für den Timer0 nur ein TCCR hat. Ich war mir daher nicht sicher, ober der Timer0 überhaupt der richtige Timer für die Zeitmessung ist, wenn der atmega16 benutzt wird. Aber dein Hinweis auf SimpleOS.c hat da weiter geholfen.
    Ich habe TCCR0 auf 3 gesetzt, also die beiden Clock-Select-Bits aktiviert und so den Prescaler clock/64 gewählt. Das bedeutet allerdings, dass meine Millisekunden in Wirklichkeit ca. 1,12 Millisekunden dauern. Soweit ich sehe, müsste ich für einen korrekten Wert SimpleOS.c anpassen. Genügt es, wenn ich dies in der Kopie in myProject\haikuvM mache, oder muss ich sonst noch etwas beachten?

    Der Nibobee-Programmer nimmt, soweit ich erkennen kann, keine Commandline-Optionen an. Dahingehende Versuche hat er ignoriert.

    Hier folgen noch meine Anpassungen an deiner avr\tutorial\BlinkAsync.java:
    Code:
    ...
    import haiku.vm.NativeCVariable8;
    ...
    @NativeCVariable8
    public static volatile int SREG;
    ...
    // In main():
    SREG |= (1 << 7); // Interrupts ein		
    // Timer-Konfiguration: Prescaler auf 1/64
    TCCR0 |= (1 << CS01);
    TCCR0 |= (1 << CS00);
    TIMSK |= (1 << TOIE0); // Timer-Overflow-Interrupt-Enable Timer0
    ...
    Ich werde als nächstes versuchen, für die beiden externen Interrupts INT0 und INT1 Routinen zu schreiben analog zu deiner Idee für den Timer2.

    Vielen Dank für deine Hilfe, ohne wäre ich nicht weiter gekommen.
    Geändert von mlade (22.02.2013 um 20:46 Uhr) Grund: Code-Blöcke eingefügt

  2. #2
    Neuer Benutzer Öfters hier
    Registriert seit
    28.10.2012
    Beiträge
    26
    Hallo mlade,

    1) Naja, ich kenne nicht jeden Microcontroller so genau und wußte nicht, dass der ATmega16 das Register TCCR0B nicht kennt. Sorry für das Stiften von Verwirrung.
    In 'SimpleOS.c' findest du in der Funktion millis() die Zeile:
    Code:
        return m * 128 / 125;
    Hier solltest du auf ((m * 128 / 125) * 1.12) ändern. (Also, ich habe rein rechnerisch den Faktor 1.066667!) Probiere deshalb doch mal:
    Code:
        return m * 71 / 65;
    oder noch genauer
    Code:
        return m * 225 / 206;
    oder noch genauer
    Code:
        return m * 2048 / 1875;
    In der nächsten Version werde ich das dann via 'HaikuVM.properties' konfigurierbar machen. Aber berichte erst mal ob der Gedanke in der Praxis taugt.

    2) Besser ist für dich wenn du die Änderung in 'haikuVM\SimpleOS.c' machst und dann (nur) 'myCProject\haikuVM\' löschst. Nur dann profitierst du auch bei deinem nächsten NIBOBEE Projekt ( vielleicht in 'myCProject2\' ) von dieser Änderung.

    3) Wie heißt denn der Nibobee-Programmer genau. Dann kann ich ihn wenigstens bei der nächsten Version (eben ganz ohne Commandline-Option) eintragen. In der Hoffnung, dass es für NIBOBEEisten dann noch etwas bequemer wird.

    4) Aah, und wie ich sehe bist du von get-/setMemory(..) zu direct memory access übergegangen. (Ist doch bequemer so, oder?)

    5) (Ist als Info gedacht: ) Ach ja, noch was, allgemein sind die Interrupts bei HaikuVM schon enabled.
    Code:
    ...
    // In main():
    SREG |= (1 << 7); // Interrupts ein       
    ...
    Schadet nicht, ist aber Doppeltgemoppelt. (SREG habe ich jetzt trotzdem in AVRConstants.java aufgenommen. Danke für den versteckten Tipp.)

  3. #3
    Der Programmer heißt NIBObeeProgrammer.exe und wird standardmäßig in "C:\Program Files (x86)\NIBObeeLib" installiert.

    Ich habe ein Problem beim Versuch, c einzubinden. So wie es aussieht, kennt der Compiler JNIEnv nicht. Die Fehlermeldungen lauten:
    Code:
    ../../int0.c:11: error: expected ')' before '*' token
    ../../int0.c: In function 'native_IntTest_getCount0_J':
    ../../int0.c:30: error: 'JNIEnv' undeclared (first use in this function)
    ../../int0.c:30: error: (Each undeclared identifier is reported only once
    ../../int0.c:30: error: for each function it appears in.)
    ../../int0.c:30: error: 'env' undeclared (first use in this function)
    ../../int0.c:31: warning: implicit declaration of function 'Java_IntTest_getCount0'
    int0.c:
    Code:
    #include "haikuJ2C.h"				// Header-Datei statt jni.h?
    #include <avr/interrupt.h>
    
    volatile static jlong jl_intcount0 = 0;
    
    SIGNAL (SIG_INTERRUPT0)				//INT0_vect
    {
          jl_intcount0 ++;
    }
    
    jlong Java_IntTest_getCount0( JNIEnv *env, jobject obj) {
        jlong value;
        uint8_t oldSREG = SREG;
        cli();
        value= jl_intcount0;
        SREG = oldSREG;
        return value;
    }
    
    
    /**
     * Automatic generated (see ./myCProject/haikuC/haikuJNI.c)
     * getTimer2()J
     */
    
     void native_IntTest_getCount0_J(void) {
        pushTop();    // Save variable top onto stack.
        {
            jobject    obj = NULL;
            JNIEnv *env = NULL;
            top.j = Java_IntTest_getCount0(env, obj);
        }
        // Variable top holds the return value. But we have to push the lower half.
        pushTop0();
    }
    Ich vermute mal, dass alles ab Zeile 20 raus muss, aber allein daran liegt es nicht. Ich vermisse auch das Einbinden von jni.h. Irgendwie scheint ein Schritt zu fehlen...

  4. #4
    Neuer Benutzer Öfters hier
    Registriert seit
    28.10.2012
    Beiträge
    26
    Hallo mlade,

    1) Danke für die Info zum Programmer. Wird gleich eingebaut.

    2) Ja, das stimmt, noch gibt es JNIEnv nicht. (Sorry, noch bin ich nicht fertig mit dem Interrupt- und JNI-Tutorial und auch 'jni.h' gibt es noch nicht.)
    JNIEnv bzw. env wird aber auch in 'int0.c' nicht gebraucht. Deshalb kannst du es in 'int0.c' wie folgt deklarieren:
    Code:
    typedef void JNIEnv;
    3) Aber nein, alles ("ab Zeile 20") kann und muß drin bleiben. Du hast es dir nur schwer gemacht und 'native_IntTest_getCount0_J()' tatsächlich selbst geschrieben. Schau doch mal wirklich in './myCProject/haikuC/haikuJNI.c' nach. Da sollte diese Funktion, frisch generiert, drin stehen. (Falls du sie schon in Java (als native) deklariert hast.)

    4) Welche IDE nutzt du? Falls Eclipse, hat dir das hier geholfen?

  5. #5
    Neuer Benutzer Öfters hier
    Registriert seit
    25.01.2013
    Beiträge
    12
    Hallo haikuvm,
    Ich wollte nun den NXT mit dem Arduino über I2C verbinden. Anscheinend gibt es mit haiku.avr.lib.arduino.save.TWI schon eine Klasse, die den Einstieg erleichtern soll.
    Gibt es eine Dokumentation oder die Source zu zu dieser Klasse?
    Den Arduino mit haikuvm möchte ich als Slave betreiben.

  6. #6
    Neuer Benutzer Öfters hier
    Registriert seit
    28.10.2012
    Beiträge
    26
    Hallo janvp,

    1) Da habe ich wohl den Überblick verloren. Wo hast du 'haiku.avr.lib.arduino.save.TWI' denn her? Welcher download welches SVN release? Denn das wollte ich erst veröffentlichen wenn es wirklich funktioniert.

    2) Statt dessen kann ich dir einen funktionierenden TWI Master für den LSM303DLM anbieten, rein in Java geschreiben. (Obwohl, wie man sieht, es ist ein gefühlvoll gemoddetes C Programm.) In der Hoffnung, das es dir hilft. Dies läuft z.Zt. am besten mit der arduino Config und dann mit einem putty auf 57600 baud.
    Wenn du mit deinem TWI Slave fertig bist übernehme ich den gerne auch in die Gallerie. Und die wird beim nächtsten release mit veröffentlicht.
    Code:
    package avr.gallerie.twi;
    
    import static haiku.avr.ATmega328p.*;
    import static haiku.avr.lib.arduino.WProgram.*;
    import static haiku.avr.lib.arduino.Print.*;
    import static java.lang.Math.*;
    
    /**
     * http://www.pololu.com/catalog/product/1273
     * 
     * arduino-1.0.2 -> Binäre Sketchgröße: 9.446 Bytes
     * HaikuVM -> 15104 (with code reduced to the min: 11684)
     * 
     *
     */
    public class Compass {
        /*
         * compass: for the Orangutan LV, SV, SVP, and X2.
         *
         * This example program demonstrates how to use the LSM303DLM 3D compass and
         * accelerometer carrier with an Orangutan robot controller.
         *
         * The LSM303DLM carrier should be connected to the Orangutan's I2C pins; on the
         * LV and SV, these are PC5 and PC4 for SCL and SDA, respectively, and on the
         * SVP and X2, these are PC0 and PC1, respectively. (PC0 and PC1 are LCD data
         * lines on the X2, but this code will work on it if the LCD is not used, or
         * used in 4-bit mode.) 
         *
         * http://www.pololu.com
         * http://forum.pololu.com
    
        #include <avr/io.h>  
        #include <pololu/orangutan.h>  
        #include <math.h>
        #include <stdlib.h>
        #include <string.h>
        #include "vector.h"
    
         * This program assumes that the LSM303DLM carrier is oriented with X pointing
         * to the right, Y pointing backward, and Z pointing down (toward the ground).
         * The code compensates for tilts of up to 90 degrees away from horizontal.
         * Vector p should be defined as pointing forward, parallel to the ground,
         * with coordinates {X, Y, Z}.
         */
        public static class vector {
            public vector(double a, double b, double c) {
                x = a;
                y = b;
                z = c;
            }
    
            public vector() {
    
            }
    
            public void show(String msg) {
                print(msg+" (");
                print(x); print(", ");
                print(y); print(", ");
                print(z); println(")");
            }
    
            public double x, y, z;
        }
        
        /**
         *     Newton's method 
         */
        static double xsqrt(double x) {
            if (!(x > 0))
                return (x == 0) ? x : Double.NaN;
            double x0=0.1*x;
            double x1=x0-(x0*x0-x)/(2*x0);
            while(abs(x1/x0-1)>0.001) {
                x0=x1;
                x1=x0-(x0*x0-x)/(2*x0);
            }
            return x1;
        }
        
        static void vector_cross(final vector a, final vector b, vector out)
        {
            out.x = a.y * b.z - a.z * b.y;
            out.y = a.z * b.x - a.x * b.z;
            out.z = a.x * b.y - a.y * b.x;
        }
    
        static double vector_dot(final vector a, final vector b)
        {
          return a.x * b.x + a.y * b.y + a.z * b.z;
        }
    
        static void vector_normalize(vector a)
        {
            double dot = vector_dot(a, a);
            double mag = sqrt(dot);
            //print("dot="); println(dot);
            //print("mag="); println(mag);
            a.x /= mag;
            a.y /= mag;
            a.z /= mag;
        }
    
        static vector p = new vector(0, -1, 0);
    
        /*
         * m_max and m_min are calibration values for the maximum and minimum
         * measurements recorded on each magnetic axis, which can vary for each
         * LSM303DLM. You should replace the values below with max and min readings from
         * your particular device.
         *
         * To obtain the max and min values, you can use this program's
         * calibration mode, which is enabled by pressing one of the pushbuttons. While
         * calibration mode is active, point each of the axes of the LSM303DLM toward
         * and away from the earth's North Magnetic Pole. Due to space constraints on an
         * 8x2 LCD, only one axis is displayed at a time; each button selects an axis to
         * display (top = X, middle = Y, bottom = Z), and pressing any button a second
         * time exits calibration mode and returns to normal operation.
         */
        static vector m_max = new vector(527, 419, 417);
        static vector m_min = new vector(-472, -584, -562);
    
        static String ribbon = "   N   NE   E   SE   S   SW   W   NW   N   "; // 42
    
        static enum calibration_mode {CAL_NONE, CAL_X, CAL_Y, CAL_Z};
    
        static void i2c_start() {  
            TWCR = (byte)((1 << TWINT) | (1 << TWSTA) | (1 << TWEN)); // send start condition  
            while (((TWCR & (1 << TWINT))==0));  
        }  
          
        static void i2c_write_byte(int b) {  
            TWDR = (byte)(b);              
            TWCR = (byte)((1 << TWINT) | (1 << TWEN)); // start address transmission  
            while (((TWCR & (1 << TWINT))==0));  
        }  
          
        static int i2c_read_byte() {  
            TWCR = (1 << TWINT) | (1 << TWEA) | (1 << TWEN); // start data reception, transmit ACK  
            while (((TWCR & (1 << TWINT))==0));  
            return TWDR&0xff;
        }  
    
        static int i2c_read_last_byte() {  
            TWCR = (byte)((1 << TWINT) | (1 << TWEN)); // start data reception
            while (((TWCR & (1 << TWINT))==0));  
            return TWDR&0xff;
        }  
          
        static void i2c_stop() {  
            TWCR = (byte)((1 << TWINT) | (1 << TWSTO) | (1 << TWEN)); // send stop condition  
        }  
    
        // Returns a set of acceleration and raw magnetic readings from the cmp01a.
        static void read_data_raw(vector a, vector m)
        {
            // read accelerometer values
            i2c_start();
            i2c_write_byte(0x30); // write acc
            i2c_write_byte(0xa8); // OUT_X_L_A, MSB set to enable auto-increment
            i2c_start();          // repeated start
            i2c_write_byte(0x31); // read acc
            /*unsigned*/ int axl = i2c_read_byte();
            /*unsigned*/ int axh = i2c_read_byte();
            /*unsigned*/ int ayl = i2c_read_byte();
            /*unsigned*/ int ayh = i2c_read_byte();
            /*unsigned*/ int azl = i2c_read_byte();
            /*unsigned*/ int azh = i2c_read_last_byte();
            i2c_stop();
    
            // read magnetometer values
            i2c_start(); 
            i2c_write_byte(0x3C); // write mag
            i2c_write_byte(0x03); // OUTXH_M
            i2c_start();          // repeated start
            i2c_write_byte(0x3D); // read mag
            /*unsigned*/ int mxh = i2c_read_byte();
            /*unsigned*/ int mxl = i2c_read_byte();
            /*unsigned*/ int mzh = i2c_read_byte();
            /*unsigned*/ int mzl = i2c_read_byte();
            /*unsigned*/ int myh = i2c_read_byte();
            /*unsigned*/ int myl = i2c_read_last_byte();
            i2c_stop();
    
            a.x = axh << 8 | axl;
            a.y = ayh << 8 | ayl;
            a.z = azh << 8 | azl;
            m.x = mxh << 8 | mxl;
            m.y = myh << 8 | myl;
            m.z = mzh << 8 | mzl;
        }
    
        // Returns a set of acceleration and adjusted magnetic readings from the cmp01a.
        static void read_data(vector a, vector m)
        {
            read_data_raw(a, m);
    
            // shift and scale
            m.x = ((m.x - m_min.x) / (m_max.x - m_min.x) * 2 - 1.0);
            m.y = ((m.y - m_min.y) / (m_max.y - m_min.y) * 2 - 1.0);
            m.z = ((m.z - m_min.z) / (m_max.z - m_min.z) * 2 - 1.0);
        }
    
        // Returns a heading (in degrees) given an acceleration vector a due to gravity, a magnetic vector m, and a facing vector p.
        static int get_heading(final vector a, final vector m, final vector p)
        {
            vector E = new vector();
            vector N = new vector();
    
            // cross magnetic vector (magnetic north + inclination) with "down" (acceleration vector) to produce "east"
            vector_cross(m, a, E);
            vector_normalize(E);
    
            // cross "down" with "east" to produce "north" (parallel to the ground)
            vector_cross(a, E, N);
            vector_normalize(N);
    
            // compute heading
            int heading = (int) Math.round(atan2(vector_dot(E, p), vector_dot(N, p)) * 180 / PI);
            if (heading < 0)
                heading += 360;
            return heading;
        }
    
        /**
         * @param args
         */
        public static void main(String[] args)
        {
            DDRC = 0;                              // all inputs
            PORTC = (byte)((1 << PORTC4) | (1 << PORTC5)); // enable pull-ups on SDA and SCL, respectively
    
            TWSR = 0;   // clear bit-rate prescale bits
            TWBR = 17;  // produces an SCL frequency of 400 kHz with a 20 MHz CPU clock speed
    
            //clear();  
            //enable accelerometer
            i2c_start(); 
            i2c_write_byte(0x30); // write acc
            i2c_write_byte(0x20); // CTRL_REG1_A
            i2c_write_byte(0x27); // normal power mode, 50 Hz data rate, all axes enabled
            i2c_stop();
    
            //enable magnetometer
            i2c_start(); 
            i2c_write_byte(0x3C); // write mag
            i2c_write_byte(0x02); // MR_REG_M
            i2c_write_byte(0x00); // continuous conversion mode
            i2c_stop();
    
            vector a=new vector(), m=new vector();
            //char ribbon_segment[] = new char[8];
            /*unsigned*/ char button;
            calibration_mode mode = calibration_mode.CAL_NONE;
            vector cal_m_max = new vector();
            vector cal_m_min = new vector();
    
            println(ribbon);
            while(true)
            {
                // see if a button was pressed to enable calibration mode
                // each button displays max and min measurements for one axis:
                // top = X, middle = Y, bottom = Z
                // if any button is pressed a second time, return to normal mode
                //while (Serial.available()==0);
                if (Serial.available()>0) {
                    button = (char)Serial.read();
                    //println(":"+button);
                } else {
                    button = ' ';
                    //println("-");
                }
    
                if (button=='x')
                {
                    if (mode != calibration_mode.CAL_X)
                        mode = calibration_mode.CAL_X;
                    else
                        mode = calibration_mode.CAL_NONE;
                }
                else if (button=='y')
                {
                    if (mode != calibration_mode.CAL_Y)
                        mode = calibration_mode.CAL_Y;
                    else
                        mode = calibration_mode.CAL_NONE;
                }
                else if (button=='z')
                {
                    if (mode != calibration_mode.CAL_Z)
                        mode = calibration_mode.CAL_Z;
                    else
                        mode = calibration_mode.CAL_NONE;
                }
    
                
                if (mode == calibration_mode.CAL_NONE) // normal mode - display heading and compass ribbon
                {
                    vector a_avg = new vector(), m_avg = new vector();
                
                    // take 5 acceleration and magnetic readings and average them
                    for(int i = 0; i < 5; i++)
                    {
                        read_data(a, m);
    
                        a_avg.x += a.x;
                        a_avg.y += a.y;
                        a_avg.z += a.z;
                        m_avg.x += m.x;
                        m_avg.y += m.y;
                        m_avg.z += m.z;
                    }
    
                    a_avg.x /= 5;
                    a_avg.y /= 5;
                    a_avg.z /= 5;
                    m_avg.x /= 5;
                    m_avg.y /= 5;
                    m_avg.z /= 5;
    
                    int heading = get_heading(a_avg, m_avg, p);
    
                    // select the portion of the ribbon to display
    //                strlcpy(ribbon_segment, &ribbon[(heading + 5) / 10], 8);
    
                    //clear();
                    //print("heading: ");  println(heading);
                    heading=(heading + 5) / 10;
                    for (int i = 0; i < 42; i++) {
                        print(' ');
                    }
                    print('\r');
                    for (int i = 0; i < heading; i++) {
                        print(' ');
                    }
                    print('V');  // center indicator
                    print((char)8);
                    
                    
    //                lcd_goto_xy(4, 0);
    //                print('v');  // center indicator
    //                lcd_goto_xy(1, 1);
    //                print(ribbon_segment); // ribbon segment
                }
                else // calibration mode - record and display max/min measurements
                {
                    read_data_raw(a, m);
                    if (m.x < cal_m_min.x) cal_m_min.x = m.x;
                    if (m.x > cal_m_max.x) cal_m_max.x = m.x;
                    if (m.y < cal_m_min.y) cal_m_min.y = m.y;
                    if (m.y > cal_m_max.y) cal_m_max.y = m.y;
                    if (m.z < cal_m_min.z) cal_m_min.z = m.z;
                    if (m.z > cal_m_max.z) cal_m_max.z = m.z;
    
                    //clear();
    
                    switch (mode)
                    {
                        case CAL_X:
                            print("Xmax: ");
                            print(cal_m_max.x);
                            print(" min: ");
                            println(cal_m_min.x);
                            break;
                        case CAL_Y:
                            print("Ymax: ");
                            print(cal_m_max.y);
                            print(" min: ");
                            println(cal_m_min.y);
                            break;
                        default:
                            print("Zmax: ");
                            print(cal_m_max.z);
                            print(" min: ");
                            println(cal_m_min.z);
                            break;
                    }
                }
    
                delay(100);
            }
        }
    }
    3) Mit welcher IDE arbeitest du?

  7. #7
    Neuer Benutzer Öfters hier
    Registriert seit
    25.01.2013
    Beiträge
    12
    Hallo haikuvm,

    ich benutze Eclipse, aber wenn ich wie auf deiner Seite beschrieben vorgehe, bekommen ich immer die Ausgabe 'make not found'. Deswegen gebe ich die Befehle zum Ausführen immer noch im Terminal ein. Das funktioniert auch.

    'haiku.avr.lib.arduino.save.TWI' befindet sich im bootstrap.jar von Version 1.0.1.

    Zwei Sachen sind mir nochnicht ganz klar geworden:
    Gibt es die Möglichkeit auf Interrups zu reagieren?
    Wie schreibt man native code für haikuvm?

Ähnliche Themen

  1. [ERLEDIGT] [ARDUINO] 2 Motoren + Servo mit dem Arduino Uno steuern
    Von Torrentula im Forum C - Programmierung (GCC u.a.)
    Antworten: 0
    Letzter Beitrag: 31.08.2011, 16:31
  2. ARDUINO ARDUINO l293D + MegaservoLib
    Von m1ch15inner im Forum Microcontroller allgemeine Fragen/Andere Microcontroller
    Antworten: 2
    Letzter Beitrag: 11.07.2011, 11:50
  3. Runtime Error! in AVR Studio
    Von NemesisoD im Forum C - Programmierung (GCC u.a.)
    Antworten: 6
    Letzter Beitrag: 05.01.2007, 19:30
  4. (LEGO MINDSTORMS) lejos Klassen Facharbeit
    Von George Dorn im Forum Allgemeines zum Thema Roboter / Modellbau
    Antworten: 0
    Letzter Beitrag: 01.03.2006, 16:29
  5. lego, lejos, logitech ;)
    Von maze2k im Forum Allgemeines zum Thema Roboter / Modellbau
    Antworten: 7
    Letzter Beitrag: 21.05.2005, 22:21

Stichworte

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •  

12V Akku bauen