Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 20 of 20

Thread: Using ChatGPT / OpenAI for Arduino help

  1. #1
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,708

    Using ChatGPT / OpenAI for Arduino help

    Made an account at openai.com/blog/chatgpt/

    Useful? The list below is a 'thread' and interesting how it took feedback to correct offered code. And it stubbed in the ajustClock() code. Going further - it knows what ARM is and how to use the cycle counter.

    The second question I asked was:
    how accurate is a gps pps signal
    A:
    A GPS PPS (precise positioning service) signal is highly accurate and can be used to determine a location to within a few meters. The exact accuracy of a GPS PPS signal depends on various factors, such as the quality of the receiver and the number of satellites in view. In general, GPS PPS signals are accurate enough to be used for a wide range of applications, including navigation, surveying, and scientific research.
    That didn't get the expected detail - asked:
    what is the pps pin on a device
    A:
    A PPS (pulse per second) pin on a device is a signal output that produces a pulse at a regular interval of one second. This signal is typically used to synchronize the device's clock with an external time reference, such as a GPS signal or a network time server. The PPS pin can be used to improve the accuracy of the device's clock and ensure that it is synchronized with other devices. It is often used in applications where precise timing is critical, such as in networked systems and scientific instruments.
    That was the right direction so (with my typo on CLOCK):
    how can a c program use a pps signal to normalize a device closk timing
    A:
    In order to use a PPS signal to normalize the clock timing of a device in a C program, you would first need to configure the device to output the PPS signal on a specific pin. Then, you would need to use a C library or system call to read the signal from the PPS pin and use it to update the device's clock. This can be done by measuring the time between consecutive pulses and using that information to adjust the clock so that it remains synchronized with the PPS signal.
    Seems to be capable so:
    what would an arduino C program for that look like
    A:
    Here is an example of an Arduino C program that uses a PPS signal to normalize the device's clock timing:

    Code:
    #include <Arduino.h>
    
    // Pin where the PPS signal is connected
    const int PPS_PIN = 2;
    
    // Variables to keep track of the PPS signal
    unsigned long last_pps_time = 0;
    unsigned long current_pps_time = 0;
    
    void setup() {
      // Set the PPS pin as an input
      pinMode(PPS_PIN, INPUT);
    }
    
    void loop() {
      // Read the current time
      current_pps_time = micros();
    
      // Check if a PPS pulse has been received
      if (digitalRead(PPS_PIN) == HIGH) {
        // Calculate the time difference between the last and current PPS pulse
        unsigned long pps_interval = current_pps_time - last_pps_time;
    
        // Use the time difference to update the device's clock
        adjustClock(pps_interval);
    
        // Save the current time as the last PPS time
        last_pps_time = current_pps_time;
      }
    }
    
    // Function to adjust the clock using the PPS interval
    void adjustClock(unsigned long interval) {
      // TODO: Implement clock adjustment using the PPS interval
    }
    This code sets the PPS pin as an input and reads the signal in the loop() function. When a PPS pulse is detected, the time difference between the last and current pulse is calculated and used to adjust the device's clock. This process is repeated continuously to keep the device's clock synchronized with the PPS signal.

    Reading the code:
    That program does not set current_pps_time:
    A:
    You are correct, the current_pps_time variable is not being initialized in the example code. This can be fixed by adding the following line to the setup() function:

    Code:
    current_pps_time = micros();
    This will initialize the current_pps_time variable to the current time, measured in microseconds, when the program starts. The loop() function will then use this value to calculate the time difference between the last and current PPS pulses. Here is the updated code with the initialization added:

    Code:
    #include <Arduino.h>
    
    // Pin where the PPS signal is connected
    const int PPS_PIN = 2;
    
    // Variables to keep track of the PPS signal
    unsigned long last_pps_time = 0;
    unsigned long current_pps_time = 0;
    
    void setup() {
      // Set the PPS pin as an input
      pinMode(PPS_PIN, INPUT);
    
      // Initialize the current PPS time
      current_pps_time = micros();
    }
    
    void loop() {
      // Read the current time
      current_pps_time = micros();
    
      // Check if a PPS pulse has been received
      if (digitalRead(PPS_PIN) == HIGH) {
        // Calculate the time difference between the last and current PPS pulse
        unsigned long pps_interval = current_pps_time - last_pps_time;
    
        // Use the time difference to update the device's clock
        adjustClock(pps_interval);
    
        // Save the current time as the last PPS time
        last_pps_time = current_pps_time;
      }
    }
    
    // Function to adjust the clock using the PPS interval
    void adjustClock(unsigned long interval) {
      // TODO: Implement clock adjustment using the PPS interval
    }
    This code should now properly initialize the current_pps_time variable and use it to calculate the time difference between consecutive PPS pulses.
    Going further:
    The device is ARM and has a cycle counter, could it be used for finer correction
    A:
    Yes, if the device has a cycle counter, it can be used to make more precise adjustments to the clock using the PPS signal. The cycle counter is a hardware feature that allows the device to count the number of cycles that have elapsed since it was last reset. By measuring the number of cycles between consecutive PPS pulses, the device can calculate the exact time difference with a high degree of precision.

    To use the cycle counter in an Arduino C program, you would first need to initialize it in the setup() function:

    Copy code
    // Initialize the cycle counter
    DWT->CYCCNT = 0;
    DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
    This code enables the cycle counter and resets it to zero. You can then use the following code to read the current cycle count in the loop() function:

    Copy code
    // Read the current cycle count
    unsigned long cycle_count = DWT->CYCCNT;
    This code reads the current value of the cycle counter and saves it in the cycle_count variable. You can then use this value to calculate the time difference between consecutive PPS pulses using the cycle counter's clock frequency. Here is an updated version of the code that uses the cycle counter to adjust the device's clock:

    Copy code
    #include <Arduino.h>
    #include <inttypes.h>

    // Pin where the PPS signal is connected
    const int PPS_PIN = 2;

    // Clock frequency of the cycle counter, in Hz
    const uint32_t CYCLE_COUNTER_FREQ = 100000000;

    // Variables to keep track of the PPS signal
    unsigned long last_pps_time = 0;
    unsigned long current_pps_time = 0;
    unsigned long last_cycle_count = 0;
    unsigned long current_cycle_count = 0;

    void setup() {
    // Set the PPS pin as an input
    pinMode(PPS_PIN, INPUT);

    // Initialize the cycle counter
    DWT->CYCCNT = 0;
    DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;

    // Initialize the current PPS time and cycle count
    current_pps_time = micros();
    current_cycle_count = DWT->CYCCNT;
    }

    void loop() {
    // Read the current time and cycle count
    current_pps_time = micros();
    current_cycle_count = DWT->CYCCNT;

    // Check if a PPS pulse has been received
    if (digitalRead(PPS_PIN) == HIGH) {
    // Calculate the time difference between the last and current PPS pulse
    unsigned long pps_interval = current_pps_time - last_pps_time;

    // Calculate the cycle count difference between the last and current PPS pulse
    unsigned long cycle_count_diff = current_cycle_count - last_cycle_count;

    // Use the time and cycle count differences to update the device's clock
    adjustClock(pps_interval, cycle_count_diff, CYCLE_COUNTER_FREQ);

    // Save the current time and cycle count as the last PPS time and count
    last_pps_time = current_pps_time;
    last_cycle_
    Opps - failed to complete output:
    that complete answer did not appear
    A:
    I apologize if my previous answer was incomplete. Here is the complete updated code that uses the cycle counter to adjust the device's clock:

    Code:
    #include <Arduino.h>
    #include <inttypes.h>
    
    // Pin where the PPS signal is connected
    const int PPS_PIN = 2;
    
    // Clock frequency of the cycle counter, in Hz
    const uint32_t CYCLE_COUNTER_FREQ = 100000000;
    
    // Variables to keep track of the PPS signal
    unsigned long last_pps_time = 0;
    unsigned long current_pps_time = 0;
    unsigned long last_cycle_count = 0;
    unsigned long current_cycle_count = 0;
    
    void setup() {
      // Set the PPS pin as an input
      pinMode(PPS_PIN, INPUT);
    
      // Initialize the cycle counter
      DWT->CYCCNT = 0;
      DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
    
      // Initialize the current PPS time and cycle count
      current_pps_time = micros();
      current_cycle_count = DWT->CYCCNT;
    }
    
    void loop() {
      // Read the current time and cycle count
      current_pps_time = micros();
      current_cycle_count = DWT->CYCCNT;
    
      // Check if a PPS pulse has been received
      if (digitalRead(PPS_PIN) == HIGH) {
        // Calculate the time difference between the last and current PPS pulse
        unsigned long pps_interval = current_pps_time - last_pps_time;
    
        // Calculate the cycle count difference between the last and current PPS pulse
        unsigned long cycle_count_diff = current_cycle_count - last_cycle_count;
    
        // Use the time and cycle count differences to update the device's clock
        adjustClock(pps_interval, cycle_count_diff, CYCLE_COUNTER_FREQ);
    
        // Save the current time and cycle count as the last PPS time and count
        last_pps_time = current_pps_time;
        last_cycle_count = current_cycle_count;
      }
    }
    
    // Function to adjust the clock using the PPS interval and cycle count difference
    void adjustClock(unsigned long interval, unsigned long cycle_count_diff, uint32_t freq) {
      // TODO: Implement clock adjustment using the PPS interval and cycle count difference
    }
    This code reads the current time and cycle count in the loop() function, and uses the time and cycle count differences between consecutive PPS pulses to adjust the device's clock. By using the cycle counter, the clock can be adjusted with a higher level of precision compared to using only the time difference between PPS pulses.

  2. #2
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    8,539
    @defragster - looks like fun. Did you try the sketch to see if it works

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    27,401
    It used the CMSIS-style syntax for the ARM DWT cycle counter.

  4. #4
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,910
    I was going to try it but in order to register they want name, email address (OKish, so far) and phone number (NOPE).
    They don't need my phone number.

    Pete

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,708
    @mjs513, I had not tried it - as Paul notes the DWT-> usage would need to be edited.

    Funny it added comments. And amazing how it holds context across questions just like a thread here.

    Also, noteworthy when it understood and not just CopyPasta: "You are correct, the current_pps_time variable is not being initialized in the example code." So that required seeing that to get near a usable solution adding micros().

    I asked another about Prime numbers, and it gave a simple sieve and another with optimizations to go from 'i += 1' to 'i +=6' during the search loop. So, I asked "Why use i += 6", and it explained.

    Just realized before I closed the thread, I should have asked for it to be written in Python and other languages. I might have asked for an _isr() version as well but closed the thread too quickly after posting here. Could be a useful learning tool.

    That was easier and more direct and fruitful than a web search. Seeing comments that this will end g00gle in 2 years has some validity.

    'It'/AI is quick to point out it doesn't have an internet connect to search the web. It gave that 'no internet boilerplate' to my first typed query: "WTF". Then asked what it stood for, and it explained the general meaning and usage of 'what the f**k'.

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,708
    Quote Originally Posted by el_supremo View Post
    I was going to try it but in order to register they want name, email address (OKish, so far) and phone number (NOPE).
    They don't need my phone number.

    Pete
    Yeah - hated doing that ... but wanted to try it.

    It has web info - but was a scan done some time back:
    Click image for larger version. 

Name:	GPTteensy.jpg 
Views:	34 
Size:	112.0 KB 
ID:	29917
    I gave that answer a thumbs down with notes.

  7. #7
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,910
    Yeah - hated doing that ... but wanted to try it.
    I have a problem I'm trying to solve and thought it would be neat to see if it could even get close to an answer.
    But I don't need to know that badly.

    Pete

  8. #8
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,708
    Quote Originally Posted by el_supremo View Post
    I have a problem I'm trying to solve and thought it would be neat to see if it could even get close to an answer.
    But I don't need to know that badly.

    Pete
    What's the question ... can I try it?

  9. #9
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,910
    It's actually a multi-part thing that builds up to the big one. I'll have to type it up and I'll PM it. If anything interesting comes of it, you can post the results


    Pete

  10. #10
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,708
    Quote Originally Posted by el_supremo View Post
    It's actually a multi-part thing that builds up to the big one. I'll have to type it up and I'll PM it. If anything interesting comes of it, you can post the results


    Pete
    Sounds like fun

  11. #11
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,708
    Got the questions from Pete and got him a txt file of the responses, with C code.

    Just asked it to 'provide a python version' and it did for one prior example. Like in p#1 the output appearing in the web browser entered like it was being typed/block inserted and it failed to complete. Didn't ask for a redo - but here is a screen image of the output. The code is formatted and color coded, this the python version.
    Click image for larger version. 

Name:	PythonDCA.png 
Views:	26 
Size:	70.9 KB 
ID:	29918

  12. #12
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,910
    Thanks for doing it
    Can you post a text version of the C code? I don't think it is right, but I don't know Python that well.

    Pete

  13. #13
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,708
    Pete: I sent email with the full 17KB as a text file to look at - here with my email address removed:
    PeteAI.txt

    Here's the DCA C code:
    Sure, here is an example of how the distance of closest approach (DCA) could be calculated in the C programming language:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    // Function for calculating the distance of closest approach (DCA)
    double dca(double distance, double bearing) {
      return distance * sin(bearing);
    }
    
    int main() {
      // Example: calculate the DCA given the distance and bearing
      double distance = 100.0; // distance between the jet and your home in kilometers
      double bearing = 45.0; // bearing of the jet relative to your home in degrees
      double dca = dca(distance, bearing);
      printf("The DCA is %f kilometers.\n", dca);
      return 0;
    }
    This code defines a function called dca() that calculates the distance of closest approach (DCA) given the distance between the jet and your home and the jet's bearing relative to your home. The function takes as input the distance and bearing and returns the DCA in kilometers.

    In the main() function, the code provides example input values for distance and bearing and then calls the dca() function to calculate the DCA. The DCA is then printed to the console.

    This is just one way to calculate the DCA in C. Depending on your specific requirements and preferences, you may want to modify this code to suit your needs.

  14. #14
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,910
    The answer to the first question is spot on and the solution to the second one is trivial, given the answer to the first.
    But then it loses the plot at the third question. It reverts to planar geometry to solve the third and remaining questions.
    The answer to the last question (true north bearing of closest approach of the jet) is wrong. This bit, I don't understand at all:
    "For example, let's say that your home is located at a bearing of 90 degrees relative to true north."

    Anyway, it's an interesting program but I don't think anybody's job is at risk yet

    Thanks Tim.
    Pete

  15. #15
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,708
    The session was still open and asked again to get this - then follow up seems to have broken it 'had an error while processing ...'
    The examples I provided for calculating the distance of closest approach (DCA), the jet's elevation above the horizon, and the true north bearing of DCA in C and Python are based on simple planar geometry, which assumes that the jet is flying at a constant altitude and that there are no other factors that could affect its elevation or bearing. This is a simplified approach that can provide a rough estimate of the jet's position and orientation, but it is not necessarily precise or accurate.

    If you want to calculate the jet's position and orientation in three-dimensional space more precisely, you would need to use more advanced mathematical techniques that take into account the jet's altitude and any other factors that could affect its position and orientation. This could involve using techniques from vector calculus, spherical geometry, or other areas of mathematics to calculate the jet's position and orientation in three-dimensional space.

    It's important to note that even with these advanced techniques, the calculations may not be perfectly precise or accurate due to the many variables that can affect the jet's position and orientation, such as wind speed and direction, air traffic control restrictions, and other factors. Additionally, the actual position and orientation of the jet may change over time, so any calculations would need to be updated regularly in order to remain accurate.

  16. #16
    Junior Member
    Join Date
    May 2017
    Location
    Atlanta, Georgia
    Posts
    14
    About ChatGPT code offerings:
    I appreciate the comments into it's replys added based on the query psudo-code...
    ...but mostly the orthogonal formatting and correct indentations throughout the code.
    [The "copy code" button in the HTML is a nice Human touch too!]

  17. #17
    Senior Member
    Join Date
    Feb 2018
    Location
    Corvallis, OR
    Posts
    408
    I wouldn't rely too heavily on the example code on post #13. The dca function is taking an input parameter of a bearing in degrees and using the sin() function without converting the angle to radians!

  18. #18
    Senior Member
    Join Date
    Oct 2019
    Posts
    378
    Just a side note - I use GPT to help with the Teensy coding as well as Python/SQL.
    But the current model (GPT3.5) is only trained with data up to 2021 (when it was "released"). Any info or code examples that came after that - it's not in the training model.

  19. #19
    Member
    Join Date
    Nov 2020
    Location
    Montana, USA
    Posts
    29
    Not sure if this is of interest but back in 2001 I was toying around with AliceBot (the website is not owned by the same people anymore). I played around with it until about 2016. I have all the origional files as downloaded from the origional site which includes programs written in JAVA, Python, C, Visual Basic, and .NET . They were all opensource and atleast one program was modified and used by PandoraBots in a web interface, not sure if they're still around... I uploaded it to my OneDrive. Here is the link: https://1drv.ms/u/s!AhetIkCC6afBjU61...63sfq?e=VC5qFg .

    It's older code but all required files including the AIML that won the loebner prize. Not sure if you've ever heard of it but thought i'd share the sources that I had laying around that have been untouched for several years and some may still be available with newer version on sites like github and sourceforge.

    cheers,
    Jorge

  20. #20
    You have to guide it. It gave a pretty good template for a state machine, but when I asked it is the top priority message ID for a CAN bus node, it provided exactly the wrong answer. It never says maybe or I think, it just presents the answer outright.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •