Nieuws:

Welkom, Gast. Alsjeblieft inloggen of registreren.
Heb je de activerings-mail niet ontvangen?

Auteur Topic: Python --> C/C++  (gelezen 9962 keer)

Python --> C/C++
« Gepost op: 2021/04/11, 14:39:16 »
Ik probeer dit stukje programma in (micro)Python om te zetten naar C/C++ voor een Arduino.

(micro)Python
Dit stukje programma leest de gegevens van een sensor.
De inhoud wordt gesplitst m.b.v. de  komma (,) in 4 variablelen. (q0, q1,q2 en q3)

.............
.............
dataPacket=ad.readline()        # deze lijn leest de input van een sensor. b.v. "-0.01,0.55,-0.7,-1"
dataPacket=str(dataPacket,'utf-8')     
splitPacket=dataPacket.split(",")         
q0=float(splitPacket[0])
q1=float(splitPacket[1])
q2=float(splitPacket[2])
q3=float(splitPacket[3])
...........
...........

Tijdens het testen ben ik tot zover gekomen:

C/C++ (Arduino)
//  https://www.codingame.com/playgrounds/14213/how-to-play-with-strings-in-c/string-split

void setup(){
  Serial.begin(115200);
}

void loop(){

  char dataPacket[] = "-0.01,0.55,-0.7,-1";
  char delim[] = ",";
  char *ptr = strtok(dataPacket, delim);

   while (ptr != NULL){
    Serial.println(ptr);
    ptr = strtok(NULL, delim);
   
   q0 = float .......???;   
   q1 = float .......???;   
   q2 = float .......???;   
   q3 = float .......???;   
  }
  // delay(1000);
}

Output van dit programma is:



Mijn probleem is hoe ik de waarden van de gesplitste string in C/C++ kan  toevoegen aan q0, q1,q2,q3.  :|
𝕸𝖎𝖘𝖈𝖊𝖗𝖊 𝖚𝖙𝖎𝖑𝖊 𝖉𝖚𝖑𝖈𝖎. (Ter leering ende vermaeck)
𝕹𝖎𝖑 𝖛𝖔𝖑𝖊𝖓𝖙𝖎𝖇𝖚𝖘 𝖆𝖗𝖉𝖚𝖚𝖒. (Niets is moeilijk voor hen die willen)
https://henk.oegema.com  (Op RaspberryPi2)
Registered linux user 520520.  In gebruik: Ubuntu  24.04 Hobby's: Radio Amateur callsign: PA2HO.  Interesses: Raspberry Pi & Arduino & TELLO drone (voor AI)

Re: Python --> C/C++
« Reactie #1 Gepost op: 2021/04/11, 19:57:29 »
Zoiets werkt als je zeker weet dat er in dataPacket[] altijd netjes 4 getallen zitten bij splitsen op komma. Ik ben zeker geen C-expert, dus voor eigen risico  ;) C is een mijnenveld waarin je heel makkelijk een stapje verkeerd zet.

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int main (int argc, char *argv[])
{
    char dataPacket[] = "-0.01,0.55,-0.7,-1";
    char delim[] = ",";   
       
    float q0 = atof(strtok(dataPacket, delim));
    float q1 = atof(strtok(NULL, delim));
    float q2 = atof(strtok(NULL, delim));
    float q3 = atof(strtok(NULL, delim));
   
    printf("q0 = %f\n", q0);
    printf("q1 = %f\n", q1);
    printf("q2 = %f\n", q2);
    printf("q3 = %f\n", q3);
   
    return 0;
}

Re: Python --> C/C++
« Reactie #2 Gepost op: 2021/04/12, 12:47:54 »
Erik.

Het is bijna goed.    ;)
De waarde van q0 is ok.
Echter de waarden van q1,q2,q3 zijn allemaal  0.00   :'(


void setup(){
  Serial.begin(115200);
}

void loop(){

  char dataPacket[] = "-0.01,0.55,-0.7,-1";
  char delim[] = ",";
  char *ptr = strtok(dataPacket, delim);

  float q0 = atof(strtok(dataPacket, delim));
  float q1 = atof(strtok(NULL, delim));
  float q2 = atof(strtok(NULL, delim));
  float q3 = atof(strtok(NULL, delim));
 
  Serial.println(q0);
  Serial.println(q1);
  Serial.println(q2);
  Serial.println(q3);
 
  delay(1000);
}
𝕸𝖎𝖘𝖈𝖊𝖗𝖊 𝖚𝖙𝖎𝖑𝖊 𝖉𝖚𝖑𝖈𝖎. (Ter leering ende vermaeck)
𝕹𝖎𝖑 𝖛𝖔𝖑𝖊𝖓𝖙𝖎𝖇𝖚𝖘 𝖆𝖗𝖉𝖚𝖚𝖒. (Niets is moeilijk voor hen die willen)
https://henk.oegema.com  (Op RaspberryPi2)
Registered linux user 520520.  In gebruik: Ubuntu  24.04 Hobby's: Radio Amateur callsign: PA2HO.  Interesses: Raspberry Pi & Arduino & TELLO drone (voor AI)

Re: Python --> C/C++
« Reactie #3 Gepost op: 2021/04/12, 16:05:06 »
Zoiets werkt als je zeker weet dat er in dataPacket[] altijd netjes 4 getallen zitten bij splitsen op komma. Ik ben zeker geen C-expert, dus voor eigen risico  ;) C is een mijnenveld waarin je heel makkelijk een stapje verkeerd zet.

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

int main (int argc, char *argv[])
{
    char dataPacket[] = "-0.01,0.55,-0.7,-1";
    char delim[] = ",";   
       
    float q0 = atof(strtok(dataPacket, delim));
    float q1 = atof(strtok(NULL, delim));
    float q2 = atof(strtok(NULL, delim));
    float q3 = atof(strtok(NULL, delim));
   
    printf("q0 = %f\n", q0);
    printf("q1 = %f\n", q1);
    printf("q2 = %f\n", q2);
    printf("q3 = %f\n", q3);
   
    return 0;
}

In jouw programma werkt het WEL.  =D
In mijn Arduino (nog niet)
𝕸𝖎𝖘𝖈𝖊𝖗𝖊 𝖚𝖙𝖎𝖑𝖊 𝖉𝖚𝖑𝖈𝖎. (Ter leering ende vermaeck)
𝕹𝖎𝖑 𝖛𝖔𝖑𝖊𝖓𝖙𝖎𝖇𝖚𝖘 𝖆𝖗𝖉𝖚𝖚𝖒. (Niets is moeilijk voor hen die willen)
https://henk.oegema.com  (Op RaspberryPi2)
Registered linux user 520520.  In gebruik: Ubuntu  24.04 Hobby's: Radio Amateur callsign: PA2HO.  Interesses: Raspberry Pi & Arduino & TELLO drone (voor AI)

Re: Python --> C/C++
« Reactie #4 Gepost op: 2021/04/12, 16:16:19 »
Hoera, hoera. Het werkt.  =D
Bij deze mijn dank aan Erik voor de genomen moeite.   ^-^
𝕸𝖎𝖘𝖈𝖊𝖗𝖊 𝖚𝖙𝖎𝖑𝖊 𝖉𝖚𝖑𝖈𝖎. (Ter leering ende vermaeck)
𝕹𝖎𝖑 𝖛𝖔𝖑𝖊𝖓𝖙𝖎𝖇𝖚𝖘 𝖆𝖗𝖉𝖚𝖚𝖒. (Niets is moeilijk voor hen die willen)
https://henk.oegema.com  (Op RaspberryPi2)
Registered linux user 520520.  In gebruik: Ubuntu  24.04 Hobby's: Radio Amateur callsign: PA2HO.  Interesses: Raspberry Pi & Arduino & TELLO drone (voor AI)

Offline TopGear

  • Forumteam
Re: Python --> C/C++
« Reactie #5 Gepost op: 2021/04/20, 15:23:20 »
Wat was de uiteindelijke oplossing, Henk?

Re: Python --> C/C++
« Reactie #6 Gepost op: 2021/04/20, 18:11:20 »
Wat was de uiteindelijke oplossing, Henk?


void setup(){
  Serial.begin(115200);
}

void loop(){

  char dataPacket[] = "-0.01,0.55,-0.7,-1";
  char delim[] = ",";
  char *ptr = strtok(dataPacket, delim);   <====== moest deze lijn weglaten. (zoals erik1984 al aangaf)

  float q0 = atof(strtok(dataPacket, delim));
  float q1 = atof(strtok(NULL, delim));
  float q2 = atof(strtok(NULL, delim));
  float q3 = atof(strtok(NULL, delim));
 
  Serial.println(q0);
  Serial.println(q1);
  Serial.println(q2);
  Serial.println(q3);
 
  delay(1000);
}
𝕸𝖎𝖘𝖈𝖊𝖗𝖊 𝖚𝖙𝖎𝖑𝖊 𝖉𝖚𝖑𝖈𝖎. (Ter leering ende vermaeck)
𝕹𝖎𝖑 𝖛𝖔𝖑𝖊𝖓𝖙𝖎𝖇𝖚𝖘 𝖆𝖗𝖉𝖚𝖚𝖒. (Niets is moeilijk voor hen die willen)
https://henk.oegema.com  (Op RaspberryPi2)
Registered linux user 520520.  In gebruik: Ubuntu  24.04 Hobby's: Radio Amateur callsign: PA2HO.  Interesses: Raspberry Pi & Arduino & TELLO drone (voor AI)

Offline bart85

  • Lid
Re: Python --> C/C++
« Reactie #7 Gepost op: 2021/05/09, 11:05:59 »
waarom niet onderstaande?

float q[4];
q[0] = atof(strtok(dataPacket, delim));

for(int i=1;i<4;i++{
    q[i]=atof(strtok(NULL, delim));
}
Je leert maar mooi over weg gaan met de commandline.
Linus: "I'm happy with the people who are wandering around looking at the stars but I am looking at the ground and I want to fix the pothole before I fall in."
I look to the clouds behind me and see the thunder coming.

Re: Python --> C/C++
« Reactie #8 Gepost op: 2021/05/09, 12:48:26 »
waarom niet onderstaande?

float q[4];
q[0] = atof(strtok(dataPacket, delim));

for(int i=1;i<4;i++{
    q[i]=atof(strtok(NULL, delim));
}

Ja, had ook gekund.   :)
𝕸𝖎𝖘𝖈𝖊𝖗𝖊 𝖚𝖙𝖎𝖑𝖊 𝖉𝖚𝖑𝖈𝖎. (Ter leering ende vermaeck)
𝕹𝖎𝖑 𝖛𝖔𝖑𝖊𝖓𝖙𝖎𝖇𝖚𝖘 𝖆𝖗𝖉𝖚𝖚𝖒. (Niets is moeilijk voor hen die willen)
https://henk.oegema.com  (Op RaspberryPi2)
Registered linux user 520520.  In gebruik: Ubuntu  24.04 Hobby's: Radio Amateur callsign: PA2HO.  Interesses: Raspberry Pi & Arduino & TELLO drone (voor AI)

Offline bart85

  • Lid
Re: Python --> C/C++
« Reactie #9 Gepost op: 2021/05/10, 21:12:10 »
ik denk dat de python code wat mooier kan. Met onderstaande oneliner:

q = [float(i) for i in str(ad.readline(), 'utf-8').split(",")]

Dit creeert een array met 4 elementen van het type float. Dat zijn 7 regels naar 1 regel gebracht.
Met een simpele for loop kun je q uitlezen.
« Laatst bewerkt op: 2021/05/10, 21:25:30 door bart85 »
Je leert maar mooi over weg gaan met de commandline.
Linus: "I'm happy with the people who are wandering around looking at the stars but I am looking at the ground and I want to fix the pothole before I fall in."
I look to the clouds behind me and see the thunder coming.

Re: Python --> C/C++
« Reactie #10 Gepost op: 2021/05/11, 09:14:10 »
ik denk dat de python code wat mooier kan. Met onderstaande oneliner:

q = [float(i) for i in str(ad.readline(), 'utf-8').split(",")]

Dit creeert een array met 4 elementen van het type float. Dat zijn 7 regels naar 1 regel gebracht.
Met een simpele for loop kun je q uitlezen.

Er zijn altijd nog mensen die slimmer zijn dan ik.   :)
𝕸𝖎𝖘𝖈𝖊𝖗𝖊 𝖚𝖙𝖎𝖑𝖊 𝖉𝖚𝖑𝖈𝖎. (Ter leering ende vermaeck)
𝕹𝖎𝖑 𝖛𝖔𝖑𝖊𝖓𝖙𝖎𝖇𝖚𝖘 𝖆𝖗𝖉𝖚𝖚𝖒. (Niets is moeilijk voor hen die willen)
https://henk.oegema.com  (Op RaspberryPi2)
Registered linux user 520520.  In gebruik: Ubuntu  24.04 Hobby's: Radio Amateur callsign: PA2HO.  Interesses: Raspberry Pi & Arduino & TELLO drone (voor AI)

Offline bart85

  • Lid
Re: Python --> C/C++
« Reactie #11 Gepost op: 2021/05/12, 09:13:45 »
Mij lijkt in de python code een deel overbodig is. ad.readline() geeft waarschijnlijk al een string terug.

q = [float(i) for i in ad.readline().split(",")]
Werkt deze regel ook zonder errors?
Je leert maar mooi over weg gaan met de commandline.
Linus: "I'm happy with the people who are wandering around looking at the stars but I am looking at the ground and I want to fix the pothole before I fall in."
I look to the clouds behind me and see the thunder coming.

Re: Python --> C/C++
« Reactie #12 Gepost op: 2021/05/12, 15:03:05 »
Mij lijkt in de python code een deel overbodig is. ad.readline() geeft waarschijnlijk al een string terug.

q = [float(i) for i in ad.readline().split(",")]
Werkt deze regel ook zonder errors?

Ik heb het niet getest.
Had het project al afgesloten.
𝕸𝖎𝖘𝖈𝖊𝖗𝖊 𝖚𝖙𝖎𝖑𝖊 𝖉𝖚𝖑𝖈𝖎. (Ter leering ende vermaeck)
𝕹𝖎𝖑 𝖛𝖔𝖑𝖊𝖓𝖙𝖎𝖇𝖚𝖘 𝖆𝖗𝖉𝖚𝖚𝖒. (Niets is moeilijk voor hen die willen)
https://henk.oegema.com  (Op RaspberryPi2)
Registered linux user 520520.  In gebruik: Ubuntu  24.04 Hobby's: Radio Amateur callsign: PA2HO.  Interesses: Raspberry Pi & Arduino & TELLO drone (voor AI)